RPC Commands
Goctl Rpc is an rpc service code generation module under goctl
scaffolding, supporting proto template generation and rpc service code generation, through this tool to generate code you only need to focus on business logic writing instead of writing some repetitive code. This allows us to focus on the business, thus speeding up the development efficiency and reducing the error rate of the code.
#
Features- Easy to use
- Fast and efficient development
- Low error rate
- Close to protoc
#
Quick start#
Way 1: Quickly generate greet servicesGenerated by the command goctl rpc new ${servieName}
If you generate the greet rpc service.
The code structure after execution is as follows:
tip
See rpc directory for details of the new version of the directory.
#
Way 2: Generate rpc service by specifying proto生成proto模板
Generate rpc service code
#
Preparing- Installed the go environment
- protoc & protoc-gen-go are installed, and environment variables have been set
- For more questions, see Notes
#
Usage#
rpc service generation usageYou can understand that zrpc code generation is done with goctl rpc $protoc_command --zrpc_out=${output}
templates, as in the original command to generate grpc code
then the zrpc code command would be
tip
- --go_out and --go-grpc_out must generate the same final directory
- The final directories generated by --go_out & --go-grpc_out and --zrpc_out must not be the same directory, otherwise pb.go and _grpc.pb.go are at the same level as the main function, which is not allowed.
The directories produced by --go_out and --go-grpc_out are affected by the go_package values in the --go_opt and --grpc-go_opt and proto source files. To understand the generation logic here, it is recommended to read the Official documentation: Go Generated Code
#
What developers need to doFocus on business code writing, leave the repetitive, non-business related work to goctl, after generating the rpc service code, developers only need to modify
- write configuration files in the service (etc/xx.json, internal/config/config.go)
- Business logic writing in the service (internal/logic/xxlogic.go)
- Writing of resource contexts in services (internal/svc/servicecontext.go)
#
Cautionproto does not support simultaneous generation of multiple files at the moment
proto does not support external dependency package introduction, message does not support inline
Currently main file, shared file, handler file will be forced to overwrite, and developers need to write manually will not overwrite the generation, this kind of code in the header are
Please be careful not to write business code in it; and do not write it inside business code.
#
proto import- For requestType and returnType in rpc must be defined in main proto file, for message in proto you can import other proto files like protoc.
proto example: