官方文档
官网架构图
集群部署架构图
因此开源的时候推荐用户把所有服务列表放到一个vip下面,然后挂到一个域名下面
http://ip1:port/openAPI 直连ip模式,机器挂则需要修改ip才可以使用。
http://VIP:port/openAPI 挂载VIP模式,直连vip即可,下面挂server真实ip,可读性不好。
http://nacos.com:port/open API域名+VIP模式,可读性好,而且换ip方便,推荐模式
上图官网翻译,真实情况
按照上述,我们需要mysql数据库。
官网说明
默认Nacos使用嵌入式数据库实现数据的存储。所以,如果启动多个默认配置下的Nacos节点,数据存储是存在一致性问题的。为了解决这个问题,Nacos采用了集中式存储的方式来支持集群化部署,目前只支持MySQL的存储。
Nacos支持三种部署模式
Windows
cmd startup.cmd或者双击startup.cmd文件
单机模式支持mysql
在0.7版本之前,在单机模式时nacos使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源能力,具体的操作步骤:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos_devtest
db.password=youdontknow
再以单机模式启动nacos,nacos所有写嵌入式数据库的数据都写到了mysql。
Nacos默认自带的是嵌入式数据库derby,nacos的pom.xml中可以看出。
derby到mysql切换配置步骤:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=1234
启动Nacos,可以看到是个全新的空记录界面,以前是记录进derby。
预计需要,1个Nginx+3个nacos注册中心+1个mysql
请确保是在环境中安装使用:
- 64 bit OS Linux/Unix/Mac,推荐使用Linux系统。
- 64 bit JDK 1.8+;下载.配置。
- Maven 3.2.x+;下载.配置。
- 3个或3个以上Nacos节点才能构成集群。
link
Nacos下载Linux版
集群配置步骤(重点)
1.Linux服务器上mysql数据库配置
新建数据库:nacos
SQL脚本在哪里 - 目录nacos/conf/nacos-mysql.sql
自己Linux机器上的Mysql数据库上运行
2.application.properties配置
位置
添加以下内容,设置数据源
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456
3.Linux服务器上nacos的集群配置cluster.conf
梳理出3台nacos集器的不同服务端口号,设置3个端口:
复制出cluster.conf
内容
192.168.111.144:3333
192.168.111.144:4444
192.168.111.144:5555
注意,这个IP不能写127.0.0.1,必须是Linux命令hostname -i
能够识别的IP
4.编辑Nacos的启动脚本startup.sh,使它能够接受不同的启动端口
/mynacos/nacos/bin目录下有startup.sh
平时单机版的启动,都是./startup.sh即可
但是,集群启动,我们希望可以类似其它软件的shell命令,传递不同的端口号启动不同的nacos实例。
命令: ./startup.sh -p 3333表示启动端口号为3333的nacos服务器实例,和上一步的cluster.conf配置的一致。
修改内容
while getopts ":m:f:s:p:" opt
do
case $opt in
m)
MODE=$OPTARG;;
f)
FUNCTION_MODE=$OPTARG;;
s)
SERVER=$OPTARG;;
p)
PORT=$OPTARG;;
?)
echo "Unknown parameter"
exit 1;;
esac
done
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
执行方式 - startup.sh - p 端口号
5.Nginx的配置,由它作为负载均衡器
修改nginx的配置文件 - nginx.conf
修改内容
upstream cluster{
server 127.0.0.1:3333;
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
server {
listen 1111;
server_name nacos;
location / {
proxy_pass http://cluster;
}
}
按照指定启动
启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6.截止到此处,1个Nginx+3个nacos注册中心+1个mysql
测试
启动3个nacos注册中心
startup.sh - p 3333
startup.sh - p 4444
startup.sh - p 5555
ps -ef | grep nacos | grep -v grep | wc -l
启动nginx
./nginx -c /usr/local/nginx/conf/nginx.conf
ps - ef| grep nginx
测试通过nginx,访问nacos - http://192.168.111.144:1111/nacos/#/login
新建一个配置测试
select * from config;
server:
port: 9002
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
#配置Nacos地址
#server-addr: Localhost:8848
#换成nginx的1111端口,做集群
server-addr: 192.168.111.144:1111
management:
endpoints:
web:
exposure:
inc1ude: '*'
高可用小总结
nacos1.4.x 版本之后,使用集群启动出现以下问题:
2022-09-02 11:24:10,363 INFO Nacos-related cluster resource initialization
2022-09-02 11:24:10,385 INFO The cluster resource is initialized
2022-09-02 11:24:10,830 INFO Nacos is starting...
2022-09-02 11:24:11,834 INFO Nacos is starting...
2022-09-02 11:24:11,894 ERROR Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'trafficReviseFilterRegistration' defined in class path resource [com/alibaba/nacos/naming/web/NamingConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'trafficReviseFilterRegistration' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'trafficReviseFilter': Unsatisfied dependency expressed through field 'serverStatusManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverStatusManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'consistencyDelegate' defined in URL [jar:file:/apps/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-1.4.2.jar!/com/alibaba/nacos/naming/consistency/DelegateConsistencyServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistentConsistencyServiceDelegate' defined in URL [jar:file:/apps/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-1.4.2.jar!/com/alibaba/nacos/naming/consistency/persistent/PersistentConsistencyServiceDelegateImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyServiceDelegateImpl]: Constructor threw exception; nested exception is java.lang.UnsatisfiedLinkError: /tmp/librocksdbjni5938770652802224675.so: libstdc++.so.6: 无法打开共享对象文件: 没有那个文件或目录
...
...
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyServiceDelegateImpl]: Constructor threw exception; nested exception is java.lang.UnsatisfiedLinkError: /tmp/librocksdbjni5938770652802224675.so: libstdc++.so.6: 无法打开共享对象文件: 没有那个文件或目录
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:187)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300)
... 126 common frames omitted
Caused by: java.lang.UnsatisfiedLinkError: /tmp/librocksdbjni5938770652802224675.so: libstdc++.so.6: 无法打开共享对象文件: 没有那个文件或目录
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at org.rocksdb.NativeLibraryLoader.loadLibraryFromJar(NativeLibraryLoader.java:78)
at org.rocksdb.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:56)
at org.rocksdb.RocksDB.loadLibrary(RocksDB.java:64)
at org.rocksdb.RocksDB.(RocksDB.java:35)
at com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage.(RocksDBLogStorage.java:75)
at com.alipay.sofa.jraft.core.DefaultJRaftServiceFactory.createLogStorage(DefaultJRaftServiceFactory.java:50)
at com.alipay.sofa.jraft.core.NodeImpl.initLogStorage(NodeImpl.java:571)
at com.alipay.sofa.jraft.core.NodeImpl.init(NodeImpl.java:991)
at com.alipay.sofa.jraft.core.NodeImpl.init(NodeImpl.java:138)
at com.alipay.sofa.jraft.RaftServiceFactory.createAndInitRaftNode(RaftServiceFactory.java:47)
at com.alipay.sofa.jraft.RaftGroupService.start(RaftGroupService.java:129)
at com.alibaba.nacos.core.distributed.raft.JRaftServer.createMultiRaftGroup(JRaftServer.java:268)
at com.alibaba.nacos.core.distributed.raft.JRaftProtocol.addRequestProcessors(JRaftProtocol.java:163)
at com.alibaba.nacos.naming.consistency.persistent.impl.PersistentServiceProcessor.afterConstruct(PersistentServiceProcessor.java:79)
at com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyServiceDelegateImpl.createNewPersistentServiceProcessor(PersistentConsistencyServiceDelegateImpl.java:108)
at com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyServiceDelegateImpl.(PersistentConsistencyServiceDelegateImpl.java:54)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:175)
nacos集群启动报这个错误的主要问题是:
Caused by: java.lang.UnsatisfiedLinkError: /tmp/librocksdbjni5938770652802224675.so: libstdc++.so.6: 无法打开共享对象文件: 没有那个文件或目录
究其原因就是在64位系统安装32位软件,需要有32位库。
解决方案:
先更新一下64位的库:
yum update libstdc++-4.8.5-36.el7_6.2.x86_64
然后再安装:libstdc++.i686
yum install libstdc++.i686
安装完成后 nacos1.4.2版本成功集群启动: