2016-01-31 22 views
2

gRPC在這樣的配置中似乎不起作用。最小不工作例如:在使用gRPC python服務器和ruby客戶端時出現問題

的Protobuf規格:

// a.proto 
syntax = "proto3"; 
message M { string s = 1; } 
service A { rpc Serve(M) returns (M); } 

生成存根

#!/bin/sh 
#codegen.sh 
protoc -I . --ruby_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_ruby_plugin` a.proto 
protoc -I . --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` a.proto 

服務器(如下HelloWorld示例)

#!/usr/bin/python 
    #python_server.py 
    import a_pb2 
    import time 
    from grpc.beta import implementations 
    class AServer(a_pb2.BetaAServicer): 
    def Serve(self, request, context): 
     return a_pb2.M(s = request.s) 
    server = a_pb2.beta_create_A_server(AServer()) 
    server.add_insecure_port("localhost:666123") 
    server.start() 

Python客戶端(正常工作)

#!/usr/bin/python 
    #python_client.py 
    from grpc.beta import implementations 
    import a_pb2 
    channel = implementations.insecure_channel('localhost', 666123) 
    stub = a_pb2.beta_create_A_stub(channel) 
    req = a_pb2.M(s = "test".encode('utf-8')) 
    response = stub.Serve(req, 10) 
    print "got " + response.s 

紅寶石客戶端(似乎忽視服務器)

#!/usr/bin/env ruby 
#ruby_client.rb 
$LOAD_PATH.unshift '.' 
require 'grpc' 
require 'a_services' 
stub = A::Stub.new('localhost:666123', :this_channel_is_insecure) 
req = M.new(s: "test") 
response = stub.serve(req) 
puts("got #{response}") 

Python客戶端輸出 「得到的試驗」 如預期。紅寶石客戶端與異常晶粒

in `check_status': 12:Method "Serve" of service ".A" not implemented! (GRPC::BadStatus) 

版本:gem list輸出google-protobuf (3.0.0.alpha.3)grpc (0.12.0) pip list輸出protobuf (3.0.0a3)grpcio (0.12.0b0)

回答

2

在錯誤消息中的服務「.A」最可能表示這是使用空時的錯誤包名稱在您的.proto中。我爲此提交了an issue

雖然解決方法很簡單;只需在您的原始文件中指定'包'即可:

// a.proto 
syntax = "proto3"; 
package your.package.name; 
message M { string s = 1; } 
service A { rpc Serve(M) returns (M); } 

包名稱通常用於指定防止名稱衝突。