繁体   English   中英

c#Grpc Client无法连接到本地主机的docker(for Windows)容器中托管的Grpc Server

[英]c# Grpc Client Not able to connect to Grpc Server hosted in docker(for windows) container on localhost

我与客户一起做了一个简单的Grpc Greeter Service 当该服务托管在localhost上时,客户端可以调用rpc并接收响应。 但是,当服务在本地主机上的docker容器内运行时,客户端将无法连接到服务。

** Grpc服务服务器**

namespace GreeterServer
{
    class Program
    {
        private readonly static ManualResetEvent shutdown = new ManualResetEvent(false);
        static void Main(string[] args)
        {
            int port = 5000;
            Console.WriteLine("Hello World!");
            Server server = new Server{
                Services = {GreeterService.BindService(new GreeterController())},
                Ports = {new ServerPort("localhost", port, ServerCredentials.Insecure)}
            };
            server.Start();
            Console.WriteLine("Grpc Server started");
            Console.Read();
            shutdown.WaitOne();
        }
    }
}

**用于Grpc服务的Dockerfile **

FROM microsoft/dotnet:2.1-sdk as base
WORKDIR /app

COPY *.csproj .
RUN dotnet restore

COPY . .
RUN dotnet build -c Release -o out

FROM microsoft/dotnet:2.1-runtime
WORKDIR /app
COPY --from=base /app/out .
ENTRYPOINT ["dotnet", "GreeterServer.dll"]
EXPOSE 5000

** Grpc客户**

namespace GreeterClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Hello World!");
                Channel channel = new Channel("127.0.0.1:5000", ChannelCredentials.Insecure);
                GreeterService.GreeterServiceClient client = new GreeterService.GreeterServiceClient(channel);
                HelloRequest request = new HelloRequest
                {
                    Message = "Hi From Client"
                };
                HelloResponse response = client.SayHello(request);
                Console.WriteLine(response.Message);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }
    }
}

**来自Grpc客户端的Stacktrace **

Grpc.Core.RpcException: Status(StatusCode=Unknown, Detail="Stream removed")
   at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg) in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\AsyncCall.cs:line 75
   at Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core\DefaultCallInvoker.cs:line 46
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) in T:\src\github\grpc\src\csharp\Grpc.Core\Interceptors\InterceptingCallInvoker.cs:line 51
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) in T:\src\github\grpc\src\csharp\Grpc.Core\ClientBase.cs:line 174
   at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core\Interceptors\InterceptingCallInvoker.cs:line 48
   at Greeter.Proto.GreeterService.GreeterServiceClient.SayHello(HelloRequest request, CallOptions options) in F:\c#\GrpcPracticeWithDocker\GreeterClient\GrpcClasses\GreeterGrpc.cs:line 70
   at Greeter.Proto.GreeterService.GreeterServiceClient.SayHello(HelloRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in F:\c#\GrpcPracticeWithDocker\GreeterClient\GrpcClasses\GreeterGrpc.cs:line 66
   at GreeterClient.Program.Main(String[] args) in F:\c#\GrpcPracticeWithDocker\GreeterClient\Program.cs:line 23

(1)不要在docker内部的GRPC服务器中使用localhost,因为无法从docker外部访问回送localhost,所以它将不起作用,您需要侦听所有接口,因此请使用“ 0.0.0.0”,例如。

端口= {新的ServerPort(“ 0.0.0.0”,端口,ServerCredentials.Insecure)}

以下是涉及此问题的堆栈: 无法连接到在本地Docker容器中运行的Go GRPC服务器

(2)我有同样的问题。 我发现问题是在使用docker run或docker compose时我没有使用交互式shell。 如果使用docker run,则需要使用-it命令,对于docker compose,则需要使用stdin_open:true和tty:true。

Docker运行示例

docker run --name grpcserver -p 8081:8081 -it -d grpcserver

Docker撰写示例

version: '3'

services:
  web:
    container_name: mynginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 8080:80
  grpcserver:
    image: grpcserver
    container_name: grpcserver
    stdin_open: true
    tty: true
    build:
      context: .
      dockerfile: Dockerfile

这是讨论此问题的另一个堆栈: 使用Docker Compose的交互式shell

祝好运!

我遇到了同样的问题。 但最后它与代理配置有关。 GRPC使用“ HTTP_PROXY”和“ HTTPS_PROXY”环境配置。 不幸的是,我的grpc服务器在不需要代理的本地网络上运行。 我错过了定义“ NO_PROXY”环境变量的过程。

将grpc服务器ip添加到NO_PROXY后,它开始工作。

注意:

  1. 我能够让docker仅使用“ localhost”工作。 我尚未将grpc服务器限制为“ 0.0.0.0”
  2. 我通过启用GRPC跟踪日志记录来诊断问题。 启用跟踪之后,您必须使用以下代码将日志显示到控制台中。

参考: https : //github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md

GrpcEnvironment.SetLogger(new ConsoleLogger());

暂无
暂无

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

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