# Nacos Cluster Mode - Docker Compose
# 适用于生产环境，推荐3节点及以上
version: '3.8'

services:
  # ---------------------------------------------------------------
  # MySQL for Nacos Cluster (生产环境建议使用外部MySQL主从)
  # ---------------------------------------------------------------
  nacos-mysql:
    image: mysql:8.0
    container_name: erp-nacos-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root123456
      MYSQL_DATABASE: nacos_config
      MYSQL_USER: nacos
      MYSQL_PASSWORD: nacos123456
    ports:
      - "3308:3306"
    volumes:
      - nacos_mysql_data:/var/lib/mysql
      - ./init/mysql-schema.sql:/docker-entrypoint-initdb.d/mysql-schema.sql
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
    networks:
      - nacos-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot123456"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # ---------------------------------------------------------------
  # Nacos Server Node 1
  # ---------------------------------------------------------------
  nacos-server-1:
    image: nacos/nacos-server:v2.2.3
    container_name: erp-nacos-1
    hostname: nacos-server-1
    environment:
      MODE: cluster
      NACOS_SERVERS: "nacos-server-1:8848 nacos-server-2:8848 nacos-server-3:8848"
      MYSQL_SERVICE_HOST: nacos-mysql
      MYSQL_SERVICE_DB_NAME: nacos_config
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos123456
      # 集群配置
      NACOS_SERVER_IP: nacos-server-1
      # JVM调优（生产环境推荐2g+）
      JVM_XMS: 1g
      JVM_XMX: 1g
      JVM_XMN: 512m
      # 认证配置（生产环境务必修改SecretKey）
      NACOS_AUTH_ENABLE: "true"
      NACOS_AUTH_TOKEN: "SecretKey012345678901234567890123456789012345678901234567890123456789"
      NACOS_AUTH_IDENTITY_KEY: "serverIdentity"
      NACOS_AUTH_IDENTITY_VALUE: "security"
    ports:
      - "8841:8848"
      - "9841:9848"
      - "9842:9849"
    volumes:
      - nacos1_logs:/home/nacos/logs
      - nacos1_data:/home/nacos/data
    depends_on:
      nacos-mysql:
        condition: service_healthy
    networks:
      - nacos-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/v1/console/health/readiness"]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 60s

  # ---------------------------------------------------------------
  # Nacos Server Node 2
  # ---------------------------------------------------------------
  nacos-server-2:
    image: nacos/nacos-server:v2.2.3
    container_name: erp-nacos-2
    hostname: nacos-server-2
    environment:
      MODE: cluster
      NACOS_SERVERS: "nacos-server-1:8848 nacos-server-2:8848 nacos-server-3:8848"
      MYSQL_SERVICE_HOST: nacos-mysql
      MYSQL_SERVICE_DB_NAME: nacos_config
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos123456
      NACOS_SERVER_IP: nacos-server-2
      JVM_XMS: 1g
      JVM_XMX: 1g
      JVM_XMN: 512m
      NACOS_AUTH_ENABLE: "true"
      NACOS_AUTH_TOKEN: "SecretKey012345678901234567890123456789012345678901234567890123456789"
      NACOS_AUTH_IDENTITY_KEY: "serverIdentity"
      NACOS_AUTH_IDENTITY_VALUE: "security"
    ports:
      - "8842:8848"
      - "9843:9848"
      - "9844:9849"
    volumes:
      - nacos2_logs:/home/nacos/logs
      - nacos2_data:/home/nacos/data
    depends_on:
      nacos-mysql:
        condition: service_healthy
    networks:
      - nacos-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/v1/console/health/readiness"]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 60s

  # ---------------------------------------------------------------
  # Nacos Server Node 3
  # ---------------------------------------------------------------
  nacos-server-3:
    image: nacos/nacos-server:v2.2.3
    container_name: erp-nacos-3
    hostname: nacos-server-3
    environment:
      MODE: cluster
      NACOS_SERVERS: "nacos-server-1:8848 nacos-server-2:8848 nacos-server-3:8848"
      MYSQL_SERVICE_HOST: nacos-mysql
      MYSQL_SERVICE_DB_NAME: nacos_config
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos123456
      NACOS_SERVER_IP: nacos-server-3
      JVM_XMS: 1g
      JVM_XMX: 1g
      JVM_XMN: 512m
      NACOS_AUTH_ENABLE: "true"
      NACOS_AUTH_TOKEN: "SecretKey012345678901234567890123456789012345678901234567890123456789"
      NACOS_AUTH_IDENTITY_KEY: "serverIdentity"
      NACOS_AUTH_IDENTITY_VALUE: "security"
    ports:
      - "8843:8848"
      - "9845:9848"
      - "9846:9849"
    volumes:
      - nacos3_logs:/home/nacos/logs
      - nacos3_data:/home/nacos/data
    depends_on:
      nacos-mysql:
        condition: service_healthy
    networks:
      - nacos-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/v1/console/health/readiness"]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 60s

  # ---------------------------------------------------------------
  # Nginx as Load Balancer for Nacos Cluster
  # ---------------------------------------------------------------
  nacos-lb:
    image: nginx:alpine
    container_name: erp-nacos-lb
    ports:
      - "8848:8848"
      - "9848:9848"
    volumes:
      - ./cluster/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - nacos-server-1
      - nacos-server-2
      - nacos-server-3
    networks:
      - nacos-network
    restart: unless-stopped

volumes:
  nacos_mysql_data:
  nacos1_logs:
  nacos1_data:
  nacos2_logs:
  nacos2_data:
  nacos3_logs:
  nacos3_data:

networks:
  nacos-network:
    driver: bridge
