运行gRPC Java示例时出现Connection refused错误:localhost/[0:0:0:0:0:0:0:1]:50051
gRPC Java 1.53.0示例客户端连接失败问题排查(Windows 10环境)
问题概述
在Windows 10系统中完成gRPC Java 1.53.0示例项目的gradle installDist构建后,运行客户端程序build\install\examples\bin\hello-world-client时,出现RPC调用失败,报错为Connection refused: no further information: localhost/[0:0:0:0:0:0:0:1]:50051。
环境信息
Microsoft Windows [Version 10.0.19045.2604] java version "19.0.2" 2023-01-17 Java(TM) SE Runtime Environment (build 19.0.2+7-44) Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing) javac 19.0.2 Gradle 8.0.1
完整报错日志
C:\Users\admin\Downloads\grpc-java-1.53.0\examples>build\install\examples\bin\hello-world-client 2023年2月22日 上午11:24:41 io.grpc.examples.helloworld.HelloWorldClient greet 信息: Will try to greet world ... 2023年2月22日 上午11:24:41 io.grpc.examples.helloworld.HelloWorldClient greet 警告: RPC failed: Status{code=UNAVAILABLE, description=io exception, cause=io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/[0:0:0:0:0:0:0:1]:50051 Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:973) at io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337) at io.grpc.netty.shaded.io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:1589) }
解决步骤
- 启动gRPC服务端程序:客户端连接前必须确保服务端已启动。在相同目录下打开新的命令行窗口,运行
build\install\examples\bin\hello-world-server,待服务端输出启动成功信息后,再运行客户端程序。 - 检查端口占用情况:执行命令
netstat -ano | findstr :50051,查看端口50051是否被其他进程占用。若有,可通过任务管理器结束对应进程,或修改示例代码中的端口号后重新构建。 - 强制使用IPv4连接:报错中客户端尝试连接IPv6的localhost(::1),若系统IPv6配置异常,可修改
HelloWorldClient.java中的目标地址为127.0.0.1:50051,执行gradle installDist重新构建后再运行客户端。
内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ
相关产品推荐
相关产品推荐

