gRPC:无法从Windows客户端连接到Linux服务

时间:2020-04-08 08:04:01

标签: c# linux windows centos grpc

我正在尝试在项目中开始使用gRPC服务。 首先,我使用的是Visual Studio中的测试项目,当您添加gRPC服务项目时会自动创建一个测试项目。这是一些代码。

客户:

class Program
{
    static async Task Main(string[] args)
    { 
        HttpClientHandler clientHandler = new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
        };

        HttpClient httpClient = new HttpClient(clientHandler);

        using var channel = GrpcChannel.ForAddress("https://localhost:55555",
            new GrpcChannelOptions { HttpClient = httpClient });

        var client = new Greeter.GreeterClient(channel);
        Console.Write("Name: ");
        string name = Console.ReadLine();

        var reply = await client.SayHelloAsync(new HelloRequest { Name = name });
        Console.WriteLine("Response: " + reply.Message);
        Console.ReadKey();
    }
}

服务:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    // Additional configuration is required to successfully run gRPC on macOS.
    // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)

            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

public class GreeterService : Greeter.GreeterBase
{
    private readonly ILogger<GreeterService> _logger;
    public GreeterService(ILogger<GreeterService> logger)
    {
        _logger = logger;
    }

    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply
        {
            Message = "Hello " + request.Name
        });
    }
}

我覆盖了appsettings.json中的默认5001端口:

{
"Logging": {
    "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "Grpc": "Debug"
    }
},
"AllowedHosts": "*",
"Kestrel": {
    "EndPoints": {
        "Http": {
            "Url": "https://localhost:55555"
        }
    },
    "EndpointDefaults": {
        "Protocols": "Http2"
    }
}  }

当客户端和服务都在Windows计算机上时,我没有问题启动项目,这是屏幕截图:

enter image description here

当它们都在Linux机器上时也是如此:

enter image description here

但是我无法使其与Windows上运行的客户端以及Linux上的服务一起使用。 我对Linux不太了解,但在我看来,问题可能出在我无法从Windows计算机访问端口55555。

我尝试使用Powershell对其进行测试,结果如下:

enter image description here

我检查了服务是否正在运行并且端口是否打开,这是我尝试过的一些命令以及得到的结果:

[user@localhost ~]$ grep -w 55555 /etc/services
test        55555/tcp       # TestService

[user@localhost ~]$ sudo lsof -i -P -n | grep LISTEN
[sudo] password for user: 
GrpcServi 10933         user  166u  IPv4 1090871      0t0  TCP 127.0.0.1:55555 (LISTEN)
GrpcServi 10933         user  167u  IPv6 1090874      0t0  TCP [::1]:55555 (LISTEN)

[user@localhost ~]$ sudo netstat -tulpn | grep LISTEN     
tcp        0      0 127.0.0.1:55555         0.0.0.0:*               LISTEN      10933/./GrpcService 
tcp6       0      0 ::1:55555               :::*                    LISTEN      10933/./GrpcService 

[user@localhost ~]$ sudo netstat -tulpn | grep :55555
tcp        0      0 127.0.0.1:55555         0.0.0.0:*               LISTEN      10933/./GrpcService 
tcp6       0      0 ::1:55555               :::*                    LISTEN      10933/./GrpcService 

[user@localhost ~]$ sudo ss -tulpn | grep LISTEN                                               
tcp   LISTEN   0        128              127.0.0.1:55555          0.0.0.0:*      users: 
(("GrpcService",pid=10933,fd=166))                                                                                   
tcp   LISTEN   0        128                  [::1]:55555             [::]:*      users: 
(("GrpcService",pid=10933,fd=167))                                       

[user@localhost ~]$ sudo ss -tulpn | grep ':55555'
tcp   LISTEN   0        128              127.0.0.1:55555          0.0.0.0:*      users: 
(("GrpcService",pid=10933,fd=166))                                       
tcp   LISTEN   0        128                  [::1]:55555             [::]:*      users: 
(("GrpcService",pid=10933,fd=167))                                       

[user@localhost ~]$ sudo lsof -i -P -n | grep LISTEN
GrpcServi 10933         user  166u  IPv4 1090871      0t0  TCP 127.0.0.1:55555 (LISTEN)
GrpcServi 10933         user  167u  IPv6 1090874      0t0  TCP [::1]:55555 (LISTEN)

[user@localhost ~]$ sudo firewall-cmd --zone=public --list-all
[sudo] password for user: 
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp2s0
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 3389/tcp 3389/udp 55555/tcp 
  protocols: 
  masquerade: yes
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

我还尝试在Windows和Linux上都禁用防火墙,以检查是否有帮助,但无济于事。

两台机器都在同一网络中。 CentoOS 8和Windows 10。

1 个答案:

答案 0 :(得分:0)

在这里https://unix.stackexchange.com/questions/578677/unable-to-connect-to-a-linux-port-from-window找到我的答案

必须将 appsettings .json文件中的“网址”:“ https://localhost:55555”更改为“网址”:“ https://0.0.0.0:55555