gRPC:无法从Windows客户端连接到Linux服务 [英] gRPC: cannot connect to a Linux service from a Windows client
问题描述
我试图在我的项目中开始使用gRPC服务.首先,我使用的是Visual Studio中的测试项目,该项目在添加gRPC服务项目时会自动创建.这是一些代码.
客户:
class程序{静态异步任务Main(string [] args){HttpClientHandler clientHandler =新的HttpClientHandler{ServerCertificateCustomValidationCallback =(发件人,证书,链,sslPolicyErrors)=>{返回true;}};HttpClient httpClient =新的HttpClient(clientHandler);使用var channel = GrpcChannel.ForAddress("https://localhost:55555",新的GrpcChannelOptions {HttpClient = httpClient});var client = new Greeter.GreeterClient(channel);Console.Write("Name:");字符串名称= Console.ReadLine();var Reply = await client.SayHelloAsync(new HelloRequest {Name = name});Console.WriteLine("Response:" + reply.Message);Console.ReadKey();}}
服务:
公共类程序{公共静态无效的Main(string [] args){CreateHostBuilder(args).Build().Run();}//要在macOS上成功运行gRPC,需要进行其他配置.//有关如何在macOS上配置Kestrel和gRPC客户端的说明,请访问https://go.microsoft.com/fwlink/?linkid=2099682公共静态IHostBuilder CreateHostBuilder(string [] args)=>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup< Startup>();});}公共类GreeterService:Greeter.GreeterBase{私有只读ILogger< GreeterService>_logger;公共GreeterService(ILogger< GreeterService>记录器){_logger =记录器;}公共替代Task< HelloReply>SayHello(HelloRequest请求,ServerCallContext上下文){返回Task.FromResult(新的HelloReply{消息=你好" + request.Name});}}
我覆盖了appsettings.json中的默认5001端口:
{记录":{"LogLevel":{默认":调试","Microsoft":警告","Microsoft.Hosting.Lifetime":信息","Grpc":调试"}},"AllowedHosts":"*",茶est":{终点":{"Http":{网址":"https://localhost:55555"}},"EndpointDefaults":{协议":"Http2"}}}
当客户端和服务都在Windows计算机上时,我没有问题启动项目,这是屏幕截图:
当它们都在Linux机器上时也是如此:
但是我无法使其与Windows上运行的客户端以及Linux上的服务一起使用.我对Linux不太了解,但是在我看来,问题可能出在我无法从Windows计算机访问端口55555.
我尝试使用Powershell对其进行测试,结果如下:
我检查了服务是否正在运行并且端口是否打开,这是我尝试过的一些命令以及得到的结果:
[user @ localhost〜] $ grep -w 55555/etc/services测试55555/tcp#TestService[user @ localhost〜] $ sudo lsof -i -P -n |grep LISTEN用户的[sudo]密码:GrpcServi 10933用户166u IPv4 1090871 0t0 TCP 127.0.0.1:55555(LISTEN)GrpcServi 10933用户167u IPv6 1090874 0t0 TCP [:: 1]:55555(LISTEN)[user @ localhost〜] $ sudo netstat -tulpn |grep LISTENtcp 0 0 127.0.0.1:55555 0.0.0.0:* LISTEN 10933/./GrpcServicetcp6 0 0 :: 1:55555 ::: * LISTEN 10933/./GrpcService[user @ localhost〜] $ sudo netstat -tulpn |grep:55555tcp 0 0 127.0.0.1:55555 0.0.0.0:* LISTEN 10933/./GrpcServicetcp6 0 0 :: 1:55555 ::: * LISTEN 10933/./GrpcService[user @ localhost〜] $ sudo ss -tulpn |grep LISTENtcp LISTEN 0 128 127.0.0.1:55555 0.0.0.0:*用户:((" GrpcService,pid = 10933,fd = 166))tcp LISTEN 0 128 [:: 1]:55555 [::]:*用户:((" 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:*用户:((" GrpcService,pid = 10933,fd = 166))tcp LISTEN 0 128 [:: 1]:55555 [::]:*用户:((" GrpcService,pid = 10933,fd = 167))[user @ localhost〜] $ sudo lsof -i -P -n |grep LISTENGrpcServi 10933用户166u IPv4 1090871 0t0 TCP 127.0.0.1:55555(LISTEN)GrpcServi 10933用户167u IPv6 1090874 0t0 TCP [:: 1]:55555(LISTEN)[user @ localhost〜] $ sudo firewall-cmd --zone = public --list-all用户的[sudo]密码:公开(活动)目标:默认icmp-block-inversion:否接口:enp2s0资料来源:服务:座舱dhcpv6-client ssh端口:3389/tcp 3389/udp 55555/tcp协议:假面舞会:是的前向端口:源端口:icmp-blocks:丰富的规则:
我还尝试在Windows和Linux上都禁用防火墙,以检查是否有帮助,但无济于事.
两台机器都在同一网络中.CentoOS 8和Windows 10.
在这里找到我的答案应用设置中< https://localhost:55555 "/strong>.json文件转换为网址":" https://0.0.0.0:55555 ">
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.
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"
这篇关于gRPC:无法从Windows客户端连接到Linux服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!