368 lines
10 KiB
Bash
368 lines
10 KiB
Bash
#!/bin/bash
|
|
|
|
# 扩展服务启动停止一体化脚本
|
|
# 使用方法: ./start-extend-services.sh [start|stop|restart|status] [service_name]
|
|
# 服务名称: monitor, snailjob, all
|
|
|
|
# 配置变量
|
|
MONITOR_JAR="ruoyi-monitor-admin.jar"
|
|
SNAILJOB_JAR="ruoyi-snailjob-server.jar"
|
|
JAVA_OPTS="-Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m"
|
|
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
|
|
JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
|
|
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/"
|
|
|
|
# 服务配置
|
|
declare -A SERVICES
|
|
SERVICES[monitor]="9090:ruoyi-monitor-admin:prod"
|
|
SERVICES[snailjob]="8800:ruoyi-snailjob-server:prod"
|
|
|
|
# 日志目录
|
|
LOG_DIR="logs"
|
|
mkdir -p $LOG_DIR
|
|
|
|
# 颜色输出
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 打印带颜色的消息
|
|
print_message() {
|
|
local color=$1
|
|
local message=$2
|
|
echo -e "${color}${message}${NC}"
|
|
}
|
|
|
|
# 检查JAR文件是否存在
|
|
check_jar() {
|
|
local service=$1
|
|
local jar_file=""
|
|
|
|
case $service in
|
|
monitor)
|
|
jar_file="./$MONITOR_JAR"
|
|
;;
|
|
snailjob)
|
|
jar_file="./$SNAILJOB_JAR"
|
|
;;
|
|
esac
|
|
|
|
if [ ! -f "$jar_file" ]; then
|
|
print_message $RED "错误: $jar_file 文件不存在!"
|
|
print_message $YELLOW "请先编译项目: mvn clean package -Pprod"
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# 启动单个服务
|
|
start_service() {
|
|
local service=$1
|
|
local config=${SERVICES[$service]}
|
|
local port=$(echo $config | cut -d: -f1)
|
|
local app_name=$(echo $config | cut -d: -f2)
|
|
local profile=$(echo $config | cut -d: -f3)
|
|
|
|
local pid_file="logs/${app_name}.pid"
|
|
local log_file="logs/${app_name}.log"
|
|
|
|
# 检查是否已经运行
|
|
if [ -f "$pid_file" ]; then
|
|
local pid=$(cat "$pid_file")
|
|
if ps -p $pid > /dev/null 2>&1; then
|
|
print_message $YELLOW "服务 $service 已经在运行中 (PID: $pid)"
|
|
return
|
|
else
|
|
# PID文件存在但进程不存在,清理PID文件
|
|
rm -f "$pid_file"
|
|
fi
|
|
fi
|
|
|
|
# 检查JAR文件
|
|
if ! check_jar $service; then
|
|
return 1
|
|
fi
|
|
|
|
print_message $GREEN "正在启动服务 $service (端口: $port, 配置文件: $profile)..."
|
|
|
|
# 启动应用
|
|
case $service in
|
|
monitor)
|
|
local jar_path="../ruoyi-extend/ruoyi-monitor-admin/target/$MONITOR_JAR"
|
|
nohup java $JAVA_OPTS \
|
|
-Dspring.profiles.active=$profile \
|
|
-Dserver.port=$port \
|
|
-jar "$jar_path" \
|
|
> "$log_file" 2>&1 &
|
|
;;
|
|
snailjob)
|
|
local jar_path="../ruoyi-extend/ruoyi-snailjob-server/target/$SNAILJOB_JAR"
|
|
nohup java $JAVA_OPTS \
|
|
-Dspring.profiles.active=$profile \
|
|
-Dserver.port=$port \
|
|
-jar "$jar_path" \
|
|
> "$log_file" 2>&1 &
|
|
;;
|
|
esac
|
|
|
|
local new_pid=$!
|
|
echo $new_pid > "$pid_file"
|
|
|
|
# 等待启动
|
|
sleep 5
|
|
|
|
# 检查启动状态
|
|
if ps -p $new_pid > /dev/null 2>&1; then
|
|
print_message $GREEN "服务 $service 启动成功 (PID: $new_pid)"
|
|
print_message $GREEN "访问地址: http://localhost:$port"
|
|
print_message $GREEN "日志文件: $log_file"
|
|
|
|
# 显示服务特定信息
|
|
case $service in
|
|
monitor)
|
|
print_message $BLUE "监控中心: http://localhost:$port/admin"
|
|
;;
|
|
snailjob)
|
|
print_message $BLUE "任务调度: http://localhost:$port/snail-job"
|
|
;;
|
|
esac
|
|
else
|
|
print_message $RED "服务 $service 启动失败,请检查日志文件: $log_file"
|
|
rm -f "$pid_file"
|
|
fi
|
|
}
|
|
|
|
# 停止单个服务
|
|
stop_service() {
|
|
local service=$1
|
|
local config=${SERVICES[$service]}
|
|
local app_name=$(echo $config | cut -d: -f2)
|
|
local pid_file="logs/${app_name}.pid"
|
|
|
|
if [ -f "$pid_file" ]; then
|
|
local pid=$(cat "$pid_file")
|
|
if ps -p $pid > /dev/null 2>&1; then
|
|
print_message $YELLOW "正在停止服务 $service (PID: $pid)..."
|
|
kill $pid
|
|
|
|
# 等待进程结束
|
|
local count=0
|
|
while ps -p $pid > /dev/null 2>&1 && [ $count -lt 30 ]; do
|
|
sleep 1
|
|
count=$((count + 1))
|
|
done
|
|
|
|
# 强制杀死进程
|
|
if ps -p $pid > /dev/null 2>&1; then
|
|
print_message $YELLOW "强制停止服务 $service..."
|
|
kill -9 $pid
|
|
fi
|
|
|
|
rm -f "$pid_file"
|
|
print_message $GREEN "服务 $service 已停止"
|
|
else
|
|
print_message $YELLOW "服务 $service 未运行"
|
|
rm -f "$pid_file"
|
|
fi
|
|
else
|
|
print_message $YELLOW "服务 $service 未运行"
|
|
fi
|
|
}
|
|
|
|
# 重启单个服务
|
|
restart_service() {
|
|
local service=$1
|
|
print_message $YELLOW "重启服务 $service..."
|
|
stop_service $service
|
|
sleep 3
|
|
start_service $service
|
|
}
|
|
|
|
# 查看服务状态
|
|
status_service() {
|
|
local service=$1
|
|
local config=${SERVICES[$service]}
|
|
local port=$(echo $config | cut -d: -f1)
|
|
local app_name=$(echo $config | cut -d: -f2)
|
|
local pid_file="logs/${app_name}.pid"
|
|
|
|
if [ -f "$pid_file" ]; then
|
|
local pid=$(cat "$pid_file")
|
|
if ps -p $pid > /dev/null 2>&1; then
|
|
local memory=$(ps -o rss= -p $pid | awk '{print $1/1024 " MB"}')
|
|
local uptime=$(ps -o etime= -p $pid)
|
|
print_message $GREEN "服务 $service: 运行中 (PID: $pid, 端口: $port, 内存: $memory, 运行时间: $uptime)"
|
|
else
|
|
print_message $RED "服务 $service: 异常 (PID文件存在但进程不存在)"
|
|
rm -f "$pid_file"
|
|
fi
|
|
else
|
|
print_message $YELLOW "服务 $service: 未运行 (端口: $port)"
|
|
fi
|
|
}
|
|
|
|
# 启动所有服务
|
|
start_all() {
|
|
print_message $GREEN "启动所有扩展服务..."
|
|
for service in "${!SERVICES[@]}"; do
|
|
start_service $service
|
|
sleep 3
|
|
done
|
|
}
|
|
|
|
# 停止所有服务
|
|
stop_all() {
|
|
print_message $YELLOW "停止所有扩展服务..."
|
|
for service in "${!SERVICES[@]}"; do
|
|
stop_service $service
|
|
done
|
|
}
|
|
|
|
# 重启所有服务
|
|
restart_all() {
|
|
print_message $YELLOW "重启所有扩展服务..."
|
|
stop_all
|
|
sleep 5
|
|
start_all
|
|
}
|
|
|
|
# 查看所有服务状态
|
|
status_all() {
|
|
print_message $GREEN "所有扩展服务状态:"
|
|
for service in "${!SERVICES[@]}"; do
|
|
status_service $service
|
|
done
|
|
}
|
|
|
|
# 编译项目
|
|
build_project() {
|
|
print_message $BLUE "开始编译扩展服务项目..."
|
|
|
|
# 编译monitor-admin
|
|
print_message $YELLOW "编译 ruoyi-monitor-admin..."
|
|
cd ../ruoyi-extend/ruoyi-monitor-admin
|
|
mvn clean package -Pprod
|
|
if [ $? -eq 0 ]; then
|
|
print_message $GREEN "ruoyi-monitor-admin 编译成功"
|
|
else
|
|
print_message $RED "ruoyi-monitor-admin 编译失败"
|
|
return 1
|
|
fi
|
|
|
|
# 编译snailjob-server
|
|
print_message $YELLOW "编译 ruoyi-snailjob-server..."
|
|
cd ../ruoyi-snailjob-server
|
|
mvn clean package -Pprod
|
|
if [ $? -eq 0 ]; then
|
|
print_message $GREEN "ruoyi-snailjob-server 编译成功"
|
|
else
|
|
print_message $RED "ruoyi-snailjob-server 编译失败"
|
|
return 1
|
|
fi
|
|
|
|
# 返回原目录
|
|
cd ../../部署文档/杭州第一技师部署文档
|
|
print_message $GREEN "所有扩展服务编译完成"
|
|
}
|
|
|
|
# 显示帮助信息
|
|
show_help() {
|
|
echo "扩展服务启动停止一体化脚本使用方法:"
|
|
echo " $0 [命令] [服务名称]"
|
|
echo ""
|
|
echo "命令:"
|
|
echo " start 启动服务"
|
|
echo " stop 停止服务"
|
|
echo " restart 重启服务"
|
|
echo " status 查看服务状态"
|
|
echo " build 编译项目"
|
|
echo ""
|
|
echo "服务名称:"
|
|
echo " monitor 启动监控中心服务 (端口: 9090)"
|
|
echo " snailjob 启动任务调度服务 (端口: 8800)"
|
|
echo " all 操作所有服务"
|
|
echo ""
|
|
echo "示例:"
|
|
echo " $0 start monitor # 启动监控中心服务"
|
|
echo " $0 start snailjob # 启动任务调度服务"
|
|
echo " $0 start all # 启动所有服务"
|
|
echo " $0 stop all # 停止所有服务"
|
|
echo " $0 status all # 查看所有服务状态"
|
|
echo " $0 build # 编译所有扩展服务"
|
|
echo ""
|
|
echo "服务访问地址:"
|
|
echo " 监控中心: http://localhost:9090/admin"
|
|
echo " 任务调度: http://localhost:8800/snail-job"
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
local action=$1
|
|
local service=$2
|
|
|
|
# 检查参数
|
|
if [ -z "$action" ]; then
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
case $action in
|
|
start)
|
|
if [ "$service" = "all" ]; then
|
|
start_all
|
|
elif [ -n "${SERVICES[$service]}" ]; then
|
|
start_service $service
|
|
else
|
|
print_message $RED "无效的服务名称: $service"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
;;
|
|
stop)
|
|
if [ "$service" = "all" ]; then
|
|
stop_all
|
|
elif [ -n "${SERVICES[$service]}" ]; then
|
|
stop_service $service
|
|
else
|
|
print_message $RED "无效的服务名称: $service"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
;;
|
|
restart)
|
|
if [ "$service" = "all" ]; then
|
|
restart_all
|
|
elif [ -n "${SERVICES[$service]}" ]; then
|
|
restart_service $service
|
|
else
|
|
print_message $RED "无效的服务名称: $service"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
;;
|
|
status)
|
|
if [ "$service" = "all" ]; then
|
|
status_all
|
|
elif [ -n "${SERVICES[$service]}" ]; then
|
|
status_service $service
|
|
else
|
|
print_message $RED "无效的服务名称: $service"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
;;
|
|
build)
|
|
build_project
|
|
;;
|
|
*)
|
|
print_message $RED "无效的命令: $action"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# 执行主函数
|
|
main "$@" |