【发布时间】:2021-05-12 10:23:49
【问题描述】:
我目前正在尝试获取 gRPC 工作的示例。我使用 C# Asp.NET Core WebApi 作为服务器,并尝试通过 Python 客户端连接到它。
资源
我的原型文件:
syntax = "proto3";
service Example {
rpc Insert (InsertRequest) returns (Empty);
rpc Update (UpdateRequest) returns (Empty);
rpc Delete (DeleteRequest) returns (Empty);
}
message InsertRequest {
int32 Value = 1;
}
message UpdateRequest {
string Id = 1;
int32 Value = 2;
}
message DeleteRequest {
string Id = 1;
}
message Empty { }
Python 客户端:
import grpc
import example_pb2
import example_pb2_grpc
with grpc.insecure_channel('localhost:5001') as channel:
stub = example_pb2_grpc.ExampleStub(channel)
stub.Insert(example_pb2.InsertRequest(Value = 155))
问题
当我尝试运行我的 Python 客户端时,我收到以下错误:
Traceback(最近一次调用最后一次):文件“gRPCExampleClient.py”,行 10、在 stub.Insert(example_pb2.InsertRequest(Value = 155)) 文件“C:\Python38\lib\site-packages\grpc_channel.py”,第 923 行,在 致电 return _end_unary_response_blocking(state, call, False, None) 文件“C:\Python38\lib\site-packages\grpc_channel.py”,第 826 行,在 _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_inactiverpcerror of rpc statuscode.unavailable details="无法连接到所有地址" debug_error_string="{" created subchannel>
到目前为止我尝试了什么
- 为我的 Kestrel 服务器显式启用 HTTP/2
- 已验证服务器正在使用 C# gRPC 客户端
- C# 客户端使用与 Python 客户端相同的 proto 文件(C# 客户端按预期工作,可以与服务器通信)
class Program
{
static void Main(string[] args)
{
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Example.ExampleClient(channel);
client.Insert(new InsertRequest() { Value = 155 });
Console.ReadKey(true);
}
}
- 尝试在我的 Python 客户端 (
with grpc.insecure_channel('https://localhost:5001') as channel:) 中使用“https://”方案。执行此操作时,我收到以下错误消息:
Traceback(最近一次调用最后一次):文件 “gRPCExampleClient.py”,第 10 行,在 stub.Insert(example_pb2.InsertRequest(Value = 155)) 文件“C:\Python38\lib\site-packages\grpc_channel.py”,第 923 行,在 致电 return _end_unary_response_blocking(state, call, False, None) 文件“C:\Python38\lib\site-packages\grpc_channel.py”,第 826 行,在 _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_inactiverpcerror of rpc statuscode.unavailable details="服务的 DNS 解析失败:https://localhost:5001" debug_error_string="{" created https: :>
- 在此之后,我尝试将环境变量
GRPC_DNS_RESOLVER设置为native。遗憾的是,错误保持不变
from os import environ
environ["GRPC_DNS_RESOLVER"] = "native"
- 尝试使用
127.0.0.1、0.0.0.0和https://方案而不是localhost
其他信息
- 根据https://github.com/grpc/grpc-dotnet/issues/959#issuecomment-647935019,当未指定任何方案时,gRPC 会将
dns://作为默认值。我可以通过启用environ["GRPC_TRACE"] = "api,client_channel_routing,cares_resolver"和environ["GRPC_VERBOSITY"] = "debug"来确认这一点- -> 这使得在指定
https时出现的DNS 错误似乎是我应该重点解决的问题,因为dns://可能是可解决的,但首先没有任何意义. - 可悲的是,https://stackoverflow.com/a/62136381/4932122 似乎与这个假设相矛盾
- -> 这使得在指定
我希望我只是遗漏了一些明显的东西,感谢您阅读并考虑提供帮助。
【问题讨论】: