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:
.
├── etc // yaml configuration file
│ └── greet.yaml
├── go.mod
├── greet // pb.go folder①
│ └── greet.pb.go
├── greet.go // main entry
├── greet.proto // proto source file
├── greetclient // call logic ②
│ └── greet.go
└── internal
├── config // yaml configuration corresponding entity
│ └── config.go
├── logic //business code
│ └── pinglogic.go
├── server // rpc server
│ └── greetserver.go
└── svc // dependent resources
└── servicecontext.go
if option.Name == "go_package" {
ret.GoPackage = option.Constant.Source
}
...
if len(ret.GoPackage) == 0 {
}
ret.PbPackage = GoSanitized(filepath.Base(ret.GoPackage))
...
if strings.ToLower(proto.Service.Name) == strings.ToLower(proto.GoPackage) {
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
syntax = "proto3";
package remote;
option go_package = "remote";
message Request {
string username = 1;
string password = 2;
}
message Response {
string name = 1;
string gender = 2;
}
service User {
rpc Login(Request)returns(Response);
}
-
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
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 toprotoc -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:
syntax = "proto3";
package greet;
option go_package = "greet";
import "base/common.proto"
message Request {
string ping = 1;
}
message Response {
string pong = 1;
}
service Greet {
rpc Ping(base.In) returns(base.Out);// request and return do not support import
}
Import correctly
syntax = "proto3";
package greet;
option go_package = "greet";
import "base/common.proto"
message Request {
base.In in = 1;
}
message Response {
base.Out out = 2;
}
service Greet {
rpc Ping(Request) returns(Response);
}