goctl rpc

Overview

goctl rpc is one of the core modules in goctl, which can quickly generate a rpc service with .proto file, and you can complete a rpc service development and work properly if you only start a rpc demonstration project for go-zero without even encoding.In the traditional rpc project, we create directories at all levels, prepare structures, define routes, add logical files, a series of operations that take approximately 5 to 6 minutes down to actually enter business logic writing if the entire code is calculated according to the business requirements of a deal, without taking into account the errors that may arise in the preparation process, which will increase positively as the number of protocols grow, and goctl rpc will completely replace you to do this part, regardless of how many of your agreements are, and eventually, just 10 seconds to complete.

goctl rpc directive

  1. $ goctl rpc --help
  2. Generate rpc code
  3. Usage:
  4. goctl rpc [flags]
  5. goctl rpc [command]
  6. Available Commands:
  7. new Generate rpc demo service
  8. protoc Generate grpc code
  9. template Generate proto template
  10. Flags:
  11. --branch string The branch of the remote repo, it does work with --remote
  12. -h, --help help for rpc
  13. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  14. --o string Output a sample proto file
  15. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  16. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  17. Use "goctl rpc [command] --help" for more information about a command.
goctl rpc - 图1 Parameter fieldgoctl rpc - 图2 Parameter Typegoctl rpc - 图3 Required?goctl rpc - 图4 Default valuegoctl rpc - 图5 Parameter Description
branchstringNOEmpty stringTemplate repository branch, with —remote usage
homestringNO~/.goctlTemplate repository local path higher than —remote
ostringNOEmpty stringOutput api file
remotestringNOEmpty stringTemplate repository remote path

Example:generates proto file

  1. $ goctl rpc --o greet.proto

goctl rpc new

Quickly generate a rpc service that receives a terminal parameter to specify the service name.

  1. $ goctl rpc new --help
  2. Generate rpc demo service
  3. Usage:
  4. goctl rpc new [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -h, --help help for new
  8. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  9. --idea Whether the command execution environment is from idea plugin.
  10. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  11. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  12. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero")
  13. -v, --verbose Enable log output
goctl rpc - 图6 Parameter fieldgoctl rpc - 图7 Parameter Typegoctl rpc - 图8 Required?goctl rpc - 图9 Default valuegoctl rpc - 图10 Parameter Description
branchstringNOEmpty stringTemplate repository branch, with —remote usage
homestringNO~/.goctlTemplate repository local path higher than —remote
ideaboolNOfalseUse this field only for plugins, please ignore this field
remotestringNOEmpty stringTemplate repository remote path
stylestringNOgozeroFilename style, reference file style

Example:

  1. $ goctl rpc new greet

goctl rpc protoc

Generate rpc service based on protobefer file.

  1. $ goctl rpc protoc --help
  2. Generate grpc code
  3. Usage:
  4. goctl rpc protoc [flags]
  5. Examples:
  6. goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.
  7. Flags:
  8. --branch string The branch of the remote repo, it does work with --remote
  9. -h, --help help for protoc
  10. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  11. -m, --multiple Generated in multiple rpc service mode
  12. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  13. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  14. --style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero")
  15. -v, --verbose Enable log output
  16. --zrpc_out string The zrpc output directory
goctl rpc - 图11 Parameter fieldgoctl rpc - 图12 Parameter Typegoctl rpc - 图13 Required?goctl rpc - 图14 Default valuegoctl rpc - 图15 Parameter Description
branchstringNOEmpty stringTemplate repository branch, with —remote usage
homestringNO~/.goctlTemplate repository local path higher than —remote
multipleboolNOfalseWhether to generate multiple rpc services
remotestringNOEmpty stringTemplate repository remote path
stylestringNOgozeroFilename style, reference file style
zrpc_outstringNOEmpty stringOutput directory

In addition to the above parameters, there are native arguments that support the protoc directive. See Go Generated Code Guide for details.

Example:

  1. # generate rpc code without group
  2. $ goctl rpc protoc greet.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.
  3. # generate rpc code with group
  4. $ goctl rpc protoc greet.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=. -m
goctl rpc - 图16tip

multiple rpc services generate examples (rpc group) with reference service group

::tip Small skills
goctl rpc protoc directives are longer and many parameters are well understood to mean the following form:

goctl rpc ${protoc usage} —zrpc_out=${output directory}, e.g. directive goctl rpc protoc greet.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=., of which protoc greet.proto --go_out=./pb --go-grpc_out=./pb is entirely the usage of protoc directives, but --zrpc_out=. is the parameter of the goctl rpc protoc directive. :::

::caution Note
goctl rpc protoc protoc generated rpc service with some information about proto:

  1. Proto file will not process import proto file and will need to process it manually if there are import statements.
  2. The requester and response body in rpc service must be the message in the current proto file, not the message in the import file.

:::

goctl rpc template

Quickly generate a proto template file that receives a proto file name parameter.

::caution takes note of
the directive has been deprecated, and recommends using goctl rpc -o. :::

  1. $ goctl rpc template --help
  2. Generate proto template
  3. Usage:
  4. goctl rpc template [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -h, --help help for template
  8. --home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  9. --o string Output a sample proto file
  10. --remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
  11. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
goctl rpc - 图17 Parameter fieldgoctl rpc - 图18 Parameter Typegoctl rpc - 图19 Required?goctl rpc - 图20 Default valuegoctl rpc - 图21 Parameter Description
branchstringNOEmpty stringTemplate repository branch, with —remote usage
homestringNO~/.goctlTemplate repository local path higher than —remote
ostringNOEmpty stringOutput File Path
remotestringNOEmpty stringTemplate repository remote path

Example:

  1. $ goctl rpc template -o greet.proto

References