springboot微服务连接nacos超时

问题现象

java应用启动失败,查看日志,发现是连接Nacos超时,获取不到配置,导致dubbo注册失败,错误日志如下:

java 复制代码
2025-05-01 14:50:08.973 ERROR [TW-172.29.245.61-9063-3] [com.alibaba.nacos.common.utils.LoggerUtils.printIfErrorEnabled:102] c.a.nacos.common.remote.client.grpc.GrpcClient - Server check fail, please check server 172.29.242.158 ,port 9848 is available , error ={}
java.util.concurrent.TimeoutException: Waited 3000 milliseconds (plus 147164 nanoseconds delay) for com.alibaba.nacos.shaded.io.grpc.stub.ClientCalls$GrpcFuture@5fcf7541[status=PENDING, info=[GrpcFuture{clientCall=PendingCall{realCall=ClientCallImpl{method=MethodDescriptor{fullMethodName=Request/request, type=UNARY, idempotent=false, safe=false, sampledToLocalTracing=true, requestMarshaller=com.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller@720ff3a9, responseMarshaller=com.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller@7f335908, schemaDescriptor=com.alibaba.nacos.api.grpc.auto.RequestGrpc$RequestMethodDescriptorSupplier@1b679ea8}}}}]]
        at com.alibaba.nacos.shaded.com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:508)
        at com.alibaba.nacos.common.remote.client.grpc.GrpcClient.serverCheck(GrpcClient.java:218)
        at com.alibaba.nacos.common.remote.client.grpc.GrpcClient.connectToServer(GrpcClient.java:329)
        at com.alibaba.nacos.common.remote.client.RpcClient.start(RpcClient.java:363)
        at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.ensureRpcClient(ClientWorker.java:891)
        at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.getOneRunningClient(ClientWorker.java:1038)
        at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.queryConfig(ClientWorker.java:946)
        at com.alibaba.nacos.client.config.impl.ClientWorker.getServerConfig(ClientWorker.java:403)
        at com.alibaba.nacos.client.config.NacosConfigService.getConfigInner(NacosConfigService.java:184)
--
        at com.tongweb.web.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1642)
        at com.tongweb.web.util.net.SocketProcessorBase.run(SocketProcessorBase.java:33)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at com.tongweb.web.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:47)
        at java.lang.Thread.run(Thread.java:748)

排查思路

1.网络问题

因为连接超时,第一反应就是网络或者端口不通,通过telnet命令排查,发现正常

bash 复制代码
telnet 172.29.242.145 8848   # 测试Http端口
telnet 172.29.242.145 9848   # 测试gRPC端口

2.域名解析

已排除网络和端口,那么可能是连接真的超时了,超时时间是3s。

(1)解析本机ip

(2)发请求到nacos服务端

(3)服务端返回数据

通过hostname -i命令本机ip,发现大概4~5秒才返回,明显有问题,使用hostname查看主机名称,然后通过cat /etc/hosts命令查看host,发现主机名称没有配置host;加上host,重启服务,能正常注册,问题解决。

bash 复制代码
hostname -i     # 查看主机ip
hostname        # 查看主机名称
cat /etc/hosts  # 查看host文件
相关推荐
deng-c-f15 分钟前
Linux C/C++ 学习日记(29):IO密集型与CPU密集型、CPU的调度与线程切换
linux·学习·线程·cpu·io密集·cpu密集
桦说编程2 小时前
深入解析CompletableFuture源码实现(2)———双源输入
java·后端·源码
舒一笑2 小时前
大模型时代的程序员成长悖论:如何在AI辅助下不失去竞争力
后端·程序员·掘金技术征文
lang201509282 小时前
Spring Boot优雅关闭全解析
java·spring boot·后端
二宝1522 小时前
黑马商城day4-微服务02
微服务·云原生·架构
报错小能手3 小时前
linux学习笔记(43)网络编程——HTTPS (补充)
linux·网络·学习
小羊在睡觉3 小时前
golang定时器
开发语言·后端·golang
报错小能手3 小时前
linux学习笔记(45)git详解
linux·笔记·学习
用户21411832636023 小时前
手把手教你在魔搭跑通 DeepSeek-OCR!光学压缩 + MoE 解码,97% 精度还省 10-20 倍 token
后端
追逐时光者3 小时前
一个基于 .NET 开源、功能强大的分布式微服务开发框架
后端·.net