Nacos集群部署配置Nginx负载均衡
1|新建nacos文件夹
mkdir nacos 新建文件夹
cd nacos 进入文件夹
2|下载Nacos安装包(前提是云服务器,有网。也可以在windows下载好再上传)
wget https://github.com/alibaba/nacos/releases/download/2.0.4/nacos-server-2.0.4.tar.gz
3|解压
tar -zxvf nacos-server-1.4.1.tar.gz
4|重命名
mv nacos nacos8849
3\4操作三次后
[root@VM-12-13-centos nacos]# ls
nacos8849 nacos8850 nacos8851 nacos-server-1.4.1.tar.gz
5|导入conf\nacos-mysql.sql到mysql,mysql版本最好在5.7+
6|配置conf\application.propertites
cd nacos8849/conf/
vim application.properties
修改的配置项,根据实际修改IP:端口/数据库名,用户名,密码
server.port=8849
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&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=123456
:wq (保存)
7|修改cluster.conf.example重命名为cluster.conf
cp cluster.conf.example cluster.conf
vim cluster.conf
修改IP:端口为
#it is ip
#example
43.142.131.251:8849
43.142.131.251:8850
43.142.131.251:8851
8|修改startup.sh(进入bin文件夹)
vim startup.sh
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then
JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else
if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
fi
JAVA_OPT="${JAVA_OPT} -server -Xms128m -Xmx128m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
主要是修改:-Xms512m -Xmx512m -Xmn256m,根据实际的内存大小来分配即可,我的服务器很小,如果这几个参数都为512m,服务器就卡死了,所以我设置为128m。
启动测试单机是否成功
./startup.sh
如有问题可以查看
nacos is starting,you can check the /nacos/nacos8849/logs/start.out
常见问题有NO source set ----账号密码错误
address is aleady use ---- 端口被占用
端口未开放 服务器开放安全组端口
可参考:https://blog.csdn.net/qq_42259469/article/details/127850289?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168346835916800188591698%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168346835916800188591698&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-10-127850289-null-null.142^v86^insert_down38v5,239^v2^insert_chatgpt&utm_term=Caused%20by%3A%20io.grpc.netty.shaded.io.netty.channel.unix.Errors%24NativeIoException%3A%20bind%28..%29%20failed%3A%20Address%20already%20in%20use&spm=1018.2226.3001.4187
9|复制配置文件到其他结点
[root@VM-12-13-centos nacos]# ll
total 77216
drwxr-xr-x 8 root root 4096 May 7 22:08 nacos8849
drwxr-xr-x 5 root root 4096 May 7 21:17 nacos8850
drwxr-xr-x 5 root root 4096 May 7 21:17 nacos8851
[root@VM-12-13-centos nacos]# cp ./nacos8849/conf/application.properties ./nacos8850/conf/application.properties
cp: overwrite ‘./nacos8850/conf/application.properties’? y
[root@VM-12-13-centos nacos]# cp ./nacos8849/conf/application.properties ./nacos8851/conf/application.properties
cp: overwrite ‘./nacos8851/conf/application.properties’? y
cp ./nacos8849/conf/cluster.conf ./nacos8850/conf/cluster.conf
cp ./nacos8849/conf/cluster.conf ./nacos8851/conf/cluster.conf
[root@VM-12-13-centos nacos]# cp ./nacos8849/bin/startup.sh ./nacos8851/bin/startup.sh
cp: overwrite ‘./nacos8851/bin/startup.sh’? y
You have new mail in /var/spool/mail/root
[root@VM-12-13-centos nacos]# cp ./nacos8849/bin/startup.sh ./nacos8850/bin/startup.sh
cp: overwrite ‘./nacos8850/bin/startup.sh’? y
修改另外两个application.properties文件的端口
vim ./nacos8850/conf/application.properties
server.port = 8850
vim ./nacos8851/conf/application.properties
server.port = 8851
分别启动8850和8851服务
sh ./nacos8850/bin/startup.sh
sh ./nacos8851/bin/startup.sh
查看服务开启情况注意需要开放端口
http://43.142.131.251:8849/nacos/index.html
使用Nginx配置负载均衡
upstream nacoscluster {
server 43.142.131.251:8849;
server 43.142.131.251:8850;
server 43.142.131.251:8851;
}
server
{
listen 8847;
server_name localhost;
location /nacos/ {
proxy_pass http://nacoscluster/nacos/;
}
}
访问地址:http://43.142.131.251:8847/nacos/
刚开始有可能访问不了,是因为所有的集群都没有开启,开一个会报gateWay 502,有的开启比较慢,同样访问不了。所以得有点耐心。