|
hai 1 ano | |
---|---|---|
.. | ||
cli | hai 1 ano | |
execx | hai 1 ano | |
generator | hai 1 ano | |
parser | hai 1 ano | |
README.md | hai 1 ano | |
cmd.go | hai 1 ano |
Goctl Rpc是goctl
脚手架下的一个rpc服务代码生成模块,支持proto模板生成和rpc服务代码生成,通过此工具生成代码你只需要关注业务逻辑编写而不用去编写一些重复性的代码。这使得我们把精力重心放在业务上,从而加快了开发效率且降低了代码出错率。
通过命令 goctl rpc new ${servieName}
生成
如生成greet rpc服务:
goctl rpc new greet
执行后代码结构如下:
.
└── greet
├── etc
│ └── greet.yaml
├── greet
│ ├── greet.go
│ ├── greet.pb.go
│ └── greet_grpc.pb.go
├── greet.go
├── greet.proto
└── internal
├── config
│ └── config.go
├── logic
│ └── pinglogic.go
├── server
│ └── greetserver.go
└── svc
└── servicecontext.go
生成proto模板
$ goctl rpc template -o=user.proto
syntax = "proto3";
package user;
option go_package="./user";
message Request {
string ping = 1;
}
message Response {
string pong = 1;
}
service User {
rpc Ping(Request) returns(Response);
}
生成rpc服务代码
$ goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=.
$ goctl rpc protoc -h
Generate grpc code
Usage:
goctl rpc protoc [flags]
Examples:
goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.
Flags:
--branch string The branch of the remote repo, it does work with --remote
-h, --help help for protoc
--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
-m, --multiple Generated in multiple rpc service mode
--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
The git repo directory must be consistent with the https://github.com/wuntsong-org/go-zero-plus-template directory structure
--style string The file naming format, see [https://github.com/wuntsong-org/go-zero-plus/tree/master/tools/goctl/config/readme.md] (default "gozero")
-v, --verbose Enable log output
--zrpc_out string The zrpc output directory
--multiple
是否开启多个 rpc service 生成,如果开启,则满足一下新特性
- 支持 1 到多个 rpc service
- 生成 rpc 服务会按照服务名称分组(尽管只有一个 rpc service)
- rpc client 的文件目录变更为固定名称
client
如果不开启,则和旧版本 rpc 生成逻辑一样(兼容)
- 有且只能有一个 rpc service
详情见 example/rpc
源 proto 文件
syntax = "proto3";
package hello;
option go_package = "./hello";
message HelloReq {
string in = 1;
}
message HelloResp {
string msg = 1;
}
service Greet {
rpc SayHello(HelloReq) returns (HelloResp);
}
hello
├── client // 区别1:rpc client 目录固定为 client 名称
│ └── greet // 区别2:会按照 rpc service 名称分组
│ └── greet.go
├── etc
│ └── hello.yaml
├── hello.go
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ └── greet // 区别2:会按照 rpc service 名称分组
│ │ └── sayhellologic.go
│ ├── server
│ │ └── greet // 区别2:会按照 rpc service 名称分组
│ │ └── greetserver.go
│ └── svc
│ └── servicecontext.go
└── pb
└── hello
├── hello.pb.go
└── hello_grpc.pb.go
hello
├── etc
│ └── hello.yaml
├── greet
│ └── greet.go
├── hello.go
├── internal
│ ├── config
│ │ └── config.go
│ ├── logic
│ │ └── sayhellologic.go
│ ├── server
│ │ └── greetserver.go
│ └── svc
│ └── servicecontext.go
└── pb
└── hello
├── hello.pb.go
└── hello_grpc.pb.go