写在前边
最近在学习Spring Cloud Alibaba的相关内容,使用nacos时先搭了一台单机环境,测试服务注册发现及配置管理时完全ok。但是在搭建nacos集群时遇到一些比较烦的问题,整整花费了一天时间才搭建好。本人觉得nacos的官方文档对环境设置这块描述的不是很详细,这就导致按照文档安装不得不尝试很多次,浪费时间,所以决定写blog记录一下。这是本人第一次写blog,有不正确的地方请不吝赐教,谢谢!
环境准备
1.Centos7.7.1908虚拟机一台,内存32G。
2.docker-compose版本1.25.4,docker版本1.13.1
3.mysql版本5.7.28,拉取并启动mysql镜像,暴露3306端口,执行nacos-mysql.sql。
4.nginx版本1.17.9,拉取并启动nginx镜像,暴露80端口,用来做反向代理。
5.nacos-server版本1.2.0,拉取nacos-server镜像,按照nacos-docker官网下载配置文件模板。
配置修改
1.按照官方文档修改配置
2.cluster-hostname.yml的配置说明
将要启动的容器包含3个,名字分别是nacos1,nacos2,nacos3;
nacos1容器的hostname是nacos1,这里建议不要随意修改,如果有修改需求,nacos-hostname.env里边的NACOS_SERVERS对应节点值也需要跟随修改。nacos1对外暴露8848和9555端口。
nacos2容器的hostname是nacos2,同样不建议修改。nacos2对外暴露8849端口。
nacos3容器的hostname是nacos3,同样不建议修改。nacos3对外暴露8847端口。
3.nacos-hostname.env配置说明
需要注意的就是nacos-hostname.env的配置,一开始我按照自己的理解,NACOS_SERVERS(nacos集群所有节点)应该填写host机器ip+服务端口实现集群,即:
NACOS_SERVERS=172.16.52.108:8847 172.16.52.108:8848 172.16.52.108:8849
但是!这样填写会导致集群间互相访问不通,查看naming-raft.log日志文件报错:
2020-04-08 08:01:12,607 INFO received approve from peer: {"heartbeatDueMs":4500,"ip":"172.17.0.1:8848","leaderDueMs":15819,"state":"FOLLOWER","term":1767,"voteFor":"172.17.0.1:8848"}2020-04-08 08:01:12,608 INFO received approve from peer: {"heartbeatDueMs":4500,"ip":"172.17.0.1:8848","leaderDueMs":15819,"state":"FOLLOWER","term":1767,"voteFor":"172.17.0.1:8848"}2020-04-08 08:01:15,000 WARN [IS LEADER] no leader is available now!2020-04-08 08:01:28,605 INFO leader timeout, start voting,leader: null, term: 1767
查看ncaos.log报错:
java.lang.IllegalStateException: unable to find local peer: nacos1:8848, all peers: [172.16.52.108:8847, 172.16.52.108:8848, 172.16.52.108:8849]
at com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet.local(RaftPeerSet.java:224)
at com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectMetrics(PerformanceLoggerThread.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
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)2020-04-08 09:25:30,001 ERROR Unexpected error occurred in scheduled task.
查阅官方文档和各种博客,最我又将NACOS_SERVERS改成这样:
NACOS_SERVERS=nacos1:8847 nacos2:8848 nacos3:8849
启动,还是报错!查看naming-raft.log日志如下:
2020-04-08 10:05:10,562 INFO leader timeout, start voting,leader: null, term: 69
2020-04-08 10:05:15,000 WARN [IS LEADER] no leader is available now!
2020-04-08 10:05:30,000 WARN [IS LEADER] no leader is available now!
2020-04-08 10:05:30,062 INFO leader timeout, start voting,leader: null, term: 70
查看ncaos.log报错:
2020-04-08 10:06:59,989 INFO Using a shared selector for servlet write/read
2020-04-08 10:07:00,006 ERROR Unexpected error occurred in scheduled task.java.lang.IllegalStateException: unable to find local peer: nacos1:0, all peers: [nacos2:8848, nacos1:8848, nacos3:8848]
at com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet.local(RaftPeerSet.java:224)
at com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectMetrics(PerformanceLoggerThread.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
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)
2020-04-08 10:07:00,028 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
2020-04-08 10:07:00,033 INFO Started Nacos in 9.407 seconds (JVM running for 10.228)
根据上述报错信息,最终将NACOS_SERVERS改成这样:
NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
集群创建成功了!
问题:这里填写hostname可以理解,为什么不能填写hostname对应暴露出去的端口呢?
参考如下资料:
补充:
nginx的配置文件: