golang + grpc:在GrpcServer上注册服务

I'm working with this example for building a go lang grpc server.

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:

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

Was there a change in the compilation of protobuf3 files?

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)
    }
}
dongyi9298
dongyi9298 afaikforGogrpc必须使用插件协议--go_out=plugins=grpc:进行编译。*.protogithub.com/golang/protobuf#grpc-support
3 年多之前 回复

1个回答




我是用错误的方式编译它的吗?</ p>

< 代码>协议--go_output =。 * .proto </代码> </ p>
</ BLOCKQUOTE>

是。 正如评论中指出的那样,Wendy Adi protoc </ code>的命令行选项应为-go_out </ code> 不需要-go_output </ code>,也需要 plugins = grpc </ code>选项(根据 codegen.sh </ code>脚本)。 在helloworld示例中,您应该能够使用 protoc </ code>重新生成 .pb.go </ code>文件:</ p>

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

greeter_server </ code>之后应正确编译:</ p>

  cd。  ./greeter_server
go构建。
</ code> </ pre>
</ div>

展开原文

原文

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). You should be able to use protoc to regenerate the .pb.go file in the helloworld example:

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:

cd ../greeter_server
go build .

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问