start-multi-instance.bat 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal enabledelayedexpansion
  4. REM 多实例启动脚本 (Windows版本)
  5. REM 使用方法: start-multi-instance.bat [start|stop|restart|status] [instance_name]
  6. REM 实例名称: 9900, 9901, 9902, 9903, all
  7. REM 配置变量
  8. set JAR_NAME=ruoyi-admin.jar
  9. set JAVA_OPTS=-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m
  10. set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai
  11. set JAVA_OPTS=%JAVA_OPTS% -XX:+UseG1GC -XX:MaxGCPauseMillis=200
  12. set JAVA_OPTS=%JAVA_OPTS% -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs\
  13. REM 日志目录
  14. if not exist logs mkdir logs
  15. REM 颜色输出函数
  16. :print_message
  17. set color=%1
  18. set message=%2
  19. if "%color%"=="RED" (
  20. echo [31m%message%[0m
  21. ) else if "%color%"=="GREEN" (
  22. echo [32m%message%[0m
  23. ) else if "%color%"=="YELLOW" (
  24. echo [33m%message%[0m
  25. ) else (
  26. echo %message%
  27. )
  28. goto :eof
  29. REM 检查JAR文件是否存在
  30. :check_jar
  31. if not exist "%JAR_NAME%" (
  32. call :print_message RED "错误: %JAR_NAME% 文件不存在!"
  33. call :print_message YELLOW "请确保在正确的目录下运行此脚本,或先执行 mvn clean package -Pprod 编译项目"
  34. pause
  35. exit /b 1
  36. )
  37. goto :eof
  38. REM 启动单个实例
  39. :start_instance
  40. set port=%1
  41. set profile=%2
  42. set pid_file=logs\ruoyi-admin-%port%.pid
  43. set log_file=logs\ruoyi-admin-%port%.log
  44. REM 检查是否已经运行
  45. if exist "%pid_file%" (
  46. for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
  47. tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
  48. if !errorlevel! equ 0 (
  49. call :print_message YELLOW "实例 %port% 已经在运行中 (PID: !pid!)"
  50. goto :eof
  51. ) else (
  52. REM PID文件存在但进程不存在,清理PID文件
  53. del "%pid_file%" 2>nul
  54. )
  55. )
  56. call :print_message GREEN "正在启动实例 %port% (配置文件: %profile%)..."
  57. REM 启动应用
  58. start /b java %JAVA_OPTS% -Dspring.profiles.active=%profile% -Dserver.port=%port% -jar %JAR_NAME% > "%log_file%" 2>&1
  59. REM 等待启动
  60. timeout /t 3 /nobreak >nul
  61. REM 查找新启动的进程
  62. for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq java.exe" /FO CSV ^| findstr /I "java.exe"') do (
  63. set new_pid=%%i
  64. set new_pid=!new_pid:"=!
  65. echo !new_pid! > "%pid_file%"
  66. call :print_message GREEN "实例 %port% 启动成功 (PID: !new_pid!)"
  67. call :print_message GREEN "访问地址: http://localhost:%port%"
  68. call :print_message GREEN "日志文件: %log_file%"
  69. goto :eof
  70. )
  71. call :print_message RED "实例 %port% 启动失败,请检查日志文件: %log_file%"
  72. if exist "%pid_file%" del "%pid_file%"
  73. goto :eof
  74. REM 停止单个实例
  75. :stop_instance
  76. set port=%1
  77. set pid_file=logs\ruoyi-admin-%port%.pid
  78. if exist "%pid_file%" (
  79. for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
  80. tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
  81. if !errorlevel! equ 0 (
  82. call :print_message YELLOW "正在停止实例 %port% (PID: !pid!)..."
  83. taskkill /PID !pid! /F >nul 2>&1
  84. del "%pid_file%" 2>nul
  85. call :print_message GREEN "实例 %port% 已停止"
  86. ) else (
  87. call :print_message YELLOW "实例 %port% 未运行"
  88. del "%pid_file%" 2>nul
  89. )
  90. ) else (
  91. call :print_message YELLOW "实例 %port% 未运行"
  92. )
  93. goto :eof
  94. REM 重启单个实例
  95. :restart_instance
  96. set port=%1
  97. call :print_message YELLOW "重启实例 %port%..."
  98. call :stop_instance %port%
  99. timeout /t 2 /nobreak >nul
  100. if "%port%"=="9900" call :start_instance %port% prod-9900
  101. if "%port%"=="9901" call :start_instance %port% prod-9901
  102. if "%port%"=="9902" call :start_instance %port% prod-9902
  103. if "%port%"=="9903" call :start_instance %port% prod-9903
  104. goto :eof
  105. REM 查看实例状态
  106. :status_instance
  107. set port=%1
  108. set pid_file=logs\ruoyi-admin-%port%.pid
  109. if exist "%pid_file%" (
  110. for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
  111. tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
  112. if !errorlevel! equ 0 (
  113. call :print_message GREEN "实例 %port%: 运行中 (PID: !pid!)"
  114. ) else (
  115. call :print_message RED "实例 %port%: 异常 (PID文件存在但进程不存在)"
  116. del "%pid_file%" 2>nul
  117. )
  118. ) else (
  119. call :print_message YELLOW "实例 %port%: 未运行"
  120. )
  121. goto :eof
  122. REM 启动所有实例
  123. :start_all
  124. call :print_message GREEN "启动所有实例..."
  125. call :start_instance 9900 prod-9900
  126. timeout /t 2 /nobreak >nul
  127. call :start_instance 9901 prod-9901
  128. timeout /t 2 /nobreak >nul
  129. call :start_instance 9902 prod-9902
  130. timeout /t 2 /nobreak >nul
  131. call :start_instance 9903 prod-9903
  132. goto :eof
  133. REM 停止所有实例
  134. :stop_all
  135. call :print_message YELLOW "停止所有实例..."
  136. call :stop_instance 9900
  137. call :stop_instance 9901
  138. call :stop_instance 9902
  139. call :stop_instance 9903
  140. goto :eof
  141. REM 重启所有实例
  142. :restart_all
  143. call :print_message YELLOW "重启所有实例..."
  144. call :stop_all
  145. timeout /t 3 /nobreak >nul
  146. call :start_all
  147. goto :eof
  148. REM 查看所有实例状态
  149. :status_all
  150. call :print_message GREEN "所有实例状态:"
  151. call :status_instance 9900
  152. call :status_instance 9901
  153. call :status_instance 9902
  154. call :status_instance 9903
  155. goto :eof
  156. REM 显示帮助信息
  157. :show_help
  158. echo 多实例启动脚本使用方法:
  159. echo %0 [命令] [实例名称]
  160. echo.
  161. echo 命令:
  162. echo start 启动实例
  163. echo stop 停止实例
  164. echo restart 重启实例
  165. echo status 查看实例状态
  166. echo.
  167. echo 实例名称:
  168. echo 9900 启动9900端口实例
  169. echo 9901 启动9901端口实例
  170. echo 9902 启动9902端口实例
  171. echo 9903 启动9903端口实例
  172. echo all 操作所有实例
  173. echo.
  174. echo 示例:
  175. echo %0 start 9900 # 启动9900端口实例
  176. echo %0 start all # 启动所有实例
  177. echo %0 stop 9900 # 停止9900端口实例
  178. echo %0 status all # 查看所有实例状态
  179. echo %0 restart all # 重启所有实例
  180. goto :eof
  181. REM 主函数
  182. :main
  183. set action=%1
  184. set instance=%2
  185. REM 检查参数
  186. if "%action%"=="" (
  187. call :show_help
  188. pause
  189. exit /b 1
  190. )
  191. REM 检查JAR文件
  192. call :check_jar
  193. if !errorlevel! neq 0 goto :eof
  194. if "%action%"=="start" (
  195. if "%instance%"=="all" (
  196. call :start_all
  197. ) else if "%instance%"=="9900" (
  198. call :start_instance 9900 prod-9900
  199. ) else if "%instance%"=="9901" (
  200. call :start_instance 9901 prod-9901
  201. ) else if "%instance%"=="9902" (
  202. call :start_instance 9902 prod-9902
  203. ) else if "%instance%"=="9903" (
  204. call :start_instance 9903 prod-9903
  205. ) else (
  206. call :print_message RED "无效的实例名称: %instance%"
  207. call :show_help
  208. pause
  209. exit /b 1
  210. )
  211. ) else if "%action%"=="stop" (
  212. if "%instance%"=="all" (
  213. call :stop_all
  214. ) else if "%instance%"=="9900" (
  215. call :stop_instance 9900
  216. ) else if "%instance%"=="9901" (
  217. call :stop_instance 9901
  218. ) else if "%instance%"=="9902" (
  219. call :stop_instance 9902
  220. ) else if "%instance%"=="9903" (
  221. call :stop_instance 9903
  222. ) else (
  223. call :print_message RED "无效的实例名称: %instance%"
  224. call :show_help
  225. pause
  226. exit /b 1
  227. )
  228. ) else if "%action%"=="restart" (
  229. if "%instance%"=="all" (
  230. call :restart_all
  231. ) else if "%instance%"=="9900" (
  232. call :restart_instance 9900
  233. ) else if "%instance%"=="9901" (
  234. call :restart_instance 9901
  235. ) else if "%instance%"=="9902" (
  236. call :restart_instance 9902
  237. ) else if "%instance%"=="9903" (
  238. call :restart_instance 9903
  239. ) else (
  240. call :print_message RED "无效的实例名称: %instance%"
  241. call :show_help
  242. pause
  243. exit /b 1
  244. )
  245. ) else if "%action%"=="status" (
  246. if "%instance%"=="all" (
  247. call :status_all
  248. ) else if "%instance%"=="9900" (
  249. call :status_instance 9900
  250. ) else if "%instance%"=="9901" (
  251. call :status_instance 9901
  252. ) else if "%instance%"=="9902" (
  253. call :status_instance 9902
  254. ) else if "%instance%"=="9903" (
  255. call :status_instance 9903
  256. ) else (
  257. call :print_message RED "无效的实例名称: %instance%"
  258. call :show_help
  259. pause
  260. exit /b 1
  261. )
  262. ) else (
  263. call :print_message RED "无效的命令: %action%"
  264. call :show_help
  265. pause
  266. exit /b 1
  267. )
  268. pause
  269. goto :eof