参考官方文档
将nacos程序包解压到/usr/local/springCloud/nacos/server1/
cp /usr/local/springCloud/nacos/server1/cluster.conf.example /usr/local/springCloud/nacos/server1/cluster.conf
#修改配置文件
vi /usr/local/springCloud/nacos/server1/conf/cluster.conf
配置参考如下
#it is ip
127.0.0.1:8848
127.0.0.1:8849
127.0.0.1:8850
注意:linux环境下,127.0.0.1应该换为实际id否则会出现以下报错
com.alibaba.nacos.api.exception.NacosException: failed to req API:http://127.0.0.1:8848/nacos/v1/ns/instance/beat. code:503 msg: server is DOWN now, please try again later!
创建数据库
CREATE database `nacos` DEFAULT CHARACTER SET utf8mb4;
GRANT ALL PRIVILEGES ON nacos.* TO 'nacos'@'localhost' IDENTIFIED BY '1234.abcd' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON nacos.* TO 'nacos'@'%' IDENTIFIED BY '1234.abcd' WITH GRANT OPTION;
登录nacos用户配置nacos数据库
执行配置文件中的脚本 /usr/local/springCloud/nacos/server1/conf/nacos-mysql.sql
#修改配置文件的数据库连接
vi /usr/local/springCloud/nacos/server1/conf/application.properties
添加下面配置文件内容
## mysql datasource
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos
db.password=1234.abcd
复制成三份服务
cp -r /usr/local/springCloud/nacos/server1/ /usr/local/springCloud/nacos/server2/
cp -r /usr/local/springCloud/nacos/server1/ /usr/local/springCloud/nacos/server3/
#修改端口为8849 8850
vi /usr/local/springCloud/nacos/server2/conf/application.properties
vi /usr/local/springCloud/nacos/server3/conf/application.properties
#启动三个服务
/usr/local/springCloud/nacos/server1/bin/startup.sh
/usr/local/springCloud/nacos/server2/bin/startup.sh
/usr/local/springCloud/nacos/server3/bin/startup.sh
参考下面修改nginx集群配置
#参考下面upstream和server配置
vi /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
upstream nacos {
server 127.0.0.1:8848;
server 127.0.0.1:8849;
server 127.0.0.1:8850;
}
server {
listen 80;
server_name localhost;
location /nacos/ {
proxy_pass http://nacos/nacos/;
}
}
如果启动还是出错
com.alibaba.nacos.api.exception.NacosException: failed to req API:http://127.0.0.1:8848/nacos/v1/ns/instance/beat. code:503 msg: server is DOWN now, please try again later!
此时需要注意:云服务的linux环境下,云服务器一般都会提供外网和内网的 ip,访问外网 ip 时会指向对应的内网 ip 来访问到该服务器,由于 nacos 集群内部是指定的使用网卡ip地址来进行通信,但是由于三台服务器各自的内网 ip 不在同一网段,所以造成无法通信,有时候即使同一个服务器也存在此问题,nacos可以通过配置文件修改ip地址,所以可以按如下配置修改nacos.server.ip参数
#===========================================================================================
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then
JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
#添加参数设置ip,注意把0.0.00替换为主机服务器ip
JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=0.0.0.0"
fi
if [[ "${FUNCTION_MODE}" == "config" ]]; then
JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
fi