使用docker-compose部署nacos集群并通过nginx实现负载均衡
单机部署nacos+mysql
nacos_mysql.yml
version: "3"
services:
nacos:
image: nacos/nacos-server:latest
container_name: nacos-standalone-mysql
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306 #mysql数据库对应的端口
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
- MYSQL_DATABASE_NUM=1 #数据源为1个
- JVM_XMS=128m
- JVM_XMX=128m
- JVM_XMN=128m
volumes:
- /data/nacos/standalone-logs/:/home/nacos/logs
- /data/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
depends_on:
- mysql
restart: always
mysql:
container_name: mysql
image: nacos/nacos-mysql:5.7 #用mysql8改下镜像就好nacos/nacos-mysql:8.0.16
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nacos_devtest
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos
volumes:
- /data/mysql:/var/lib/mysql
ports:
- "3306:3306"
restart: always
多台服务器通过ip部署集群
cluster_nacos1.yml
version: "3"
services:
nacos:
image: nacos/nacos-server:latest
container_name: nacos-standalone-mysql
environment:
- MODE=cluster # 集群模式
- NACOS_SERVERS=IP1:8848 IP2:8848 IP3:8848 # IP1,IP2是对应的服务器外网,PORT1,PORT2是对应的nacos服务端口
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=xxx.xxx.xx.xx# mysql数据库对应的ip
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306 #mysql数据库对应的端口
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
- MYSQL_DATABASE_NUM=1 #数据源为1个
- JVM_XMS=128m
- JVM_XMX=128m
- JVM_XMN=128m
volumes:
- /data/nacos/standalone-logs/:/home/nacos/logs
- /data/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
restart: always
cluster_nacos2.yml
version: "3"
services:
nacos:
image: nacos/nacos-server:latest
container_name: nacos-standalone-mysql
environment:
- MODE=cluster # 集群模式
- NACOS_SERVERS=IP1:8848 IP2:8848 IP3:8848 # IP1,IP2是对应的服务器外网,PORT1,PORT2是对应的nacos服务端口
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=xxx.xxx.xx.xx# mysql数据库对应的ip
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306 #mysql数据库对应的端口
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
- MYSQL_DATABASE_NUM=1 #数据源为1个
- JVM_XMS=128m
- JVM_XMX=128m
- JVM_XMN=128m
volumes:
- /data/nacos/standalone-logs/:/home/nacos/logs
- /data/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
restart: always
cluster_nacos3.yml
version: "3"
services:
nacos:
image: nacos/nacos-server:latest
container_name: nacos-standalone-mysql
environment:
- MODE=cluster # 集群模式
- NACOS_SERVERS=IP1:8848 IP2:8848 IP3:8848 # IP1,IP2是对应的服务器外网,PORT1,PORT2是对应的nacos服务端口
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=xxx.xxx.xx.xx# mysql数据库对应的ip
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306 #mysql数据库对应的端口
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
- MYSQL_DATABASE_NUM=1 #数据源为1个
- JVM_XMS=128m
- JVM_XMX=128m
- JVM_XMN=128m
volumes:
- /data/nacos/standalone-logs/:/home/nacos/logs
- /data/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
restart: always
mysql
nacos_mysql.yml
version: "3"
services:
mysql:
container_name: mysql
image: nacos/nacos-mysql:5.7 #用mysql8改下镜像就好nacos/nacos-mysql:8.0.16
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nacos_devtest
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos
volumes:
- /data/mysql:/var/lib/mysql
ports:
- "3306:3306"
restart: always
nginx配置
nacos_nginx.yaml
version: "3"
services:
nginx:
restart: always
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- /etc/localtime:/etc/localtime:ro
- /opt/nginx/conf.d/:/etc/nginx/conf.d/
- /opt/nginx/nginx.conf:/etc/nginx/nginx.conf
- /opt/nginx/logs:/var/log/nginx/
- /opt/nginx/html:/var/www/html
- /opt/nginx/ssl:/etc/nginx/ssl
restart: always
nginx.conf配置文件,主要是改http中的配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
upstream nacos_cluster{
server nacos1的IP:port weight=1 max_fails=2 fail_timeout=30s;
server nacos2的IP:port weight=1 max_fails=2 fail_timeout=30s;
server nacos3的IP:port weight=1 max_fails=2 fail_timeout=30s;
}
server{
listen nginx端口号;
server_name nginx的IP;
large_client_header_buffers 4 16k; # 读取大型客户端请求头的缓冲区的最大数量和大小
client_max_body_size 300m; #设置nginx能处理的最大请求主体大小。
client_body_buffer_size 128k; #请求主体的缓冲区大小。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 8 32k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 64k;
location / {
proxy_pass http://nacos_cluster;
}
}
include /etc/nginx/conf.d/*.conf;
}
mysql数据库建表语句
大家要用mysql官网的数据库镜像可以自建表,下载对应版本的nacos,mysql数据库文件在nacos/conf/nacos-mysql.sql就是了,进入mysql数据库导入即可。
属性配置列表
属性名称 | 描述 | 选项 |
---|---|---|
MODE | 系统启动方式: 集群/单机 | cluster/standalone默认 cluster |
NACOS_SERVERS | 集群地址 | p1:port1空格ip2:port2 空格ip3:port3 |
PREFER_HOST_MODE | 支持IP还是域名模式 | hostname/ip 默认 ip |
NACOS_SERVER_PORT | Nacos 运行端口 | 默认 8848 |
NACOS_SERVER_IP | 多网卡模式下可以指定IP | |
SPRING_DATASOURCE_PLATFORM | 单机模式下支持MYSQL数据库 | mysql / 空 默认:空 |
MYSQL_SERVICE_HOST | 数据库 连接地址 | |
MYSQL_SERVICE_PORT | 数据库端口 | 默认 : 3306 |
MYSQL_SERVICE_DB_NAME | 数据库库名 | |
MYSQL_SERVICE_USER | 数据库用户名 | |
MYSQL_SERVICE_PASSWORD | 数据库用户密码 | |
MYSQL_SERVICE_DB_PARAM | 数据库连接参数 | default : characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true |
MYSQL_DATABASE_NUM | It indicates the number of database | 默认 :1 |
JVM_XMS | -Xms | 默认 :2g |
JVM_XMX | -Xmx | 默认 :2g |
JVM_XMN | -Xmn | 默认 :1g |
JVM_MS | -XX:MetaspaceSize | 默认 :128m |
JVM_MMS | -XX:MaxMetaspaceSize | 默认 :320m |
NACOS_DEBUG | 是否开启远程DEBUG | y/n 默认 :n |
TOMCAT_ACCESSLOG_ENABLED | server.tomcat.accesslog.enabled | 默认 :false |
NACOS_AUTH_SYSTEM_TYPE | 权限系统类型选择,目前只支持nacos类型 | 默认 :nacos |
NACOS_AUTH_ENABLE | 是否开启权限系统 | 默认 :false |
NACOS_AUTH_TOKEN_EXPIRE_SECONDS | token 失效时间 | 默认 :18000 |
NACOS_AUTH_TOKEN | token | 默认 :SecretKey012345678901234567890123456789012345678901234567890123456789 |
NACOS_AUTH_CACHE_ENABLE | 权限缓存开关 ,开启后权限缓存的更新默认有15秒的延迟 | 默认 : false |
MEMBER_LIST | 通过环境变量的方式设置集群地址 | 例子:192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809 |
EMBEDDED_STORAGE | 是否开启集群嵌入式存储模式 |
embedded 默认 : none |
高级配置
如果上面的属性列表无法满足你的需求时,可以挂载custom.properties
到/home/nacos/init.d/
目录,然后在里面像使用Spring Boot的application.properties
文件一样配置属性, 并且这个文件配置的属性**优先级高于application.properties