version: '3.8'

services:
  seata-server:
    image: seataio/seata:1.8.0
    container_name: seata-server
    hostname: seata-server
    ports:
      - "8091:8091"
      - "7091:7091"
    environment:
      - SEATA_CONFIG_NAME=file:/root/seata-config/registry,file:/root/seata-config
      - SEATA_STORE_MODE=db
      - SEATA_STORE_DB_DRIVER=com.mysql.cj.jdbc.Driver
      - SEATA_STORE_DB_URL=jdbc:mysql://mysql:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      - SEATA_STORE_DB_USER=root
      - SEATA_STORE_DB_PASSWORD=${MYSQL_ROOT_PASSWORD:-root123}
      - SEATA_STORE_DB_GLOBAL_TABLE=global_table
      - SEATA_STORE_DB_BRANCH_TABLE=branch_table
      - SEATA_STORE_DB_LOCK_TABLE=lock_table
      - SEATA_STORE_DB_PUBLIC_KEY=MySQL_RSA_PUBLIC_KEY
      - SEATA_TRANSACTION_LOCK_RETRY_INTERVAL=30
      - SEATA_TRANSACTION_LOCK_MAX_RETRY=100
      - SEATA_LOG_PERSIST_MODE=file
      - SEATA_SERVICE_VGROUP_MAPPING.default_tx_group=default
      - SEATA_SERVICE_ENABLE_DEGRADE=false
      - SEATA_SERVICE_DISABLE_GLOBAL_TRANSACTION=false
      - SEATA_SERVER_MODE=standalone
      - JAVA_OPT_EXT_JMX="-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9998 -Dcom.sun.management.jmxremote.rmi.port=9999 -Djava.rmi.server.hostname=127.0.0.1"
    volumes:
      - ./config/registry.conf:/root/seata-config/registry.conf:ro
      - ./config/file.conf:/root/seata-config/file.conf:ro
      - seata-server-data:/root/logs/seata
    networks:
      - seata-network
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:7091/health"]
      interval: 30s
      timeout: 10s
      retries: 5

  mysql:
    image: mysql:8.0
    container_name: seata-mysql
    hostname: seata-mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root123}
      - MYSQL_DATABASE=seata
    command:
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_general_ci
      - --max_connections=1000
      - --lower_case_table_names=1
    volumes:
      - seata-mysql-data:/var/lib/mysql
      - ./sql/store.sql:/docker-entrypoint-initdb.d/01-store.sql:ro
    networks:
      - seata-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-p${MYSQL_ROOT_PASSWORD:-root123}"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # Seata HA Mode - Additional instance
  seata-server-cluster:
    image: seataio/seata:1.8.0
    container_name: seata-server-cluster
    hostname: seata-server-cluster
    ports:
      - "8092:8091"
      - "7092:7091"
    environment:
      - SEATA_CONFIG_NAME=file:/root/seata-config/registry,file:/root/seata-config
      - SEATA_STORE_MODE=db
      - SEATA_STORE_DB_DRIVER=com.mysql.cj.jdbc.Driver
      - SEATA_STORE_DB_URL=jdbc:mysql://mysql:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      - SEATA_STORE_DB_USER=root
      - SEATA_STORE_DB_PASSWORD=${MYSQL_ROOT_PASSWORD:-root123}
      - SEATA_STORE_DB_GLOBAL_TABLE=global_table
      - SEATA_STORE_DB_BRANCH_TABLE=branch_table
      - SEATA_STORE_DB_LOCK_TABLE=lock_table
      - SEATA_SERVICE_VGROUP_MAPPING.default_tx_group=default
      - SEATA_SERVER_MODE=standalone
    volumes:
      - ./config/registry.conf:/root/seata-config/registry.conf:ro
      - ./config/file.conf:/root/seata-config/file.conf:ro
      - seata-server-cluster-data:/root/logs/seata
    networks:
      - seata-network
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped

  prometheus:
    image: prom/prometheus:latest
    container_name: seata-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
    networks:
      - seata-network
    restart: unless-stopped

  grafana:
    image: grafana/grafana:latest
    container_name: seata-grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin123
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-data:/var/lib/grafana
      - ./monitoring/grafana/provisioning:/etc/grafana/provisioning:ro
    networks:
      - seata-network
    depends_on:
      - prometheus
    restart: unless-stopped

volumes:
  seata-mysql-data:
  seata-server-data:
  seata-server-cluster-data:
  prometheus-data:
  grafana-data:

networks:
  seata-network:
    driver: bridge
