简体   繁体   English

golang + grpc:在 GrpcServer 上注册一个服务

[英]golang + grpc: register a service on GrpcServer

I'm working with this example for building a go lang grpc server.我正在使用这个示例来构建一个 go lang grpc 服务器。

But it seems that I'm missing something - In the example there is a phase of registering a service to the grpc-server but my protoc output has no registration method exported:但似乎我遗漏了一些东西 - 在示例中,有一个向 grpc-server 注册服务的阶段,但我的 protoc 输出没有导出注册方法:

s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})

Was there a change in the compilation of protobuf3 files? protobuf3 文件的编译有变化吗?

Am I'm compiling it in the wrong way?我是否以错误的方式编译它?

protoc --go_output=. *.proto

And how can I make the service work for the server, It is currently not:以及如何使服务为服务器工作,目前不是:

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    //register should go here?!
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to server: %v", err)
    }
}

Am I'm compiling it in the wrong way?我是否以错误的方式编译它?

protoc --go_output=. *.proto

Yes.是的。 As pointed out in the comment by Wendy Adi , the command-line option to protoc should be --go_out not --go_output and the plugins=grpc option is needed as well (as per the codegen.sh script ).正如指出的评论温迪阿迪,命令行选项来protoc--go_out--go_outputplugins=grpc选项需要以及(按照codegen.sh脚本)。 You should be able to use protoc to regenerate the .pb.go file in the helloworld example:你应该能够使用protoc再生.pb.go在HelloWorld示例文件:

cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
mv helloworld.pb.go helloworld.pb.go.orig
protoc --go_out=plugins=grpc:. helloworld.proto

The greeter_server should compile correctly afterwards:之后greeter_server应该可以正确编译:

cd ../greeter_server
go build .

如果使用 grpc 3 使用下面的 cmd

protoc -I=proto --go_out=. --go-grpc_out=. proto/*.proto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM