| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- @echo off
- chcp 65001 >nul
- setlocal enabledelayedexpansion
- REM 多实例启动脚本 (Windows版本)
- REM 使用方法: start-multi-instance.bat [start|stop|restart|status] [instance_name]
- REM 实例名称: 9900, 9901, 9902, 9903, all
- REM 配置变量
- set JAR_NAME=ruoyi-admin.jar
- set JAVA_OPTS=-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m
- set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai
- set JAVA_OPTS=%JAVA_OPTS% -XX:+UseG1GC -XX:MaxGCPauseMillis=200
- set JAVA_OPTS=%JAVA_OPTS% -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs\
- REM 日志目录
- if not exist logs mkdir logs
- REM 颜色输出函数
- :print_message
- set color=%1
- set message=%2
- if "%color%"=="RED" (
- echo [31m%message%[0m
- ) else if "%color%"=="GREEN" (
- echo [32m%message%[0m
- ) else if "%color%"=="YELLOW" (
- echo [33m%message%[0m
- ) else (
- echo %message%
- )
- goto :eof
- REM 检查JAR文件是否存在
- :check_jar
- if not exist "%JAR_NAME%" (
- call :print_message RED "错误: %JAR_NAME% 文件不存在!"
- call :print_message YELLOW "请确保在正确的目录下运行此脚本,或先执行 mvn clean package -Pprod 编译项目"
- pause
- exit /b 1
- )
- goto :eof
- REM 启动单个实例
- :start_instance
- set port=%1
- set profile=%2
- set pid_file=logs\ruoyi-admin-%port%.pid
- set log_file=logs\ruoyi-admin-%port%.log
- REM 检查是否已经运行
- if exist "%pid_file%" (
- for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
- tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
- if !errorlevel! equ 0 (
- call :print_message YELLOW "实例 %port% 已经在运行中 (PID: !pid!)"
- goto :eof
- ) else (
- REM PID文件存在但进程不存在,清理PID文件
- del "%pid_file%" 2>nul
- )
- )
- call :print_message GREEN "正在启动实例 %port% (配置文件: %profile%)..."
- REM 启动应用
- start /b java %JAVA_OPTS% -Dspring.profiles.active=%profile% -Dserver.port=%port% -jar %JAR_NAME% > "%log_file%" 2>&1
- REM 等待启动
- timeout /t 3 /nobreak >nul
- REM 查找新启动的进程
- for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq java.exe" /FO CSV ^| findstr /I "java.exe"') do (
- set new_pid=%%i
- set new_pid=!new_pid:"=!
- echo !new_pid! > "%pid_file%"
- call :print_message GREEN "实例 %port% 启动成功 (PID: !new_pid!)"
- call :print_message GREEN "访问地址: http://localhost:%port%"
- call :print_message GREEN "日志文件: %log_file%"
- goto :eof
- )
- call :print_message RED "实例 %port% 启动失败,请检查日志文件: %log_file%"
- if exist "%pid_file%" del "%pid_file%"
- goto :eof
- REM 停止单个实例
- :stop_instance
- set port=%1
- set pid_file=logs\ruoyi-admin-%port%.pid
- if exist "%pid_file%" (
- for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
- tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
- if !errorlevel! equ 0 (
- call :print_message YELLOW "正在停止实例 %port% (PID: !pid!)..."
- taskkill /PID !pid! /F >nul 2>&1
- del "%pid_file%" 2>nul
- call :print_message GREEN "实例 %port% 已停止"
- ) else (
- call :print_message YELLOW "实例 %port% 未运行"
- del "%pid_file%" 2>nul
- )
- ) else (
- call :print_message YELLOW "实例 %port% 未运行"
- )
- goto :eof
- REM 重启单个实例
- :restart_instance
- set port=%1
- call :print_message YELLOW "重启实例 %port%..."
- call :stop_instance %port%
- timeout /t 2 /nobreak >nul
- if "%port%"=="9900" call :start_instance %port% prod-9900
- if "%port%"=="9901" call :start_instance %port% prod-9901
- if "%port%"=="9902" call :start_instance %port% prod-9902
- if "%port%"=="9903" call :start_instance %port% prod-9903
- goto :eof
- REM 查看实例状态
- :status_instance
- set port=%1
- set pid_file=logs\ruoyi-admin-%port%.pid
- if exist "%pid_file%" (
- for /f "tokens=*" %%i in (%pid_file%) do set pid=%%i
- tasklist /FI "PID eq !pid!" 2>nul | find /I "!pid!" >nul
- if !errorlevel! equ 0 (
- call :print_message GREEN "实例 %port%: 运行中 (PID: !pid!)"
- ) else (
- call :print_message RED "实例 %port%: 异常 (PID文件存在但进程不存在)"
- del "%pid_file%" 2>nul
- )
- ) else (
- call :print_message YELLOW "实例 %port%: 未运行"
- )
- goto :eof
- REM 启动所有实例
- :start_all
- call :print_message GREEN "启动所有实例..."
- call :start_instance 9900 prod-9900
- timeout /t 2 /nobreak >nul
- call :start_instance 9901 prod-9901
- timeout /t 2 /nobreak >nul
- call :start_instance 9902 prod-9902
- timeout /t 2 /nobreak >nul
- call :start_instance 9903 prod-9903
- goto :eof
- REM 停止所有实例
- :stop_all
- call :print_message YELLOW "停止所有实例..."
- call :stop_instance 9900
- call :stop_instance 9901
- call :stop_instance 9902
- call :stop_instance 9903
- goto :eof
- REM 重启所有实例
- :restart_all
- call :print_message YELLOW "重启所有实例..."
- call :stop_all
- timeout /t 3 /nobreak >nul
- call :start_all
- goto :eof
- REM 查看所有实例状态
- :status_all
- call :print_message GREEN "所有实例状态:"
- call :status_instance 9900
- call :status_instance 9901
- call :status_instance 9902
- call :status_instance 9903
- goto :eof
- REM 显示帮助信息
- :show_help
- echo 多实例启动脚本使用方法:
- echo %0 [命令] [实例名称]
- echo.
- echo 命令:
- echo start 启动实例
- echo stop 停止实例
- echo restart 重启实例
- echo status 查看实例状态
- echo.
- echo 实例名称:
- echo 9900 启动9900端口实例
- echo 9901 启动9901端口实例
- echo 9902 启动9902端口实例
- echo 9903 启动9903端口实例
- echo all 操作所有实例
- echo.
- echo 示例:
- echo %0 start 9900 # 启动9900端口实例
- echo %0 start all # 启动所有实例
- echo %0 stop 9900 # 停止9900端口实例
- echo %0 status all # 查看所有实例状态
- echo %0 restart all # 重启所有实例
- goto :eof
- REM 主函数
- :main
- set action=%1
- set instance=%2
- REM 检查参数
- if "%action%"=="" (
- call :show_help
- pause
- exit /b 1
- )
- REM 检查JAR文件
- call :check_jar
- if !errorlevel! neq 0 goto :eof
- if "%action%"=="start" (
- if "%instance%"=="all" (
- call :start_all
- ) else if "%instance%"=="9900" (
- call :start_instance 9900 prod-9900
- ) else if "%instance%"=="9901" (
- call :start_instance 9901 prod-9901
- ) else if "%instance%"=="9902" (
- call :start_instance 9902 prod-9902
- ) else if "%instance%"=="9903" (
- call :start_instance 9903 prod-9903
- ) else (
- call :print_message RED "无效的实例名称: %instance%"
- call :show_help
- pause
- exit /b 1
- )
- ) else if "%action%"=="stop" (
- if "%instance%"=="all" (
- call :stop_all
- ) else if "%instance%"=="9900" (
- call :stop_instance 9900
- ) else if "%instance%"=="9901" (
- call :stop_instance 9901
- ) else if "%instance%"=="9902" (
- call :stop_instance 9902
- ) else if "%instance%"=="9903" (
- call :stop_instance 9903
- ) else (
- call :print_message RED "无效的实例名称: %instance%"
- call :show_help
- pause
- exit /b 1
- )
- ) else if "%action%"=="restart" (
- if "%instance%"=="all" (
- call :restart_all
- ) else if "%instance%"=="9900" (
- call :restart_instance 9900
- ) else if "%instance%"=="9901" (
- call :restart_instance 9901
- ) else if "%instance%"=="9902" (
- call :restart_instance 9902
- ) else if "%instance%"=="9903" (
- call :restart_instance 9903
- ) else (
- call :print_message RED "无效的实例名称: %instance%"
- call :show_help
- pause
- exit /b 1
- )
- ) else if "%action%"=="status" (
- if "%instance%"=="all" (
- call :status_all
- ) else if "%instance%"=="9900" (
- call :status_instance 9900
- ) else if "%instance%"=="9901" (
- call :status_instance 9901
- ) else if "%instance%"=="9902" (
- call :status_instance 9902
- ) else if "%instance%"=="9903" (
- call :status_instance 9903
- ) else (
- call :print_message RED "无效的实例名称: %instance%"
- call :show_help
- pause
- exit /b 1
- )
- ) else (
- call :print_message RED "无效的命令: %action%"
- call :show_help
- pause
- exit /b 1
- )
- pause
- goto :eof
|