项目创建.md 3.1 KB

项目创建

新建项目目录

创建项目目录hello,然后进入hello目录

mkdir hello && cd hello

goctl创建模板

使用goctl工具创建proto文件模板

goctl rpc template -o=hello.proto

编辑hello.proto

syntax = "proto3";

package hello;

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

service hello {
  rpc sayHello(HelloRequest) returns (HelloReply);
}

gotcl生成rpc服务

指定hello.proto生成rpc服务

goctl rpc proto -src hello.proto -dir .

服务目录结构

结构如下:

├── etc
│   └── hello.yaml
├── hello
│   └── hello.pb.go
├── hello.go
├── hello.proto
├── helloclient
│   └── hello.go
└── internal
    ├── config
    │   └── config.go
    ├── logic
    │   └── sayhellologic.go
    ├── server
    │   └── helloserver.go
    └── svc
        └── servicecontext.go

常见问题解决(go mod工程)

  • 错误一:

    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及一下
    • 错误二: ```golang

    go.etcd.io/etcd/clientv3/balancer/picker

    ../../../go/pkg/mod/go.etcd.io/etcd@v0.0.0-20200402134248-51bdeb39e6/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-51bdeb39e6/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) ```

  • 解决方法:

    replace google.golang.org/grpc => google.golang.org/grpc v1.29.1