简体   繁体   English

gRPC ruby​​ / python客户端出现错误

[英]Getting error with gRPC ruby / python client

Following is my protobuf defition: 以下是我的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;
}

My Ruby server: 我的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

My Ruby client: 我的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

when I run, I get following error: 运行时,出现以下错误:

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>'

I wrote a Python client too to test and I get an error here too: 我也写了一个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()

And the error: 错误:

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)>

It looks like this is due to the capitalized "Name" field in the proto file, being referred to as lowercase "name" in the code. 看起来这是由于原始文件中的大写“名称”字段所致,在代码中被称为小写“名称”。 The generated proto code's accessor is in the same case as the 生成的原始代码的访问器与

It looks like this is due to the capitalized "Name" field in the proto file, being referred to as lowercase "name" in the code. 看起来这是由于原始文件中的大写“名称”字段所致,在代码中被称为小写“名称”。 The generated proto code's accessor is in the same case as the 生成的原始代码的访问器与

Sorry for the cut sentence in the above comment ^. 对不起,以上评论^中的删节句子。

Meant to say: *The generated proto code's accessor is in the same case as it's defined in the .proto file. 意思是说:*生成的原型代码的访问器与.proto文件中定义的情况相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM