将 localhost 更改为服务器计算机名称或 IP 时,gRPC 客户端调用失败
gRPC client call fails when changing localhost to the server computer name or IP
我有一个 gRPC 客户端调用同一台机器上的服务器,客户端代码使用通过以下方式创建的通道:
Channel channel = new Channel("localhost", 11112, ChannelCredentials.Insecure);
效果很好,我收到了预期的回复。
但是,如果我将 "localhost" 替换为实际的机器名称 (FQDN) 或 IP 地址,则会出现错误:
Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1616190440.553000000","description":"Failed to pick subchannel","file":"..\..\..\src\core\ext\filters\client_channel\client_channel.cc","file_line":5397,"referenced_errors":[{"created":"@1616190440.553000000","description":"failed to connect to all addresses","file":"..\..\..\src\core\ext\filters\client_channel\lb_policy\pick_first\pick_first.cc","file_line":398,"grpc_status":14}]}")
我还测试了在远程机器上启动服务器并让客户端在创建通道时调用该远程机器(通过名称或 IP),但遇到了同样的失败。
该应用程序是用 C# .NET Core 3.2 编写的,作为控制台应用程序。包含的 Nuget 包有:Google.Protobuf、Grpc.Core、Grpc.Tools.
不确定我的代码有什么问题,或者我的机器上是否设置不正确。
这里是相关客户端代码的重置:
MWCS.McpsServer.McpsServerClient client = new MWCS.McpsServer.McpsServerClient(channel);
var data = new ProcessResponseInput() { ClientID = clientID };
var options = new CallOptions();
try {
var res = client.ProcessResponse(data, options);
return res;
} catch (RpcException ex) {
StandardOutput so = new StandardOutput() { ExitCode = 1, Message = ex.Message };
return so;
}
为了解决这个问题,我实际上不得不修复服务器代码。我应该在服务器上的绑定地址中使用 "0.0.0.0"
而不是 "localhost"
。看起来当将服务器设置为 localhost 时,它在计算机外是不可见的。
这是我现在启动服务器的方式(替换后):
_server = new Server() {
Services = { McpsServer.BindService(new McpcServerImplementation()) },
Ports = { new ServerPort("0.0.0.0", 11112, ServerCredentials.Insecure) }
};
我有一个 gRPC 客户端调用同一台机器上的服务器,客户端代码使用通过以下方式创建的通道:
Channel channel = new Channel("localhost", 11112, ChannelCredentials.Insecure);
效果很好,我收到了预期的回复。
但是,如果我将 "localhost" 替换为实际的机器名称 (FQDN) 或 IP 地址,则会出现错误:
Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1616190440.553000000","description":"Failed to pick subchannel","file":"..\..\..\src\core\ext\filters\client_channel\client_channel.cc","file_line":5397,"referenced_errors":[{"created":"@1616190440.553000000","description":"failed to connect to all addresses","file":"..\..\..\src\core\ext\filters\client_channel\lb_policy\pick_first\pick_first.cc","file_line":398,"grpc_status":14}]}")
我还测试了在远程机器上启动服务器并让客户端在创建通道时调用该远程机器(通过名称或 IP),但遇到了同样的失败。 该应用程序是用 C# .NET Core 3.2 编写的,作为控制台应用程序。包含的 Nuget 包有:Google.Protobuf、Grpc.Core、Grpc.Tools.
不确定我的代码有什么问题,或者我的机器上是否设置不正确。
这里是相关客户端代码的重置:
MWCS.McpsServer.McpsServerClient client = new MWCS.McpsServer.McpsServerClient(channel);
var data = new ProcessResponseInput() { ClientID = clientID };
var options = new CallOptions();
try {
var res = client.ProcessResponse(data, options);
return res;
} catch (RpcException ex) {
StandardOutput so = new StandardOutput() { ExitCode = 1, Message = ex.Message };
return so;
}
为了解决这个问题,我实际上不得不修复服务器代码。我应该在服务器上的绑定地址中使用 "0.0.0.0"
而不是 "localhost"
。看起来当将服务器设置为 localhost 时,它在计算机外是不可见的。
这是我现在启动服务器的方式(替换后):
_server = new Server() {
Services = { McpsServer.BindService(new McpcServerImplementation()) },
Ports = { new ServerPort("0.0.0.0", 11112, ServerCredentials.Insecure) }
};