【问题标题】:Getting error with gRPC ruby / python clientgRPC ruby​​ / python客户端出错
【发布时间】:2017-03-26 10:02:22
【问题描述】:

以下是我的 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)>

【问题讨论】:

    标签: python ruby grpc


    【解决方案1】:

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

    的情况相同

    【讨论】:

      【解决方案2】:

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

      对不起,上面评论中的句子被删减了^。

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

      【讨论】:

        猜你喜欢
        • 2020-09-09
        • 2019-03-28
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2017-10-11
        相关资源
        最近更新 更多