Nacos无法选举leader
虚机部署3节点nacos集群
参考博客:https://blog.csdn.net/weixin_42789427/article/details/114376654
mysql已配置完成
source /app/nacos/conf/nacos-mysql.sql
启动nacos,start.out无报错
集群状态异常
登录控制台发现所有节点状态均为follower
curl -X GET 'http://192.168.56.101:8848/nacos/v1/ns/raft/state'
{"services":0,"peers":[{"ip":"192.168.56.101:8848","term":0,"leaderDueMs":10115,"heartbeatDueMs":88,"state":"FOLLOWER"},{"ip":"192.168.56.103:8848","term":0,"leaderDueMs":12812,"heartbeatDueMs":1935,"state":"FOLLOWER"},{"ip":"192.168.56.102:8848","term":0,"leaderDueMs":12115,"heartbeatDueMs":176,"state":"FOLLOWER"},{"ip":"10.0.2.15:8848","voteFor":"10.0.2.15:8848","term":70,"leaderDueMs":6728,"heartbeatDueMs":4000,"state":"FOLLOWER"}]}
{“services”:0,“peers”:[{“ip”:“192.168.56.101:8848”,“term”:0,“leaderDueMs”:10115,“heartbeatDueMs”:88,“state”:"FOLLOWER "},{“ip”:“192.168.56.103:8848”,“term”:0,“leaderDueMs”:12812,“heartbeatDueMs”:1935,“state”:“FOLLOWER”},{“ip”:“192.168.56.102:8848”,“term”:0,“leaderDueMs”:12115,“heartbeatDueMs”:176,“state”:“FOLLOWER”},{“ip”:“10.0.2.15:8848”,“voteFor”:“10.0.2.15:8848”,“term”:70,“leaderDueMs”:6728,“heartbeatDueMs”:4000,“state”:“FOLLOWER”}]}
可以看到state全部是follower
服务注册异常
但是发布和获取配置是正常的
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"
true
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
helloWorld
查看日志
nacos.log和start.out均未发现明显报错
查看其余日志发现如下报错
# alipay-jraft.log
2021-03-30 15:06:15,601 WARN Node <naming_persistent_service/10.0.2.15:7848> PreVote to 192.168.56.101:7848 error: Status[ENOENT<1012>: Peer id not found: 192.168.56.101:7848, group: naming_persistent_service].
# protocol-raft.log
ERROR Fail to refresh leader for group : naming_persistent_service, status is : Status[UNKNOWN<-1>: Unknown leader, Unknown leader, Unknown leader, Unknown leader]
# naming-raft.log
org.apache.http.conn.HttpHostConnectException: Connect to 192.168.56.102:8848 [/192.168.56.102] failed: 拒绝连接 (Connection refused)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at com.alibaba.nacos.common.http.client.request.DefaultHttpClientRequest.execute(DefaultHttpClientRequest.java:55)
at com.alibaba.nacos.common.http.client.NacosRestTemplate.execute(NacosRestTemplate.java:482)
at com.alibaba.nacos.common.http.client.NacosRestTemplate.exchange(NacosRestTemplate.java:447)
at com.alibaba.nacos.naming.misc.HttpClient.request(HttpClient.java:116)
at com.alibaba.nacos.naming.misc.HttpClient.httpGet(HttpClient.java:79)
at com.alibaba.nacos.naming.misc.NamingProxy.reqCommon(NamingProxy.java:306)
at com.alibaba.nacos.naming.cluster.ServerListManager$ServerInfoUpdater.run(ServerListManager.java:173)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: 拒绝连接 (Connection refused)
可以看到是拒绝连接,但是防火墙已关,进程存在
telnet测试也是通的
[root@slave1 logs]# telnet 192.168.56.102 8848
Trying 192.168.56.102...
Connected to 192.168.56.102.
Escape character is '^]'.
^CConnection closed by foreign host.
[root@slave1 logs]# telnet 192.168.56.101 8848
Trying 192.168.56.101...
Connected to 192.168.56.101.
Escape character is '^]'.
^CConnection closed by foreign host.
[root@slave1 logs]# telnet 192.168.56.103 8848
Trying 192.168.56.103...
Connected to 192.168.56.103.
Escape character is '^]'.
^CConnection closed by foreign host.
解决方法
破案了,因为我虚拟机是双网卡,一个桥接一个nat。官网提示需要指定ip或者网卡。
然后在application.properties内添加如下内容
重启集群,发现服务注册正常
[root@slave1 bin]# curl -X GET 'http://192.168.56.101:8848/nacos/v1/ns/raft/state'
{"services":0,"peers":[{"ip":"192.168.56.101:8848","voteFor":"192.168.56.102:8848","term":5,"leaderDueMs":5891,"heartbeatDueMs":2000,"state":"FOLLOWER"},{"ip":"192.168.56.103:8848","voteFor":"192.168.56.101:8848","term":2,"leaderDueMs":17986,"heartbeatDueMs":5000,"state":"FOLLOWER"},{"ip":"192.168.56.102:8848","voteFor":"192.168.56.102:8848","term":5,"leaderDueMs":17710,"heartbeatDueMs":5000,"state":"LEADER"},{"ip":"10.0.2.15:8848","term":0,"leaderDueMs":8346,"heartbeatDueMs":299,"state":"FOLLOWER"}]}
[root@slave1 bin]# curl -X PUT 'http://127.0.0.1:8848/ceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok
[root@slave1 bin]# curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/lisserviceName=nacos.naming.serviceName'
{"hosts":[{"ip":"20.18.7.10","port":8080,"valid":true,"healthy":true,"marked":false,"instanceId":"20.18.7.10#8080#DEFAULT#DEFAULT_GROUP@@nacos.naming.serviceName","metadata":{},"enabled":true,"weight":1.0,"clusterName":"DEFAULT","serviceName":"nacos.naming.serviceName","ephemeral":true}],"dom":"nacos.naming.serviceName","name":"DEFAULT_GROUP@@nacos.naming.serviceName","cacheMillis":3000,"lastRefTime":1617260531495,"checksum":"0f579c87395fb94ff691d287b4d67cee","useSpecifiedURL":false,"clusters":"","env":"","metadata":{}}
说实话水平没到位,不是很能理解,cluster.conf已经指定ip了,为什么还要添加这一步?