RPC Commands

    Goctl Rpc is a rpc service code generation module under scaffolding, which supports proto template generation and rpc service code generation. To generate code through this tool, you only need to pay attention to the business logic writing instead of writing some repetitive code. This allows us to focus on the business, thereby speeding up development efficiency and reducing the code error rate.

    • Simple and easy to use
    • Quickly improve development efficiency
    • Low error rate
    • Close to protoc

    Generated by the command goctl rpc new ${servieName}

    Such as generating greet rpc service:

    The code structure after execution is as follows:

    1. .
    2. ├── etc // yaml configuration file
    3. └── greet.yaml
    4. ├── go.mod
    5. ├── greet // pb.go folder①
    6. └── greet.pb.go
    7. ├── greet.go // main entry
    8. ├── greet.proto // proto source file
    9. ├── greetclient // call logic ②
    10. └── greet.go
    11. └── internal
    12. ├── config // yaml configuration corresponding entity
    13. └── config.go
    14. ├── logic //business code
    15. └── pinglogic.go
    16. ├── server // rpc server
    17. └── greetserver.go
    18. └── svc // dependent resources
    19. └── servicecontext.go
    1. if option.Name == "go_package" {
    2. ret.GoPackage = option.Constant.Source
    3. }
    4. ...
    5. if len(ret.GoPackage) == 0 {
    6. }
    7. ret.PbPackage = GoSanitized(filepath.Base(ret.GoPackage))
    8. ...
    1. if strings.ToLower(proto.Service.Name) == strings.ToLower(proto.GoPackage) {
    2. callDir = filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name+"_client").ToCamel()))

    rpc one-click generation to solve common problems, see

    The way two: Generate rpc service by specifying proto

    • Generate proto template

      1. syntax = "proto3";
      2. package remote;
      3. option go_package = "remote";
      4. message Request {
      5. string username = 1;
      6. string password = 2;
      7. }
      8. message Response {
      9. string name = 1;
      10. string gender = 2;
      11. }
      12. service User {
      13. rpc Login(Request)returns(Response);
      14. }
      1. goctl rpc proto -src user.proto -dir .
    • Installed go environment
    • Protoc&protoc-gen-go is installed, and environment variables have been set
    • For more questions, please see
    1. goctl rpc proto -h

    参数说明

    • —src: required, the proto data source, currently supports the generation of a single proto file
    • —proto_path: optional. The protoc native subcommand is used to specify where to find proto import. You can specify multiple paths, such as goctl rpc -I={path1} -I={path2} ..., in You can leave it blank when there is no import. The current proto path does not need to be specified, it is already built-in. For the detailed usage of -I, please refer to protoc -h
    • —dir: optional, the default is the directory where the proto file is located, the target directory of the generated code
    • —style value the file naming format, see []
    • —idea: optional, whether it is executed in the idea plug-in, terminal execution can be ignored

    Pay attention to business code writing, hand over repetitive and business-unrelated work to goctl, after generating the rpc service code, developers only need to modify.

    • Preparation of configuration files in the service (etc/xx.json, internal/config/config.go)
    • Writing business logic in the service (internal/logic/xxlogic.go)
    • Preparation of resource context in the service (internal/svc/servicecontext.go)

    Precautions

    • proto does not support the generation of multiple files at the same time
    • proto does not support the introduction of external dependency packages, and message does not support inline
    • The requestType and returnType in rpc must be defined in the main proto file. For the message in proto, other proto files can be imported like protoc.

    proto example:

    1. syntax = "proto3";
    2. package greet;
    3. option go_package = "greet";
    4. import "base/common.proto"
    5. message Request {
    6. string ping = 1;
    7. }
    8. message Response {
    9. string pong = 1;
    10. }
    11. service Greet {
    12. rpc Ping(base.In) returns(base.Out);// request and return do not support import
    13. }

    Import correctly

    1. syntax = "proto3";
    2. package greet;
    3. option go_package = "greet";
    4. import "base/common.proto"
    5. message Request {
    6. base.In in = 1;
    7. }
    8. message Response {
    9. base.Out out = 2;
    10. }
    11. service Greet {
    12. rpc Ping(Request) returns(Response);
    13. }

    Guess you wants