kum*_*mo2 5 c# docker microservices grpc
Grpc Greeter Service我和客户一起做了一个简单的。当服务托管在本地主机上时,客户端能够调用 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
** 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
Run Code Online (Sandbox Code Playgroud)
** 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());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
** 来自 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
Run Code Online (Sandbox Code Playgroud)
(1) 不要在 docker 内的 GRPC 服务器中使用 localhost,它将不起作用,因为无法从 docker 外部访问环回 localhost,您需要监听所有接口,因此请使用“0.0.0.0”,例如。
端口 = {new 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是另一个讨论此问题的堆栈: Interactive shell using Docker Compose
祝你好运!
| 归档时间: |
|
| 查看次数: |
7785 次 |
| 最近记录: |