From 1647e0ce13b3586ef2ae558a4c5c8d64464ef05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E8=BE=89?= Date: Sat, 27 Jun 2026 16:24:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BB=A3=E7=A0=81=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ruoyi/RuoYiApplication.java | 2 +- .../ruoyi/web/core/config/DevToolsConfig.java | 24 ++++++ .../src/main/resources/application.yml | 7 ++ watch.sh | 73 +++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/DevToolsConfig.java create mode 100644 watch.sh diff --git a/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java b/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java index 4ee4001..c3dfdea 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java @@ -29,4 +29,4 @@ public class RuoYiApplication " | | \\ / \\ / \n" + " ''-' `'-' `-..-' "); } -} +} \ No newline at end of file diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/DevToolsConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/DevToolsConfig.java new file mode 100644 index 0000000..953ce14 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/DevToolsConfig.java @@ -0,0 +1,24 @@ +package com.ruoyi.web.core.config; + +import org.apache.catalina.connector.Connector; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +/** + * DevTools 热重启配置 - 修复 Windows 下端口未释放问题 + * 启用 SO_REUSEADDR,允许 DevTools 重启时立即复用端口 + * 仅在本地开发环境生效,不影响线上 + */ +@Profile("local") +@Configuration +public class DevToolsConfig implements WebServerFactoryCustomizer { + + @Override + public void customize(TomcatServletWebServerFactory factory) { + factory.addConnectorCustomizers((Connector connector) -> { + connector.setProperty("socket.soReuseAddress", "true"); + }); + } +} diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 5c0a451..f422b66 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -2,6 +2,8 @@ server: # 服务器的HTTP端口,默认为8080 port: 9091 + # 优雅关闭,防止 Windows 下 DevTools 重启时端口冲突 + shutdown: graceful servlet: # 应用的访问路径 context-path: / @@ -50,6 +52,11 @@ spring: restart: # 热部署开关 enabled: true + # 变更检测后的静默期(ms),给 IDE 编译留时间 + quiet-period: 400 + lifecycle: + # 优雅关闭超时 + timeout-per-shutdown-phase: 10s # redis 配置 # token配置 token: diff --git a/watch.sh b/watch.sh new file mode 100644 index 0000000..a0d1b8d --- /dev/null +++ b/watch.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# 石河子后端 自动重启脚本 +# VS Code 保存 → Java 扩展编译 → 本脚本检测变更 → 杀旧进程 → 起新进程 +# 用法: bash watch.sh + +SRC_DIR="$(cd "$(dirname "$0")" && pwd)" +cd "$SRC_DIR" + +APP_CLASS="com.ruoyi.RuoYiApplication" +PORT=9091 + +echo "================================================" +echo " SHZ Backend 自动重启模式" +echo " 流程: 保存文件 → Java扩展编译 → 检测变更 → 重启服务" +echo " 端口: $PORT" +echo " 按 Ctrl+C 退出" +echo "================================================" + +# 获取最新 .java 文件修改时间 +get_latest_mtime() { + find . -name "*.java" -path "*/src/*" -type f -printf '%T@\n' 2>/dev/null | sort -rn | head -1 | cut -d'.' -f1 +} + +# 精准杀后端进程 +kill_backend() { + local pid=$(jps -l 2>/dev/null | grep "$APP_CLASS" | awk '{print $1}') + if [ -n "$pid" ]; then + echo "[$(date '+%H:%M:%S')] 🔪 终止旧进程 PID=$pid..." + taskkill //PID "$pid" //F 2>/dev/null + sleep 3 + # 确认端口已释放 + while netstat -ano 2>/dev/null | grep -q ":$PORT "; do + sleep 2 + echo "[$(date '+%H:%M:%S')] ⏳ 等待端口 $PORT 释放..." + done + fi +} + +# 启动服务 +start_server() { + echo "[$(date '+%H:%M:%S')] 🚀 启动服务..." + cd "$SRC_DIR" + nohup mvn spring-boot:run -pl ruoyi-admin -DskipTests > /tmp/shz-backend.log 2>&1 & + echo "[$(date '+%H:%M:%S')] 等待服务就绪..." + for i in $(seq 1 90); do + sleep 2 + curl -s --max-time 2 "http://localhost:$PORT/" > /dev/null 2>&1 && \ + echo "[$(date '+%H:%M:%S')] ✅ 服务已就绪: http://localhost:$PORT/" && \ + return 0 + done + echo "[$(date '+%H:%M:%S')] ⚠️ 启动超时,查看 /tmp/shz-backend.log" +} + +# --- 主流程 --- + +# 首次启动 +kill_backend +start_server + +last_ts=$(get_latest_mtime) + +while true; do + sleep 3 + current_ts=$(get_latest_mtime) + + if [ -n "$current_ts" ] && [ -n "$last_ts" ] && [ "$current_ts" -gt "$last_ts" ]; then + echo "" + echo "[$(date '+%H:%M:%S')] 📝 检测到 .java 文件变更,重启服务..." + kill_backend + start_server + last_ts=$(get_latest_mtime) + fi +done