feat: 项目初始化及当前全部内容提交
This commit is contained in:
Executable
+209
@@ -0,0 +1,209 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ============================================================================
|
||||
# 情绪博物馆自动化开发脚本
|
||||
# 自动编译、启动、监控文件变化并重启服务
|
||||
# ============================================================================
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 配置
|
||||
PROJECT_ROOT=$(pwd)
|
||||
SERVICES=("emotion-gateway" "emotion-user" "emotion-ai" "emotion-record" "emotion-growth" "emotion-explore" "emotion-reward" "emotion-stats")
|
||||
PID_FILE="$PROJECT_ROOT/.dev-pids"
|
||||
LOG_DIR="$PROJECT_ROOT/dev-logs"
|
||||
|
||||
# 创建日志目录
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
echo -e "${BLUE}情绪博物馆自动化开发环境${NC}"
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
|
||||
# 清理函数
|
||||
cleanup() {
|
||||
echo -e "\n${YELLOW}正在停止所有服务...${NC}"
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
while read line; do
|
||||
if [ ! -z "$line" ]; then
|
||||
echo "停止进程: $line"
|
||||
kill -9 $line 2>/dev/null || true
|
||||
fi
|
||||
done < "$PID_FILE"
|
||||
rm -f "$PID_FILE"
|
||||
fi
|
||||
|
||||
# 清理Maven进程
|
||||
pkill -f "mvn.*spring-boot:run" 2>/dev/null || true
|
||||
|
||||
echo -e "${GREEN}✅ 清理完成${NC}"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# 捕获退出信号
|
||||
trap cleanup SIGINT SIGTERM
|
||||
|
||||
# 编译项目
|
||||
compile_project() {
|
||||
echo -e "${YELLOW}🔄 编译项目...${NC}"
|
||||
mvn clean compile -DskipTests -q
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✅ 编译成功${NC}"
|
||||
return 0
|
||||
else
|
||||
echo -e "${RED}❌ 编译失败${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 启动单个服务
|
||||
start_service() {
|
||||
local service=$1
|
||||
local port=$2
|
||||
|
||||
echo -e "${BLUE}🚀 启动 $service (端口: $port)...${NC}"
|
||||
|
||||
cd "$PROJECT_ROOT/$service"
|
||||
|
||||
# 启动服务并获取PID
|
||||
nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=$port" \
|
||||
> "$LOG_DIR/$service.log" 2>&1 &
|
||||
|
||||
local pid=$!
|
||||
echo "$pid" >> "$PID_FILE"
|
||||
|
||||
echo -e "${GREEN}✅ $service 已启动 (PID: $pid)${NC}"
|
||||
|
||||
cd "$PROJECT_ROOT"
|
||||
}
|
||||
|
||||
# 检查服务健康状态
|
||||
check_service_health() {
|
||||
local service=$1
|
||||
local port=$2
|
||||
local max_attempts=30
|
||||
local attempt=0
|
||||
|
||||
echo -e "${YELLOW}⏳ 等待 $service 启动...${NC}"
|
||||
|
||||
while [ $attempt -lt $max_attempts ]; do
|
||||
if curl -s "http://localhost:$port/actuator/health" > /dev/null 2>&1; then
|
||||
echo -e "${GREEN}✅ $service 健康检查通过${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
attempt=$((attempt + 1))
|
||||
sleep 2
|
||||
echo -n "."
|
||||
done
|
||||
|
||||
echo -e "\n${RED}❌ $service 健康检查失败${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
# 监控文件变化
|
||||
monitor_changes() {
|
||||
echo -e "${BLUE}👀 开始监控文件变化...${NC}"
|
||||
echo -e "${YELLOW}按 Ctrl+C 停止监控${NC}"
|
||||
|
||||
# 使用fswatch监控文件变化 (需要安装: brew install fswatch)
|
||||
if command -v fswatch > /dev/null; then
|
||||
fswatch -o -r --exclude="target" --exclude=".git" --exclude="node_modules" \
|
||||
--include=".*\\.java$" --include=".*\\.yml$" --include=".*\\.xml$" \
|
||||
"$PROJECT_ROOT" | while read f; do
|
||||
echo -e "${YELLOW}🔄 检测到文件变化,重新编译...${NC}"
|
||||
if compile_project; then
|
||||
echo -e "${GREEN}📝 代码已更新,DevTools将自动重启服务${NC}"
|
||||
fi
|
||||
done
|
||||
else
|
||||
# 如果没有fswatch,使用简单的循环检查
|
||||
echo -e "${YELLOW}⚠️ 建议安装 fswatch 以获得更好的文件监控体验:brew install fswatch${NC}"
|
||||
while true; do
|
||||
sleep 5
|
||||
# 简单的时间戳检查(这里可以根据需要扩展)
|
||||
echo -e "${BLUE}💓 服务运行中... ($(date))${NC}"
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# 显示服务状态
|
||||
show_status() {
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
echo -e "${BLUE}服务状态${NC}"
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
|
||||
local ports=(8080 8081 8082 8083 8084 8085 8086 8087)
|
||||
local i=0
|
||||
|
||||
for service in "${SERVICES[@]}"; do
|
||||
local port=${ports[$i]}
|
||||
if curl -s "http://localhost:$port/actuator/health" > /dev/null 2>&1; then
|
||||
echo -e "${GREEN}✅ $service (端口: $port) - 运行中${NC}"
|
||||
else
|
||||
echo -e "${RED}❌ $service (端口: $port) - 停止${NC}"
|
||||
fi
|
||||
i=$((i + 1))
|
||||
done
|
||||
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
echo -e "${YELLOW}📋 日志文件位置: $LOG_DIR/${NC}"
|
||||
echo -e "${YELLOW}🌐 API网关: http://localhost:9000${NC}"
|
||||
echo -e "${YELLOW}📚 API文档: http://localhost:9000/doc.html${NC}"
|
||||
echo -e "${BLUE}===========================================${NC}"
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
# 清理之前的进程
|
||||
cleanup 2>/dev/null || true
|
||||
|
||||
# 编译项目
|
||||
if ! compile_project; then
|
||||
echo -e "${RED}❌ 编译失败,请检查代码错误${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 启动服务
|
||||
echo -e "${BLUE}🚀 启动所有微服务...${NC}"
|
||||
|
||||
local ports=(9000 9001 9002 9003 9004 9005 9006 9007)
|
||||
local i=0
|
||||
|
||||
for service in "${SERVICES[@]}"; do
|
||||
start_service "$service" "${ports[$i]}"
|
||||
sleep 3 # 给服务一些启动时间
|
||||
i=$((i + 1))
|
||||
done
|
||||
|
||||
# 等待所有服务启动
|
||||
sleep 10
|
||||
|
||||
# 显示状态
|
||||
show_status
|
||||
|
||||
# 开始监控
|
||||
monitor_changes
|
||||
}
|
||||
|
||||
# 检查参数
|
||||
case "${1:-}" in
|
||||
"status")
|
||||
show_status
|
||||
;;
|
||||
"stop")
|
||||
cleanup
|
||||
;;
|
||||
"logs")
|
||||
echo -e "${BLUE}📋 实时日志 (按 Ctrl+C 退出):${NC}"
|
||||
tail -f "$LOG_DIR"/*.log
|
||||
;;
|
||||
*)
|
||||
main
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user