【Linux】Nacos 问题

启动 jar 连接不上Nacos服务

前置条件

  • Nacos 版本 2.0.3
  • Nacos 服务没问题,后台页面可以打开
  • Nacos所在服务器8848/9848/9849/端口都开放,或者防火墙已经关闭
  • 启动相同的jar有的服务器有问题,有的没问题

启动jar 报错

1
2
3
4
5
6
7
20:39:30.692 [main] INFO  c.a.n.c.r.client - [printIfInfoEnabled,60] - [2d2b5901-f6f8-414e-850c-1a92807e9f25_config-0] Try to connect to server on start up, server: {serverIp = '192.168.x.x', server main port = 8848}
20:39:33.695 [main] ERROR c.a.n.c.r.c.g.GrpcClient - [printIfErrorEnabled,99] - Server check fail, please check server 192.168.166.51 ,port 9848 is available , error ={}
java.util.concurrent.TimeoutException: Waited 3000 milliseconds (plus 65041 nanoseconds delay) for com.alibaba.nacos.shaded.io.grpc.stub.ClientCalls$GrpcFuture@77114efe[status=PENDING, info=[GrpcFuture{clientCall={delegate={delegate=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@77681ce4, responseMarshaller=com.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller@5d96bdf8, schemaDescriptor=com.alibaba.nacos.api.grpc.auto.RequestGrpc$RequestMethodDescriptorSupplier@6f76c2cc}}}}}]]
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:148)
at com.alibaba.nacos.common.remote.client.grpc.GrpcClient.connectToServer(GrpcClient.java:264)
at com.alibaba.nacos.common.remote.client.RpcClient.start(RpcClient.java:390)

原因解析

大概是linux 服务器 执行 hostname -i 反应慢,nacos服务中应该有获取本地ip地方,本质是执行hostname -i ,然后 导致 nacos client执行server 检查的时候超时,这个超时时间源码中是写死的3000毫秒,所以导致服务启动不起来

解决办法

查看连接不了机器的hostnameg跟对应的ip地址

1
2
3
127.0.0.1 localhost host-192-168-166-52
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

现在执行

1
2
[root@host-192-168-166-51 ~]# hostname -i # 立即返回
127.0.0.1

开机自启

1
vim /lib/systemd/system/nacos.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#vim /usr/lib/systemd/system/srs.service

[Unit]
Description=nacos
After=network.target

[Service]
Type=forking
TimeoutSec=0 #防止启动超时
User=root
Group=root

ExecStart=/bin/bash /home/yunwei/gonggonganquan/soft/nacos/bin/startup.sh -m standalone
ExecReload=/home/yunwei/gonggonganquan/soft/nacos/bin/shutdown.sh
ExecStop=/home/yunwei/gonggonganquan/soft/nacos/bin/shutdown.sh
Restart=on-failure
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
3
4
systemctl daemon-reload
systemctl enable nacos.service
systemctl start nacos.service
systemctl status nacos.service