Я сделал простой Grpc Greeter Service
вместе с клиентом.Когда служба размещается на локальном хосте, клиент может вызвать RPC и получить ответ.Но когда служба работает внутри док-контейнера на локальном хосте, клиент не может подключиться к службе.
** Сервер службы 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();
}
}
}
** Dockerfile для службы Grpc **
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());
}
}
}
}
** Stacktrace от клиента Grpc **
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