【发布时间】:2020-07-20 14:07:59
【问题描述】:
我正在尝试在我的项目中开始使用 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 机器上时,我启动项目没有问题,这是一个屏幕截图:
当它们都在 Linux 机器上时也是如此:
但我无法使其与在 Windows 上运行的客户端和在 Linux 上运行的服务一起工作。 我对 Linux 了解不多,但在我看来,问题可能在于我无法从我的 Windows 机器访问端口 55555。
我尝试使用 Powershell 对其进行测试,结果如下:
我检查了服务正在运行并且端口是打开的,这是我尝试的一些命令和我得到的结果:
[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。
【问题讨论】:
标签: c# linux windows centos grpc