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

时间:2018-09-22 08:21:16

标签: c# docker microservices grpc

  

我和一个客户做了一个简单的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 Client **

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

2 个答案:

答案 0 :(得分:1)

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

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

这里是一个涉及此问题的堆栈:Cannot connect to Go GRPC server running in local Docker container

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

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撰写示例

import sys
from cx_Freeze import setup, Executable

build_exe_options = {"excludes": ["tkinter"]}
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="Password",
      version="1.0",
      description="Generates a password made of 20 characters",
      options={"build_exe": build_exe_options},
      executables=[Executable("pass.py", base=base)])

这是另一个讨论此问题的堆栈: Interactive shell using Docker Compose

祝你好运!

答案 1 :(得分:0)

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

一旦我将grpc服务器ip添加到NO_PROXY,它便开始工作。

注意:

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

ref:https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md

GrpcEnvironment.SetLogger(new ConsoleLogger());