Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法连接到Docker中托管的net core gRPC服务_C#_Docker_.net Core_Grpc - Fatal编程技术网

C# 无法连接到Docker中托管的net core gRPC服务

C# 无法连接到Docker中托管的net core gRPC服务,c#,docker,.net-core,grpc,C#,Docker,.net Core,Grpc,我对gRPC服务比较陌生 我正在尝试将Net core gRPC服务部署到Linux docker容器中,并从VS console app本地访问它 我想让事情尽可能简单,因此docker文件与docker compose指向它的VS中的Net core gRPC docker文件相同。在VS中直接运行gRPC服务时,console应用程序可以访问该服务,但不能访问docker容器中的服务 gRPC启动设置 { "profiles": { "Traffi

我对gRPC服务比较陌生

我正在尝试将Net core gRPC服务部署到Linux docker容器中,并从VS console app本地访问它

我想让事情尽可能简单,因此docker文件与docker compose指向它的VS中的Net core gRPC docker文件相同。在VS中直接运行gRPC服务时,console应用程序可以访问该服务,但不能访问docker容器中的服务

gRPC启动设置

{
  "profiles": {
    "TrafficGrpc": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001"
    },
    "Docker": {
      "commandName": "Docker",
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "publishAllPorts": true,
      "useSSL": true
    }
  }
}
gRPC应用程序设置

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }
}
Docker文件

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["gRPC/TrafficGrpc/TrafficGrpc.csproj", "TrafficGrpc/"]
RUN dotnet restore "TrafficGrpc/TrafficGrpc.csproj"
COPY . .
WORKDIR "/src/gRPC/TrafficGrpc"
RUN dotnet build "TrafficGrpc.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TrafficGrpc.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TrafficGrpc.dll"]
Docker编写文件

version: "3.7"
services:

  # Traffic service
  traffic:
    container_name: traffic
    build:
      context: .
      dockerfile: Dockerfile-traffic
    networks:
      grpc_network:
    environment:
      - SERVICE_NAME=1
    expose:
      - "80"
      - "443"
    ports:
      - "0.0.0.0:32773:80"
      - "0.0.0.0:32774:443"
      
networks:
  grpc_network:
控制台应用程序,VS

string trafficUrl = "http://localhost:32773";
//string trafficUrl = "https://localhost:32774";

Traffic traffic = new Traffic
{
    Date = DateTime.Now.AddDays(1),
    Area = Areas[rng.Next(Areas.Length)],
    Condition = Conditions[rng.Next(Conditions.Length)]
};

GrpcChannel tChannel = GrpcChannel.ForAddress(trafficUrl);
TrafficCheckerClient tClient = new TrafficCheckerClient(tChannel);
TrafficConditionResponse tReply = await tClient.CheckTrafficConditionAsync(
    new TrafficConditionRequest { Condition = traffic.Condition }); // <-- ERROR here
string trafficUrl=”http://localhost:32773";
//字符串流量URL=”https://localhost:32774";
交通量=新交通量
{
Date=DateTime.Now.AddDays(1),
面积=面积[rng.Next(面积.长度)],
条件=条件[rng.Next(条件.长度)]
};
GrpcChannel tChannel=GrpcChannel.ForAddress(trafficUrl);
TrafficCheckerClient tClient=新的TrafficCheckerClient(tChannel);
TrafficConditionResponse tReply=等待tClient.CheckTrafficConditionAsync(

new TrafficConditionRequest{Condition=traffic.Condition});// 我今天遇到了熟悉的问题

您在dockerfile中设置了EXPOSE 80,因此需要使grpc侦听绑定到它的端口

所以在我的例子中,grpc侦听8099,并将其暴露到主机端口32812,然后客户端创建到它的通道。而且它起作用了

在dockerfile中:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 8099
EXPOSE 443
在要侦听的服务器端口中:

public static IWebHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureKestrel(options =>
                {
                    options.ListenAnyIP(
                        8099,
                        listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; }
                    );
                })
                .UseStartup<Startup>();


祝你好运~

我今天遇到了一个熟悉的问题

您在dockerfile中设置了EXPOSE 80,因此需要使grpc侦听绑定到它的端口

所以在我的例子中,grpc侦听8099,并将其暴露到主机端口32812,然后客户端创建到它的通道。而且它起作用了

在dockerfile中:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 8099
EXPOSE 443
在要侦听的服务器端口中:

public static IWebHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureKestrel(options =>
                {
                    options.ListenAnyIP(
                        8099,
                        listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; }
                    );
                })
                .UseStartup<Startup>();


祝您好运~

我看到使用了localhost,在本地运行时应该可以工作,但我认为在容器中部署时不会出现这种情况。如果要在容器中的0.0.0.0上公开服务,它将在所有容器接口上公开服务。我只在windows容器中这样做过,但我假设Linux也是如此,我看到使用了localhost,在本地运行时应该可以工作,但我认为在容器中部署时不会出现这种情况。如果要在容器中的0.0.0.0上公开服务,它将在所有容器接口上公开服务。我只在windows容器中这样做过,但我假设Linux也是如此。请将代码作为文本包含在答案中,而不是截屏。请将代码作为文本包含在答案中,而不是截屏
GrpcClientFactory.AllowUnencryptedHttp2 = true;

using var http = GrpcChannel.ForAddress("http://localhost:32812");