|
|
5 years ago | |
|---|---|---|
| .. | ||
| cli | 5 years ago | |
| execx | 5 years ago | |
| generator | 5 years ago | |
| parser | 5 years ago | |
| README.md | 5 years ago | |
Goctl Rpc是goctl脚手架下的一个rpc服务代码生成模块,支持proto模板生成和rpc服务代码生成,通过此工具生成代码你只需要关注业务逻辑编写而不用去编写一些重复性的代码。这使得我们把精力重心放在业务上,从而加快了开发效率且降低了代码出错率。
通过命令 goctl rpc new ${servieName}生成
如生成greet rpc服务:
goctl rpc new greet
执行后代码结构如下:
.
├── etc // 配置文件
│ └── greet.yaml
├── go.mod
├── greet // client call
│ └── greet.go
├── greet.go // main entry
├── greet.proto
└── internal
├── config // 配置声明
│ └── config.go
├── greet // pb.go
│ └── greet.pb.go
├── logic // logic
│ └── pinglogic.go
├── server // pb invoker
│ └── greetserver.go
└── svc // resource dependency
└── servicecontext.go
rpc一键生成常见问题解决,见 常见问题解决
goctl rpc template -o=user.proto
syntax = "proto3";
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
goctl rpc proto -h
NAME:
goctl rpc proto - generate rpc from proto
USAGE:
goctl rpc proto [command options] [arguments...]
OPTIONS:
--src value, -s value the file path of the proto source file
--proto_path value, -I value native command of protoc,specify the directory in which to search for imports. [optional]
--dir value, -d value the target path of the code,default path is "${pwd}". [optional]
--idea whether the command execution environment is from idea plugin. [optional]
goctl rpc -I={path1} -I={path2} ...,在没有import时可不填。当前proto路径不用指定,已经内置,-I的详细用法请参考protoc -h关注业务代码编写,将重复性、与业务无关的工作交给goctl,生成好rpc服务代码后,开发人员仅需要修改
google.golang.org/grpc需要降级到v1.26.0,且protoc-gen-go版本不能高于v1.3.2(see https://github.com/grpc/grpc-go/issues/3347)即
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
// Code generated by goctl. DO NOT EDIT!
// Source: xxx.proto
的标识,请注意不要将也写业务性代码写在里面。
proto示例:
syntax = "proto3";
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和return 不支持import
}
syntax = "proto3";
package greet;
import "base/common.proto"
message Request {
base.In in = 1;// 支持import
}
message Response {
base.Out out = 2;// 支持import
}
service Greet {
rpc Ping(Request) returns(Response);
}
pb/xx.pb.go:220:7: undefined: grpc.ClientConnInterface
pb/xx.pb.go:224:11: undefined: grpc.SupportPackageIsVersion6
pb/xx.pb.go:234:5: undefined: grpc.ClientConnInterface
pb/xx.pb.go:237:24: undefined: grpc.ClientConnInterface
解决方法:请将protoc-gen-go版本降至v1.3.2及一下
# go.etcd.io/etcd/clientv3/balancer/picker
../../../go/pkg/mod/go.etcd.io/etcd@v0.0.0-20200402134248-51bdeb39e698/clientv3/balancer/picker/err.go:25:9: cannot use &errPicker literal (type *errPicker) as type Picker in return argument:*errPicker does not implement Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
../../../go/pkg/mod/go.etcd.io/etcd@v0.0.0-20200402134248-51bdeb39e698/clientv3/balancer/picker/roundrobin_balanced.go:33:9: cannot use &rrBalanced literal (type *rrBalanced) as type Picker in return argument:
*rrBalanced does not implement Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
#github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c
../../../go/pkg/mod/github.com/tal-tech/go-zero@v1.0.12/zrpc/internal/balancer/p2c/p2c.go:41:32: not enough arguments in call to base.NewBalancerBuilder
have (string, *p2cPickerBuilder)
want (string, base.PickerBuilder, base.Config)
../../../go/pkg/mod/github.com/tal-tech/go-zero@v1.0.12/zrpc/internal/balancer/p2c/p2c.go:58:9: cannot use &p2cPicker literal (type *p2cPicker) as type balancer.Picker in return argument:
*p2cPicker does not implement balancer.Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
解决方法:
```golang
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
```