Nacos 2.x 集群与 Nginx 配置
下载 Nacos 安装包
从 GitHub 下载最新的 Nacos 安装包。截至 2022-11-04,最新稳定版本为 2.1.2。
修改配置
解压安装包后,进入 conf 文件夹。
创建 MySQL 表
使用 mysql-schema.sql 创建所需的数据库表。
修改 application.properties
server.port=8848 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://xxxx.mysql.rds.aliyuncs.com:43790/nacos_cluster?serverTimezone=Asia/Shanghai&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&allowPublicKeyRetrieval=true db.user.0=xxxxxx db.password.0=nxxx server.tomcat.accesslog.enabled=false nacos.core.auth.enabled=true
修改 cluster.conf
172.31.237.101:8848 172.31.237.102:8848 172.31.237.103:8848 172.31.237.104:8848
配置 Java 环境
请参考相关文章进行 Java 环境配置。
编写启动脚本
创建并编辑 start_nacos.sh 脚本:
#!/bin/bash /root/nacos/bin/shutdown.sh /root/nacos/bin/startup.sh
添加可执行权限:
chmod +x start_nacos.sh
启动测试
运行启动脚本:
./start_nacos.sh
查看启动日志:
cat /root/nacos/logs/start.out
访问 http://ip:8848/nacos/,默认用户名和密码均为 nacos。
Java 代码配置
spring:
cloud:
nacos:
username: nacos
password: nacos_密码
复制 Nacos 文件夹到其他服务器并启动
scp -r nacos root@ip.ip.ip.ip:~
配置 Nginx
Nacos 2.x 需要配置 TCP 和 HTTP。
安装带 Stream 模块的 Nginx
下载并解压 Nginx 源码包(例如 nginx-1.23.2),然后编译并安装:
./configure --with-http_v2_module --with-stream --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-poll_module --with-http_realip_module --with-stream_ssl_module make && make install
配置 Nginx
user root;
worker_processes 4;
error_log /dev/null;
events {
use epoll;
multi_accept on;
accept_mutex on;
worker_connections 65535;
}
stream {
upstream nacosGrpc {
server 172.31.237.100:9848;
server 172.31.237.101:9848;
server 172.31.237.102:9848;
server 172.31.237.103:9848;
}
server {
listen 1080;
proxy_pass nacosGrpc;
}
}
http {
access_log off;
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
gzip on;
server_tokens off;
upstream nacos-cluster{
server 172.31.237.100:8848;
server 172.31.237.101:8848;
server 172.31.237.102:8848;
server 172.31.237.103:8848;
keepalive 120;
}
server{
listen 80;
server_name yalong.site;
location / {
proxy_pass http://nacos-cluster;
}
}
}
命令
/usr/local/nginx/sbin/nginx # 启动 Nginx /usr/local/nginx/sbin/nginx -s reload # 重新加载配置文件
Java 客户端
确保选择与 Nacos 2.x 兼容的客户端版本。
