start-extend-services.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #!/bin/bash
  2. # 扩展服务启动停止一体化脚本
  3. # 使用方法: ./start-extend-services.sh [start|stop|restart|status] [service_name]
  4. # 服务名称: monitor, snailjob, all
  5. # 配置变量
  6. MONITOR_JAR="ruoyi-monitor-admin.jar"
  7. SNAILJOB_JAR="ruoyi-snailjob-server.jar"
  8. JAVA_OPTS="-Xms2048m -Xmx2048m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m"
  9. JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  10. JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
  11. JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/"
  12. # 服务配置
  13. declare -A SERVICES
  14. SERVICES[monitor]="9090:ruoyi-monitor-admin:prod"
  15. SERVICES[snailjob]="8800:ruoyi-snailjob-server:prod"
  16. # 日志目录
  17. LOG_DIR="logs"
  18. mkdir -p $LOG_DIR
  19. # 颜色输出
  20. RED='\033[0;31m'
  21. GREEN='\033[0;32m'
  22. YELLOW='\033[1;33m'
  23. BLUE='\033[0;34m'
  24. NC='\033[0m' # No Color
  25. # 打印带颜色的消息
  26. print_message() {
  27. local color=$1
  28. local message=$2
  29. echo -e "${color}${message}${NC}"
  30. }
  31. # 检查JAR文件是否存在
  32. check_jar() {
  33. local service=$1
  34. local jar_file=""
  35. case $service in
  36. monitor)
  37. jar_file="./$MONITOR_JAR"
  38. ;;
  39. snailjob)
  40. jar_file="./$SNAILJOB_JAR"
  41. ;;
  42. esac
  43. if [ ! -f "$jar_file" ]; then
  44. print_message $RED "错误: $jar_file 文件不存在!"
  45. print_message $YELLOW "请先编译项目: mvn clean package -Pprod"
  46. return 1
  47. fi
  48. return 0
  49. }
  50. # 启动单个服务
  51. start_service() {
  52. local service=$1
  53. local config=${SERVICES[$service]}
  54. local port=$(echo $config | cut -d: -f1)
  55. local app_name=$(echo $config | cut -d: -f2)
  56. local profile=$(echo $config | cut -d: -f3)
  57. local pid_file="logs/${app_name}.pid"
  58. local log_file="logs/${app_name}.log"
  59. # 检查是否已经运行
  60. if [ -f "$pid_file" ]; then
  61. local pid=$(cat "$pid_file")
  62. if ps -p $pid > /dev/null 2>&1; then
  63. print_message $YELLOW "服务 $service 已经在运行中 (PID: $pid)"
  64. return
  65. else
  66. # PID文件存在但进程不存在,清理PID文件
  67. rm -f "$pid_file"
  68. fi
  69. fi
  70. # 检查JAR文件
  71. if ! check_jar $service; then
  72. return 1
  73. fi
  74. print_message $GREEN "正在启动服务 $service (端口: $port, 配置文件: $profile)..."
  75. # 启动应用
  76. case $service in
  77. monitor)
  78. local jar_path="../ruoyi-extend/ruoyi-monitor-admin/target/$MONITOR_JAR"
  79. nohup java $JAVA_OPTS \
  80. -Dspring.profiles.active=$profile \
  81. -Dserver.port=$port \
  82. -jar "$jar_path" \
  83. > "$log_file" 2>&1 &
  84. ;;
  85. snailjob)
  86. local jar_path="../ruoyi-extend/ruoyi-snailjob-server/target/$SNAILJOB_JAR"
  87. nohup java $JAVA_OPTS \
  88. -Dspring.profiles.active=$profile \
  89. -Dserver.port=$port \
  90. -jar "$jar_path" \
  91. > "$log_file" 2>&1 &
  92. ;;
  93. esac
  94. local new_pid=$!
  95. echo $new_pid > "$pid_file"
  96. # 等待启动
  97. sleep 5
  98. # 检查启动状态
  99. if ps -p $new_pid > /dev/null 2>&1; then
  100. print_message $GREEN "服务 $service 启动成功 (PID: $new_pid)"
  101. print_message $GREEN "访问地址: http://localhost:$port"
  102. print_message $GREEN "日志文件: $log_file"
  103. # 显示服务特定信息
  104. case $service in
  105. monitor)
  106. print_message $BLUE "监控中心: http://localhost:$port/admin"
  107. ;;
  108. snailjob)
  109. print_message $BLUE "任务调度: http://localhost:$port/snail-job"
  110. ;;
  111. esac
  112. else
  113. print_message $RED "服务 $service 启动失败,请检查日志文件: $log_file"
  114. rm -f "$pid_file"
  115. fi
  116. }
  117. # 停止单个服务
  118. stop_service() {
  119. local service=$1
  120. local config=${SERVICES[$service]}
  121. local app_name=$(echo $config | cut -d: -f2)
  122. local pid_file="logs/${app_name}.pid"
  123. if [ -f "$pid_file" ]; then
  124. local pid=$(cat "$pid_file")
  125. if ps -p $pid > /dev/null 2>&1; then
  126. print_message $YELLOW "正在停止服务 $service (PID: $pid)..."
  127. kill $pid
  128. # 等待进程结束
  129. local count=0
  130. while ps -p $pid > /dev/null 2>&1 && [ $count -lt 30 ]; do
  131. sleep 1
  132. count=$((count + 1))
  133. done
  134. # 强制杀死进程
  135. if ps -p $pid > /dev/null 2>&1; then
  136. print_message $YELLOW "强制停止服务 $service..."
  137. kill -9 $pid
  138. fi
  139. rm -f "$pid_file"
  140. print_message $GREEN "服务 $service 已停止"
  141. else
  142. print_message $YELLOW "服务 $service 未运行"
  143. rm -f "$pid_file"
  144. fi
  145. else
  146. print_message $YELLOW "服务 $service 未运行"
  147. fi
  148. }
  149. # 重启单个服务
  150. restart_service() {
  151. local service=$1
  152. print_message $YELLOW "重启服务 $service..."
  153. stop_service $service
  154. sleep 3
  155. start_service $service
  156. }
  157. # 查看服务状态
  158. status_service() {
  159. local service=$1
  160. local config=${SERVICES[$service]}
  161. local port=$(echo $config | cut -d: -f1)
  162. local app_name=$(echo $config | cut -d: -f2)
  163. local pid_file="logs/${app_name}.pid"
  164. if [ -f "$pid_file" ]; then
  165. local pid=$(cat "$pid_file")
  166. if ps -p $pid > /dev/null 2>&1; then
  167. local memory=$(ps -o rss= -p $pid | awk '{print $1/1024 " MB"}')
  168. local uptime=$(ps -o etime= -p $pid)
  169. print_message $GREEN "服务 $service: 运行中 (PID: $pid, 端口: $port, 内存: $memory, 运行时间: $uptime)"
  170. else
  171. print_message $RED "服务 $service: 异常 (PID文件存在但进程不存在)"
  172. rm -f "$pid_file"
  173. fi
  174. else
  175. print_message $YELLOW "服务 $service: 未运行 (端口: $port)"
  176. fi
  177. }
  178. # 启动所有服务
  179. start_all() {
  180. print_message $GREEN "启动所有扩展服务..."
  181. for service in "${!SERVICES[@]}"; do
  182. start_service $service
  183. sleep 3
  184. done
  185. }
  186. # 停止所有服务
  187. stop_all() {
  188. print_message $YELLOW "停止所有扩展服务..."
  189. for service in "${!SERVICES[@]}"; do
  190. stop_service $service
  191. done
  192. }
  193. # 重启所有服务
  194. restart_all() {
  195. print_message $YELLOW "重启所有扩展服务..."
  196. stop_all
  197. sleep 5
  198. start_all
  199. }
  200. # 查看所有服务状态
  201. status_all() {
  202. print_message $GREEN "所有扩展服务状态:"
  203. for service in "${!SERVICES[@]}"; do
  204. status_service $service
  205. done
  206. }
  207. # 编译项目
  208. build_project() {
  209. print_message $BLUE "开始编译扩展服务项目..."
  210. # 编译monitor-admin
  211. print_message $YELLOW "编译 ruoyi-monitor-admin..."
  212. cd ../ruoyi-extend/ruoyi-monitor-admin
  213. mvn clean package -Pprod
  214. if [ $? -eq 0 ]; then
  215. print_message $GREEN "ruoyi-monitor-admin 编译成功"
  216. else
  217. print_message $RED "ruoyi-monitor-admin 编译失败"
  218. return 1
  219. fi
  220. # 编译snailjob-server
  221. print_message $YELLOW "编译 ruoyi-snailjob-server..."
  222. cd ../ruoyi-snailjob-server
  223. mvn clean package -Pprod
  224. if [ $? -eq 0 ]; then
  225. print_message $GREEN "ruoyi-snailjob-server 编译成功"
  226. else
  227. print_message $RED "ruoyi-snailjob-server 编译失败"
  228. return 1
  229. fi
  230. # 返回原目录
  231. cd ../../部署文档/杭州第一技师部署文档
  232. print_message $GREEN "所有扩展服务编译完成"
  233. }
  234. # 显示帮助信息
  235. show_help() {
  236. echo "扩展服务启动停止一体化脚本使用方法:"
  237. echo " $0 [命令] [服务名称]"
  238. echo ""
  239. echo "命令:"
  240. echo " start 启动服务"
  241. echo " stop 停止服务"
  242. echo " restart 重启服务"
  243. echo " status 查看服务状态"
  244. echo " build 编译项目"
  245. echo ""
  246. echo "服务名称:"
  247. echo " monitor 启动监控中心服务 (端口: 9090)"
  248. echo " snailjob 启动任务调度服务 (端口: 8800)"
  249. echo " all 操作所有服务"
  250. echo ""
  251. echo "示例:"
  252. echo " $0 start monitor # 启动监控中心服务"
  253. echo " $0 start snailjob # 启动任务调度服务"
  254. echo " $0 start all # 启动所有服务"
  255. echo " $0 stop all # 停止所有服务"
  256. echo " $0 status all # 查看所有服务状态"
  257. echo " $0 build # 编译所有扩展服务"
  258. echo ""
  259. echo "服务访问地址:"
  260. echo " 监控中心: http://localhost:9090/admin"
  261. echo " 任务调度: http://localhost:8800/snail-job"
  262. }
  263. # 主函数
  264. main() {
  265. local action=$1
  266. local service=$2
  267. # 检查参数
  268. if [ -z "$action" ]; then
  269. show_help
  270. exit 1
  271. fi
  272. case $action in
  273. start)
  274. if [ "$service" = "all" ]; then
  275. start_all
  276. elif [ -n "${SERVICES[$service]}" ]; then
  277. start_service $service
  278. else
  279. print_message $RED "无效的服务名称: $service"
  280. show_help
  281. exit 1
  282. fi
  283. ;;
  284. stop)
  285. if [ "$service" = "all" ]; then
  286. stop_all
  287. elif [ -n "${SERVICES[$service]}" ]; then
  288. stop_service $service
  289. else
  290. print_message $RED "无效的服务名称: $service"
  291. show_help
  292. exit 1
  293. fi
  294. ;;
  295. restart)
  296. if [ "$service" = "all" ]; then
  297. restart_all
  298. elif [ -n "${SERVICES[$service]}" ]; then
  299. restart_service $service
  300. else
  301. print_message $RED "无效的服务名称: $service"
  302. show_help
  303. exit 1
  304. fi
  305. ;;
  306. status)
  307. if [ "$service" = "all" ]; then
  308. status_all
  309. elif [ -n "${SERVICES[$service]}" ]; then
  310. status_service $service
  311. else
  312. print_message $RED "无效的服务名称: $service"
  313. show_help
  314. exit 1
  315. fi
  316. ;;
  317. build)
  318. build_project
  319. ;;
  320. *)
  321. print_message $RED "无效的命令: $action"
  322. show_help
  323. exit 1
  324. ;;
  325. esac
  326. }
  327. # 执行主函数
  328. main "$@"