Grpc Golang使用unimplemtedserver与其他嵌入式接口



我最近使用现有的proto 3代码库更新到最新的协议和Go插件,并且在使用新的unimplementtedserver功能时遇到了麻烦。用于Grpc服务器的结构体已经嵌入了另一个接口,该接口描述了由该服务实现的方法。在我的结构中嵌入UnimplementedServer引用后,我从编译器得到一个模糊的错误,它告诉我我不再实现我的服务方法了。我构建代码的方式是否有问题?在libprotoc 3.17.3, protoc-gen-go v1.27.1和protoc-gen-go-grpc 1.1.0中复制的代码如下:api/原型/core_service.proto:


package testbed;
option go_package = "internal/proto";
service Core {
rpc CreateThing(Thing) returns (ThingResponse) {};
}
message Thing {
string name = 1;
}
message ThingResponse {
int64 result = 1;
}

内部/core.go:


import (
"context"
"testbed.org/demo/internal/proto"
)
type CoreApi interface {
CreateThing(ctx context.Context, t *proto.Thing) (*proto.ThingResponse, error)
}

内部/core_endpoints.go:


import (
"context"
"testbed.org/demo/internal/proto"
)
type CoreEndpoints struct {}
func (ce CoreEndpoints) CreateThing(_ context.Context, _ *proto.Thing) (*proto.ThingResponse, error) {
return nil, nil
}

cmd/service.go:


import (
//"context"
. "testbed.org/demo/internal"
"testbed.org/demo/internal/proto"
"google.golang.org/grpc"
)
type MyServer struct {
CoreApi
proto.UnimplementedCoreServer
}
func main() {
mySvr := &MyServer{CoreApi: &CoreEndpoints{}}
//_, _ = mySvr.CoreApi.CreateThing(context.Background(), &proto.Thing{})
grpcSvr := grpc.NewServer()
proto.RegisterCoreServer(grpcSvr, mySvr)
}

构建:

protoc -I api/proto/ api/proto/*.proto --go_out=. --go-grpc_out=.
go build -o bin/svc cmd/service.go
cmd/service.go:19:26: MyServer.CreateThing is ambiguous
cmd/service.go:19:26: cannot use mySvr (type *MyServer) as type "testbed.org/demo/internal/proto".CoreServer in argument to "testbed.org/demo/internal/proto".RegisterCoreServer:
*MyServer does not implement "testbed.org/demo/internal/proto".CoreServer (missing CreateThing method)
gmake: *** [Makefile:8: service] Error 2

日志显示,'Myserver'没有实现coreserver接口。

type MyServer struct {
endpoint CoreEndpoints
proto.UnimplementedCoreServer
}
func (srv MyServer) CreateThing(ctx context.Context, in *proto.Thing)(*proto.ThingResponse, error) {
return srv.endpoint.CreateThing(ctx,in)
}

相关内容

最新更新