nacos搭建(一)

一,下载nacos镜像
docker pull nacos/nacos-server

二,启动Nacos (单机模式)
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server

standalone代表着单机模式运行,非集群模式

三,访问Nacos
访问地址:http://localhost:8848/nacos

登录密码默认nacos/nacos

四,集群搭建

环境准备
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.按照官方文档修改配置
example/cluster-hostname.yaml
nacos搭建(一)_第1张图片

env/nacos-hostname.env

nacos搭建(一)_第2张图片
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 R u n n a b l e A d a p t e r . c a l l ( E x e c u t o r s . j a v a : 511 ) a t j a v a . u t i l . c o n c u r r e n t . F u t u r e T a s k . r u n ( F u t u r e T a s k . j a v a : 266 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor RunnableAdapter.call(Executors.java:511)atjava.util.concurrent.FutureTask.run(FutureTask.java:266)atjava.util.concurrent.ScheduledThreadPoolExecutorScheduledFutureTask.access 201 ( S c h e d u l e d T h r e a d P o o l E x e c u t o r . j a v a : 180 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r 201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor 201(ScheduledThreadPoolExecutor.java:180)atjava.util.concurrent.ScheduledThreadPoolExecutorScheduledFutureTask.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 R u n n a b l e A d a p t e r . c a l l ( E x e c u t o r s . j a v a : 511 ) a t j a v a . u t i l . c o n c u r r e n t . F u t u r e T a s k . r u n ( F u t u r e T a s k . j a v a : 266 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor RunnableAdapter.call(Executors.java:511)atjava.util.concurrent.FutureTask.run(FutureTask.java:266)atjava.util.concurrent.ScheduledThreadPoolExecutorScheduledFutureTask.access 201 ( S c h e d u l e d T h r e a d P o o l E x e c u t o r . j a v a : 180 ) a t j a v a . u t i l . c o n c u r r e n t . S c h e d u l e d T h r e a d P o o l E x e c u t o r 201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor 201(ScheduledThreadPoolExecutor.java:180)atjava.util.concurrent.ScheduledThreadPoolExecutorScheduledFutureTask.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

集群创建成功了!

你可能感兴趣的:(微服务)