2017-02-04 56 views
0

我正在使用此example來構建go lang grpc服務器。golang + grpc:在GrpcServer上註冊服務

但似乎我失去了一些東西 - 在這個例子中有註冊到GRPC服務器的服務的階段,但我protoc輸出有出口沒有登記方法:

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

是否有一個改變protobuf3文件的編譯?

我在編譯它的方式是錯誤的嗎?

protoc --go_output=. *.proto

我怎樣才能使服務工作,爲服務器,目前還不能:

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) 
    } 
} 
+3

AFAIK對圍棋GRPC您必須使用插件 編譯'protoc --go_out =插件= GRPC :. * .proto' https://github.com/golang/protobuf#grpc-support –

回答

2

難道我編譯它在錯誤的道路?

protoc --go_output=. *.proto

是。如在commentWendy Adi中指出的那樣,protoc的命令行選項應當是--go_out而不是​​3210並且還需要plugins=grpc選項(根據codegen.sh script)。你應該能夠使用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 

greeter_server應編譯正確算賬:

cd ../greeter_server 
go build .