services:
# ====================== MySQL 数据库 ======================
  mysql:
    image: mysql:${MYSQL_VERSION:-8.0}
    container_name: urban-lifeline-nacos-mysql
    restart: unless-stopped
    networks:
      - urban-lifeline
    ports:
      - "${MYSQL_EXPOSE_PORT:-3307}:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root123456}
      MYSQL_DATABASE: ${NACOS_DB_NAME:-nacos_config}
      MYSQL_USER: ${MYSQL_USER:-nacos}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-nacos123456}
      TZ: Asia/Shanghai
    volumes:
      - ${DATA_ROOT:-../volumes}/nacos/mysql:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --default-authentication-plugin=mysql_native_password
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD:-root123456}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

# ====================== Nacos 注册中心 ======================
  nacos:
    image: nacos/nacos-server:${NACOS_VERSION:-v3.1.0}
    container_name: urban-lifeline-nacos
    restart: unless-stopped
    env_file:
      - .env
    networks:
      - urban-lifeline
    ports:
      - "${NACOS_CONSOLE_PORT:-8081}:8081"
      - "${NACOS_PORT:-8848}:8848"
      - "${NACOS_GRPC_PORT:-9848}:9848"
      - "${NACOS_RAFT_PORT:-9849}:9849"
    environment:
      MODE: ${NACOS_MODE:-standalone}
      SPRING_DATASOURCE_PLATFORM: mysql
      MYSQL_SERVICE_HOST: mysql
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_DB_NAME: ${NACOS_DB_NAME:-nacos_config}
      MYSQL_SERVICE_USER: ${MYSQL_USER:-nacos}
      MYSQL_SERVICE_PASSWORD: ${MYSQL_PASSWORD:-nacos123456}
      MYSQL_SERVICE_DB_PARAM: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
      JVM_XMS: ${NACOS_JVM_XMS:-512m}
      JVM_XMX: ${NACOS_JVM_XMX:-512m}
      JVM_XMN: ${NACOS_JVM_XMN:-256m}
      NACOS_AUTH_ENABLE: ${NACOS_AUTH_ENABLE:-false}
      NACOS_AUTH_TOKEN: ${NACOS_AUTH_TOKEN:-ZlRkR2ZxR3BvZ1F0a3JxY2V6RUx2cUh1Rkx6V1ZQbE9kUVd1R1VOcWFFS2t3dG5hS0E9PQ==}
      NACOS_AUTH_IDENTITY_KEY: ${NACOS_AUTH_IDENTITY_KEY:-serverIdentity}
      NACOS_AUTH_IDENTITY_VALUE: ${NACOS_AUTH_IDENTITY_VALUE:-security}
    volumes:
      - ${DATA_ROOT:-../volumes}/nacos/data:/home/nacos/data
      - ${DATA_ROOT:-../volumes}/nacos/logs:/home/nacos/logs
    depends_on:
      mysql:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos/"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

networks:
  urban-lifeline:
    name: urban-lifeline
