【问题标题】:Troubles in using gRPC python server with ruby client将 gRPC python 服务器与 ruby​​ 客户端一起使用的问题
【发布时间】:2016-01-31 13:13:52
【问题描述】:

gRPC 在这样的配置中似乎不起作用。最小的不工作示例:

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

Ruby 客户端(似乎忽略了服务器)

#!/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 客户端按预期输出“得到测试”。 Ruby 客户端异常死亡

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)

【问题讨论】:

    标签: python ruby grpc


    【解决方案1】:

    错误消息中的服务“.A”很可能意味着这是在您的 .proto 中使用空包名称时的错误。我为此提交了an issue

    不过,解决方法很简单;只需在您的 proto 文件中指定“包”:

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

    通常,指定包名称可以防止名称冲突。

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 2021-04-10
      • 2014-01-03
      • 1970-01-01
      • 2020-09-09
      • 2020-10-18
      • 2011-06-06
      • 1970-01-01
      相关资源
      最近更新 更多