服务器nacos-docker集群部署(阿里云服务器)
首先写贴上配置文件,与具体的操作,后面再叙述遇到的坑
部署环境
两台阿里云服务器
一个mysql数据库(此处未配置mysql数据库高可用,只是配置了nacos的高可用)
部署的流程
- clone项目
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
- 进入example,新建自己的docker-compose运行镜像的配置
cd example/
vi nacos-config.yaml
version: "2"
services:
nacos:
hostname: hbj-nacos
image: nacos/nacos-server:latest
container_name: nacos
environment:
- MODE=cluster # 集群模式
- NACOS_SERVERS=IP1:PORT1,IP2:PORT2 # IP1,IP2是对应的服务器外网,PORT1,PORT2是对应的nacos服务端口
- PREFER_HOST_MODE=hostname
- NACOS_SERVER_IP=IP1 # 服务器外网
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_MASTER_SERVICE_HOST=MYSQLIP # mysql数据库对应的ip
- MYSQL_MASTER_SERVICE_PORT=3306 # mysql数据库对应的端口
- MYSQL_MASTER_SERVICE_DB_NAME=nacos
- MYSQL_MASTER_SERVICE_USER=root
- MYSQL_MASTER_SERVICE_PASSWORD=password
- MYSQL_DATABASE_NUM=1 # 数据源为1个
volumes:
- /home/nacos-docker/logs:/home/nacos/logs # 日志的挂在
ports:
- "8848:8848"
- "9555:9555"
restart: on-failure
- 执行命令:
cd ..
docker-compose -f example/nacos-config.yaml up -d
- nacos便启动起来了(另一台服务器只需要修改对应的NACOS_SERVER_IP即可)
额外的补充
nacos项目对应的github地址 https://github.com/alibaba/nacos
期间遇到的坑
- 未配置NACOS_SERVER_IP导致找不到集群的节点列表
原因:因为nacos是部署在docker容器中的,它是用的是内网的地址,然而nacos的集群配置是在多台服务器上的,导致服务器之间不能互相访问通,只需要配置上对应的NACOS_SERVER_IP即可
报错样例:
nacos管理页面上访问对应接口:
接口返回的内容:
code:503 msg: server is STARTING now, please try again later!
查看对应的日志:
nacos.log
java.lang.IllegalStateException: unable to find local peer: nacos-hbj:8848, all peers: [IP:8848, IP: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)
2019-11-06 15:30:45,002 ERROR Unexpected error occurred in scheduled task.
naming-raft.log
2019-11-06 15:47:30,000 WARN [IS LEADER] no leader is available now!
可以发现出现的原因是找不到对应的节点