#!/bin/bash
# RocketMQ 集群启动脚本

set -e

echo "=========================================="
echo "  RocketMQ 集群启动脚本"
echo "=========================================="

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "错误: Docker 未运行，请先启动Docker"
    exit 1
fi

# 创建必要的目录
echo "创建数据目录..."
mkdir -p data/namesrv-1 data/namesrv-2
mkdir -p data/broker-master-1 data/broker-master-2
mkdir -p data/broker-slave-1 data/broker-slave-2

# 停止已存在的容器
echo "停止已存在的容器..."
docker-compose -f docker-compose.yml down 2>/dev/null || true

# 启动集群
echo "启动RocketMQ集群..."
docker-compose -f docker-compose.yml up -d

# 等待NameServer启动
echo "等待NameServer启动..."
sleep 10

# 检查NameServer状态
echo "检查NameServer状态..."
docker ps | grep rocketmq-namesrv

# 等待Broker启动
echo "等待Broker启动..."
sleep 15

# 检查Broker状态
echo "检查Broker状态..."
docker ps | grep rocketmq-broker

# 创建Topic
echo "创建消息主题..."
bash config/setup-topics.sh

# 访问信息
echo ""
echo "=========================================="
echo "  RocketMQ 集群启动完成!"
echo "=========================================="
echo ""
echo "服务访问地址:"
echo "  - NameServer-1: localhost:9876"
echo "  - NameServer-2: localhost:9877"
echo "  - Broker-Master-1: localhost:10911"
echo "  - Broker-Master-2: localhost:10921"
echo "  - Dashboard: http://localhost:8080"
echo ""
echo "监控访问地址:"
echo "  - Prometheus: http://localhost:9090"
echo "  - Grafana: http://localhost:3000 (admin/admin123)"
echo ""
