gRPC: cannot connect to a Linux service from a Windows client

跟風遠走 提交于 2021-01-29 19:00:14

问题


I'm trying to start using gRPC service in my project. For the beginning I'm using a test project in Visual Studio that is created automatically when you add a gRPC service project. Here's some code.

Client:

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();
    }
}

Service:

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
        });
    }
}

I override the default 5001 port in the appsettings.json:

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

I have no problems launching the project when both client and service are on a Windows machine, here's a screenshot:

The same goes for when they both are on a Linux machine:

But I can't make it work with the client running on Windows and the service on Linux. I don't know much about Linux but it seems to me the problem can be with the fact that I can't access port 55555 from my Windows machine.

I tried testing it with Powershell with this result:

I checked that the service is running and the port is open, here are some commands that I tried and the results I got:

[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: 

I also tried disabling firewalls on both Windows and Linux to check if that might help, but to no avail.

Both machines are in the same network. CentoOS 8 and Windows 10.


回答1:


Found my answer here https://unix.stackexchange.com/questions/578677/unable-to-connect-to-a-linux-port-from-window

Had to change "Url": "https://localhost:55555" in the appsettings.json file to "Url": "https://0.0.0.0:55555"



来源:https://stackoverflow.com/questions/61096014/grpc-cannot-connect-to-a-linux-service-from-a-windows-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!