我正在尝试访问公开 9000 端口并作为 docker 容器运行的 grpc 服务器。
我正在使用 .net 核心客户端连接到服务器。当 GRPC 服务器未作为 docker 容器运行时,它工作得非常好。
我在从客户端创建通道时尝试使用“localhost”、“dockerconatiner-ip”。我在“0.0.0.0”和端口 9000 上运行 grpc 服务器。
docker 命令: docker run -p 9000:9000 -a stdin -a stdout -it
错误:状态(状态代码=不可用,详细信息=“DNS 解析失败”)
码头文件:
FROM mcr.microsoft.com/dotnet/core/runtime:3.0
COPY Sample/bin/Release/netcoreapp3.0/publish/ app/
WORKDIR /app
EXPOSE 9000
ENTRYPOINT ["dotnet", "Sample.dll"]
服务主体:
static void Main(string[] args)
{
try
{
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");
const int Port = 9000;
Server server = new Server
{
Ports = { new ServerPort("0.0.0.0", 9000, ServerCredentials.Insecure) },
Services = { BindService(new TestService()) }
};
server.Start();
Console.WriteLine("starting server");
Console.WriteLine("Press any key to shut down");
Console.ReadKey();
server.ShutdownAsync().Wait();
Console.WriteLine("Grpc Server stopped");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
测试服务有一种将接收到的文本打印到控制台的方法。