使用gRPC ruby​​ / python客户端获取错误

时间:2017-03-26 10:02:23

标签: python ruby grpc

以下是我的protobuf定义:

syntax = "proto3";
package hello;

service HelloService {
    rpc SayHello(HelloReq) returns (HelloResp) {};
    rpc SayHelloStrict(HelloReq) returns (HelloResp) {};
}

message HelloReq {
    string Name = 1;
}

message HelloResp {
    string Result = 1;
}

我的Ruby服务器:

#!/usr/bin/env ruby

this_dir = File.expand_path(File.dirname(__FILE__))
lib_dir = File.join(this_dir, 'lib')
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)

require 'grpc'
require 'hello_services_pb'

class HelloServer < Hello::HelloService::Service

  def say_hello(hello_req, _unused_call)
    Hello::HelloResp.new(result: "Hey #{hello_req.name}")
  end

  def say_hello_strict(hello_req, _unused_call)
    Hello::HelloResp.new(result: "Hey #{hello_req.name}")
  end

end

def main
  s = GRPC::RpcServer.new
  s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
  s.handle(HelloServer)
  s.run_till_terminated
end

main

我的Ruby客户端:

#!/usr/bin/env ruby

this_dir = File.expand_path(File.dirname(__FILE__))
lib_dir = File.join(this_dir, 'lib')
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)

require 'grpc'
require 'hello_services_pb'

def main
  stub = Hello::HelloService::Stub.new('localhost:50051', :this_channel_is_insecure)
  message = stub.say_hello(Hello::HelloReq.new(name: "Euler")).result
  p "Greeting: #{message}"
end

main

当我跑步时,我收到以下错误:

client.rb:12:in `initialize': Unknown field name 'name' in initialization map entry. (ArgumentError)
  from client.rb:12:in `new'
  from client.rb:12:in `main'
  from client.rb:16:in `<main>'

我也写了一个Python客户端来测试,我也在这里得到一个错误:

import grpc

import hello_pb2
import hello_pb2_grpc


def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = hello_pb2_grpc.HelloServiceStub(channel)
    response = stub.SayHello(hello_pb2.HelloReq(Name='Euler'))
    print(response.Result)


if __name__ == '__main__':
    run()

错误:

Traceback (most recent call last):
  File "client.py", line 38, in <module>
    run()
  File "client.py", line 11, in run
    response = stub.SayHello(hello_pb2.HelloReq(Name='Euler'))
  File "/Users/avi/.virtualenvs/grpc-errors/lib/python3.6/site-packages/grpc/_channel.py", line 507, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "/Users/avi/.virtualenvs/grpc-errors/lib/python3.6/site-packages/grpc/_channel.py", line 455, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNKNOWN, NoMethodError: undefined method `name' for <Hello::HelloReq: Name: "Euler">:Hello::HelloReq)>

2 个答案:

答案 0 :(得分:0)

看起来这是由于proto文件中的大写“Name”字段,在代码中称为小写“name”。生成的原型代码的访问器与

的情况相同

答案 1 :(得分:0)

  

看起来这是由于原型中的大写“名称”字段   文件,在代码中称为小写“名称”。生成的   proto代码的访问者与

的情况相同

对于上述评论^。

中的缩减句子感到抱歉

意思是说:*生成的原型代码的访问者与.proto文件中定义的情况相同。