Files
DDDMicroFrontend/docker/nacos-cluster/docker-compose.yml
2025-10-04 00:08:33 +08:00

163 lines
4.5 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 使用版本 3.8,它与 Swarm 模式兼容
version: "3.8"
# 定义服务
services:
# MySQL 数据库服务
mysql:
# 使用指定的 MySQL 镜像
image: mysql:8.0.31
# 使用环境变量文件,与原来的配置保持一致
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nacos_devtest
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos
- LANG=C.UTF-8
# 数据卷挂载,确保 MySQL 数据持久化
volumes:
- mysql-data:/var/lib/mysql
- ./mysql-schema.sql:/docker-entrypoint-initdb.d/mysql-schema.sql
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
ports:
- target: 3306
published: 3306
protocol: tcp
mode: host # 数据库通常使用 host 模式,避免路由网格开销
# 健康检查,确保数据库启动完成后再启动 Nacos
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 5s
timeout: 10s
retries: 10
# Swarm 部署配置
deploy:
# 副本数为 1因为单个数据库实例足够
replicas: 1
# 重启策略:任何情况下容器退出都自动重启
restart_policy:
condition: any
# 将服务加入我们创建的网络
networks:
- nacos-net
# Nacos 服务
nacos:
hostname: "nacos{{.Task.Slot}}"
# 使用 Nacos 镜像
image: nacos/nacos-server:v3.1.0
# 挂载日志目录
volumes:
- nacos-logs:/home/nacos/logs
# 暴露 Nacos 所需的端口
ports:
- target: 8080
published: 8080
protocol: tcp
mode: ingress # Web 控制台使用路由网格实现负载均衡
- target: 7848
published: 7848
protocol: tcp
mode: ingress # 集群通信端口
- target: 8848
published: 8848
protocol: tcp
mode: ingress # 主要服务端口,使用路由网格
- target: 9848
published: 9848
protocol: tcp
mode: ingress # gRPC 端口
- target: 9849
published: 9849
protocol: tcp
mode: ingress # gRPC 端口
# 使用环境变量文件
environment:
- MODE=cluster
- PREFER_HOST_MODE=hostname
- NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_DB_NAME=nacos_devtest
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos
- MYSQL_SERVICE_RETRY_COUNT=10
- MYSQL_SERVICE_RETRY_DELAY=5000
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
- NACOS_SERVER_AUTH_ENABLE=false
- NACOS_CONSOLE_AUTH_ENABLE=false
- NACOS_AUTH_ENABLE=false
- NACOS_AUTH_IDENTITY_KEY=bmy
- NACOS_AUTH_IDENTITY_VALUE=lb714500.
- NACOS_AUTH_TOKEN=KjdAdzBIMTBtKElJS0NrWTIxNkJ0RXN4YXlHaipSUmdDZA==
# Swarm 部署配置
deploy:
# 关键:指定副本数为 3Swarm 会自动创建 3 个 Nacos 容器
replicas: 3
# 重启策略
restart_policy:
condition: any
# 启动顺序控制
# 这是 Swarm 中实现 "depends_on" 的方式
# 它会在 mysql 服务健康检查通过后,才开始启动 Nacos 服务
update_config:
parallelism: 1
delay: 30s
# 资源限制(可选,可根据你的服务器配置调整)
resources:
limits:
cpus: '0.50'
memory: '512M'
reservations:
cpus: '0.25'
memory: '256M'
networks:
- nacos-net
# Prometheus 服务
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus/prometheus-cluster.yaml:/etc/prometheus/prometheus.yml
ports:
- target: 9090
published: 9090
protocol: tcp
mode: ingress # 监控服务使用路由网格
deploy:
replicas: 1
restart_policy:
condition: on-failure
update_config:
order: start-first
networks:
- nacos-net
# Grafana 服务
grafana:
image: grafana/grafana:latest
ports:
- target: 3000
published: 3000
protocol: tcp
mode: ingress # Grafana 使用路由网格
deploy:
# 通常 Grafana 单实例即可
replicas: 1
restart_policy:
condition: on-failure
networks:
- nacos-net
volumes:
nacos-logs:
driver: local
mysql-data:
driver: local
networks:
nacos-net:
driver: overlay