Files
happy-life-star/backend/start-local.sh
T

187 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# 情感博物馆本地启动脚本
# 使用 application-local.yml 配置文件启动所有服务
echo "=========================================="
echo "情感博物馆本地服务启动脚本"
echo "=========================================="
# 检查Java环境
if ! command -v java &> /dev/null; then
echo "❌ Java未安装或未配置到PATH"
exit 1
fi
# 检查Maven环境
if ! command -v mvn &> /dev/null; then
echo "❌ Maven未安装或未配置到PATH"
exit 1
fi
echo "✅ Java和Maven环境检查通过"
# 创建日志目录
mkdir -p logs
# 服务列表
services=(
"emotion-user:19001:用户服务"
"emotion-ai:19002:AI服务"
"emotion-record:19003:记录服务"
"emotion-growth:19004:成长服务"
"emotion-explore:19005:探索服务"
"emotion-reward:19006:奖励服务"
"emotion-stats:19007:统计服务"
"emotion-gateway:19000:网关服务"
)
# 启动函数
start_service() {
local service_name=$1
local port=$2
local description=$3
echo "🚀 启动 $description ($service_name:$port)..."
cd $service_name
# 检查端口是否被占用
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
echo "⚠️ 端口 $port 已被占用,跳过 $service_name"
cd ..
return
fi
# 编译项目
echo "📦 编译 $service_name..."
mvn clean package -DskipTests -q
if [ $? -ne 0 ]; then
echo "$service_name 编译失败"
cd ..
return
fi
# 启动服务
echo "🔄 启动 $service_name..."
nohup java -jar -Dspring.profiles.active=local target/$service_name-1.0.0.jar > ../logs/$service_name-local.log 2>&1 &
# 记录PID
echo $! > ../logs/$service_name.pid
echo "$service_name 启动完成,PID: $!"
echo "📋 日志文件: logs/$service_name-local.log"
cd ..
# 等待服务启动
sleep 3
}
# 停止所有服务函数
stop_all_services() {
echo "🛑 停止所有服务..."
for pid_file in logs/*.pid; do
if [ -f "$pid_file" ]; then
pid=$(cat "$pid_file")
service_name=$(basename "$pid_file" .pid)
if kill -0 $pid 2>/dev/null; then
echo "🔄 停止 $service_name (PID: $pid)..."
kill $pid
rm "$pid_file"
else
echo "⚠️ $service_name 进程不存在,清理PID文件"
rm "$pid_file"
fi
fi
done
echo "✅ 所有服务已停止"
}
# 检查服务状态函数
check_services() {
echo "📊 检查服务状态..."
echo "----------------------------------------"
for service_info in "${services[@]}"; do
IFS=':' read -r service_name port description <<< "$service_info"
pid_file="logs/$service_name.pid"
if [ -f "$pid_file" ]; then
pid=$(cat "$pid_file")
if kill -0 $pid 2>/dev/null; then
if curl -s http://localhost:$port/actuator/health >/dev/null 2>&1; then
echo "$description ($service_name:$port) - 运行中"
else
echo "⚠️ $description ($service_name:$port) - 启动中..."
fi
else
echo "$description ($service_name:$port) - 已停止"
rm "$pid_file"
fi
else
echo "$description ($service_name:$port) - 未启动"
fi
done
}
# 主菜单
case "$1" in
"start")
echo "🚀 开始启动所有服务..."
# 按顺序启动服务(先启动基础服务,最后启动网关)
for service_info in "${services[@]}"; do
IFS=':' read -r service_name port description <<< "$service_info"
start_service "$service_name" "$port" "$description"
done
echo ""
echo "🎉 所有服务启动完成!"
echo ""
echo "📋 服务列表:"
for service_info in "${services[@]}"; do
IFS=':' read -r service_name port description <<< "$service_info"
echo " $description: http://localhost:$port"
done
echo ""
echo "📝 使用 './start-local.sh status' 检查服务状态"
echo "📝 使用 './start-local.sh stop' 停止所有服务"
;;
"stop")
stop_all_services
;;
"status")
check_services
;;
"restart")
echo "🔄 重启所有服务..."
stop_all_services
sleep 5
$0 start
;;
*)
echo "用法: $0 {start|stop|status|restart}"
echo ""
echo "命令说明:"
echo " start - 启动所有服务"
echo " stop - 停止所有服务"
echo " status - 检查服务状态"
echo " restart - 重启所有服务"
echo ""
echo "示例:"
echo " $0 start # 启动所有服务"
echo " $0 status # 检查服务状态"
echo " $0 stop # 停止所有服务"
exit 1
;;
esac