hz usage(protobuf)
Create a project based on protobuf IDL
new: Create a new project
-
Create the protobuf IDL file in the current directory
-
Create api.proto
api.proto is an annotation file provided by hz, with the following content. Please import the file in the annotated proto file.
If you want to expand the use of annotations on your own, please do not use “5” as the beginning of the sequence number to avoid conflicts. For example, “optional string xxx = 77777;”.
-
Create Master IDL
-
-
Create a new project
-
Modify the handler and add your own logic
-
Compile the project
-
Run the project and test it
Run the project:
Test:
If it returns
{"RespBody":"hello,hertz"}
, it works.
update: Update an existing Hertz project
-
If your protobuf IDL is updated, for example:
-
Switch to the directory where the new command was executed and update the modified IDL
Note.
-
If the dependency of the IDL is not in the same path as the main IDL, you need to add the
-I
option, which means the path to search for the IDL, equivalent to the-I
command of protoc. -
When writing update command, you need to specify not only the IDL file that defines
service
, but also all the dependency files, because protobuf’s dependency files will not be updated automatically.-
As you can see
Add new method under
biz/handler/hello/hello_service.go
; The filenew_service.go
and the corresponding “Method3” method have been added underbiz/handler/hello
.Now let’s develop the “Method2” interface:
-
-
Compile the project
-
Run the project and test it
Run the project:
Test:
If it returns
{"Resp":"Other method: other method"}
, it works.
For more example code, please refer to code.