本篇文章为你整理了全网最新的nacos 2.1.0集群多节点部署教程(nacos如何部署集群)的详细内容,包含有nacos多集群模式 nacos如何部署集群 nacos节点列表 nacos集群如何实现高可用的 全网最新的nacos 2.1.0集群多节点部署教程,希望能帮助你了解 全网最新的nacos 2.1.0集群多节点部署教程。
原文链接:全网最新的nacos 2.1.0集群多节点部署教程-语雀
进度整理中
版本 2.1.0
版本发布日期 2022-04-29
git revision number b584531331cc95054964ba4e33984f4cab9e582d
编写日期 2022-07-04~ 2022-07-14
官网 https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
GitHub https://github.com/alibaba/nacos
源码编译环境
服务器 Alibaba Cloud Linux 3.2104 64位
Maven apache-maven-3.8.4
JDK 1.8.0_291编译成功
java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
OpenJDK 11 编译失败
java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
1、配置JAVA_HOME
通过vim /etc/profile编辑profile文件,配置JAVA_HOME,并通过source /etc/profile刷新配置。示例如下:
[root@aliyun ~]# cat /etc/profile
# jdk8
export JAVA_HOME=/data/source/jdk1.8.0_202
# openjdk11
#export JAVA_HOME=/usr/local/jdk-11
export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH
2、配置maven
通过vim ~/.bash_profile编辑.bash_profile文件,配置MAVEN_HOME,并通过source ~/.bash_profile刷新配置。示例如下:
#JAVA_HOME CONFIG IN /etc/profile
#JAVA_HOME=/usr/local/jdk-11
#JAVA_HOME=/data/source/jdk1.8.0_202
#export JAVA_HOME
MAVEN_HOME=/data/app/apache-maven-3.8.4
PATH=$PATH:$HOME/bin:$MAVEN_HOME/bin
export PATH
在/data/app/apache-maven-3.8.4/conf/settings.xml的mirrors节点添加aliyun maven镜像源。
mirror
id alimaven /id
name aliyun maven /name
url http://maven.aliyun.com/nexus/content/groups/public/ /url
mirrorOf central /mirrorOf
/mirror
cd /data/source
# 从git拉取最新代码
git clone https://github.com/alibaba/nacos.git
cd nacos
# 切换到master主分支,并通过git log确认当前commit为b584531331cc95054964ba4e33984f4cab9e582d
git checkout master
# cherry-pick 74c3355b712e74e8274ac038470dc3db411b7c9e,解决nacosAuthConfig jwtTokenManager之间的circular reference
git cherry-pick 74c3355b712e74e8274ac038470dc3db411b7c9e
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U 【推荐】
mvn -Prelease-nacos clean install -U -DskipTests -Drat.skip=true
执行成功时,日志如下:
[INFO] Building tar: /data/source/nacos/distribution/target/nacos-server-2.1.0.tar.gz
[WARNING] Encountered ModuleSet with no sources or binaries specified. Skipping.
[INFO] Building zip: /data/source/nacos/distribution/target/nacos-server-2.1.0.zip
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Alibaba NACOS 2.1.0 2.1.0:
[INFO]
[INFO] Alibaba NACOS 2.1.0 ................................ SUCCESS [ 4.612 s]
[INFO] nacos-api 2.1.0 .................................... SUCCESS [ 12.877 s]
[INFO] nacos-common 2.1.0 ................................. SUCCESS [ 13.351 s]
[INFO] nacos-consistency 2.1.0 ............................ SUCCESS [ 6.605 s]
[INFO] nacos-plugin 2.1.0 ................................. SUCCESS [ 0.228 s]
[INFO] nacos-auth-plugin 2.1.0 ............................ SUCCESS [ 0.979 s]
[INFO] nacos-sys 2.1.0 .................................... SUCCESS [ 2.045 s]
[INFO] nacos-auth 2.1.0 ................................... SUCCESS [ 1.515 s]
[INFO] nacos-core 2.1.0 ................................... SUCCESS [ 13.775 s]
[INFO] nacos-encryption-plugin 2.1.0 ...................... SUCCESS [ 0.533 s]
[INFO] nacos-config 2.1.0 ................................. SUCCESS [ 21.569 s]
[INFO] nacos-cmdb 2.1.0 ................................... SUCCESS [ 0.916 s]
[INFO] nacos-naming 2.1.0 ................................. SUCCESS [ 23.832 s]
[INFO] nacos-address 2.1.0 ................................ SUCCESS [ 0.954 s]
[INFO] nacos-client 2.1.0 ................................. SUCCESS [ 25.395 s]
[INFO] nacos-plugin-default-impl 2.1.0 .................... SUCCESS [ 2.811 s]
[INFO] nacos-istio 2.1.0 .................................. SUCCESS [ 8.047 s]
[INFO] nacos-console 2.1.0 ................................ SUCCESS [ 4.740 s]
[INFO] nacos-test 2.1.0 ................................... SUCCESS [ 0.382 s]
[INFO] nacos-config-test 2.1.0 ............................ SUCCESS [ 0.364 s]
[INFO] nacos-naming-test 2.1.0 ............................ SUCCESS [ 0.340 s]
[INFO] nacos-core-test 2.1.0 .............................. SUCCESS [ 0.433 s]
[INFO] nacos-example 2.1.0 ................................ SUCCESS [ 0.536 s]
[INFO] nacos-distribution 2.1.0 ........................... SUCCESS [ 10.944 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:38 min
[INFO] Finished at: 2022-07-14T22:55:27+08:00
[INFO] ------------------------------------------------------------------------
解压部署的jar包
# copy 需要部署的jar包
cp distribution/target/nacos-server-2.1.0.tar.gz /data/app/nacos-server-2.1.0.tar.gz
cd /data/app
tar -xvzf nacos-server-2.1.0.tar.gz
应用配置文件application.properties
修改DB连接串
将nacos/conf/application.properties的第33~41行DB配置信息根据实际情况修改,并取消注释
31 #*************** Config Module Related Configurations ***************#
32 ### If use MySQL as datasource:
33 spring.datasource.platform=mysql
35 ### Count of DB:
36 db.num=1
38 ### Connect URL of DB:
39 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8 connectTimeout=1000 socketTimeout=3000 autoReconnect=true useUnicode=true useSSL=false serverTimezone=UTC allowPublicKeyRetrieval=true
40 db.user.0=nacos
41 db.password.0=nacos
DB名称: 名称可由服务器所采用的字符集中任意字母、数字、“_”和“ $”组成。如果需要其他字符,需要使用[`]包裹起来,例如db-nacos。已踩坑未验证
db.url:增加allowPublicKeyRetrieval=true。 已踩坑已验证
修改服务端口
可先跳过,按顺序查看【批量服务端口】章节
启动文件startup.sh
根据硬件资源及实际情况,在nacos/bin/startup.sh文件中调整JVM参数
93 #JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
94 JAVA_OPT="${JAVA_OPT} -server -Xms128m -Xmx256m -Xmn64m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m"
JVM参数
Xss:每个线程的stack大小(栈)
Xmx:JAVA HEAP的最大值、默认为物理内存的1/4
Xms:JAVA HEAP的初始值,server端最好Xms与Xmx一样
Xmn:JAVA HEAP young区的大小
XX:MetaspaceSiz:设定内存的永久保存区域
XX:MaxMetaspaceSiz:设定最大内存的永久保存区域
[root@aliyun app]# sh nacos/bin/startup.sh
startup.sh: line 2: $\r: command not found
startup.sh: line 7: $\r: command not found
startup.sh: line 15: $\r: command not found
startup.sh: line 19: syntax error near unexpected token `$in\r
tartup.sh: line 19: `case "`uname`" in
则可以执行sed -i s/\r//g nacos/bin/*全局替换回车符。
原因
脚本是在Windows下编辑完成后上传Linux上执行的,Windows下的换行是回车符+换行符,也就是\r\n,而Linux下是换行符\n。Linux下不识别\r为回车符,所以导致每行的配置都多了个\r,因此是编码的问题。
[root@aliyun app]# sh nacos/bin/startup.sh
/data/source/jdk1.8.0_202/bin/java -Djava.ext.dirs=/data/source/jdk1.8.0_202/jre/lib/ext:/data/source/jdk1.8.0_202/lib/ext -server -Xms128m -Xmx256m -Xmn64m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/app/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Dnacos.member.list= -Xloggc:/data/app/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/data/app/nacos/plugins/health,/data/app/nacos/plugins/cmdb,/data/app/nacos/plugins/selector -Dnacos.home=/data/app/nacos -jar /data/app/nacos/target/nacos-server.jar --spring.config.additional-location=file:/data/app/nacos/conf/ --logging.config=/data/app/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /data/app/nacos/logs/start.out
[root@aliyun app]# tail -f nacos/logs/start.out nacos/logs/nacos.log
== nacos/logs/start.out ==
2022-07-14 23:12:03,213 INFO Nacos is starting...
2022-07-14 23:12:04,215 INFO Nacos is starting...
2022-07-14 23:12:05,229 INFO Nacos is starting...
2022-07-14 23:12:06,238 INFO Nacos is starting...
2022-07-14 23:12:06,358 INFO Nacos started successfully in cluster mode. use external storage
2022-07-14 23:12:06,353 INFO Tomcat started on port(s): 8848 (http) with context path /nacos
2022-07-14 23:12:06,358 INFO Started Nacos in 18.12 seconds (JVM running for 18.93)
2022-07-14 23:12:06,358 INFO Nacos started successfully in cluster mode. use external storage
可以看到Tomcat started on port(s): 8848 (http) with context path /nacos、Nacos started successfully in cluster mode. use external storage等信息,表示启动成功。
访问http://localhost:8848/nacos/,如下图:
以上为单节点,继续修改配置,在单服务器部署集群。
集群Tips
生产环境,必须在多服务器上部署集群,一台服务器上禁止部署多个nacos节点。同时也不用考虑单机节点冲突问题。个人学习因服务器硬件资源有限,选择单服务器部署所有服务。
集群配置文件cluster.conf
cat EOF nacos3/conf/cluster.conf
172.18.155.8:18848
172.18.155.8:28848
172.18.155.8:38848
复制多节点
# 复制节点2:nacos2
cp -r nacos nacos2
# 复制节点3:nacos3
cp -r nacos nacos3
# 将nacos节点移动为节点1:nacos1
mv nacos nacos1
# 修改端口号
sed -i s/8848/18848/g nacos1/conf/application.properties
sed -i s/8848/28848/g nacos2/conf/application.properties
sed -i s/8848/38848/g nacos3/conf/application.properties
raft port: ${server.port} - 1000
grpc port: ${server.port} + 1000
grpc port for server: ${server.port} + 1001
假设对外端口为8848,即总共会有4个端口被占用,分别为7848、8848、9848、9849。所以单服务器部署集群。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。