一个基于Spring Boot 3的热补丁加载器,支持运行时动态替换Java类、Spring Bean和静态方法,无需重启应用即可修复线上紧急bug。
- 🔥 热补丁加载:支持运行时动态加载补丁,无需重启应用
- 🎯 多种替换类型:支持Spring Bean、普通Java类、静态方法替换
- 💻 管理界面:提供友好的Web管理界面
- Java 17+
- Maven 3.6+
- Spring Boot 3.2+
- 克隆项目
git clone <repository-url>
cd springboot-hot-patch- 构建项目
mvn clean package- 启动应用(带Java Agent)
java -javaagent:agent/springboot-hot-patch-1.0.0-agent.jar -jar springboot-hot-patch-1.0.0.jar- 访问管理界面
- 首页:http://localhost:8080
- 热补丁管理:http://localhost:8080/hotpatch
- 用户名/密码:admin/admin123
@HotPatch(
type = PatchType.SPRING_BEAN,
originalBean = "userService",
version = "1.0.1",
description = "修复getUserInfo空指针异常"
)
@Service
public class UserServicePatch {
public String getUserInfo(Long userId) {
if (userId == null) {
return "未知用户"; // 修复空指针问题
}
// ... 其他逻辑
}
}@HotPatch(
type = PatchType.STATIC_METHOD,
originalClass = "com.example.utils.MathHelper",
methodName = "divide",
version = "1.0.3",
description = "修复除零异常"
)
public class MathHelperDividePatch {
public static int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为零");
}
return a / b;
}
}# 编译补丁类
javac -cp "target/classes:lib/*" patches/UserServicePatch.java
# 打包为jar
jar cf UserService-1.0.1.jar -C target/classes patches/UserServicePatch.class
# 放到补丁目录
cp UserService-1.0.1.jar ./patches/- 打开 http://localhost:8080/index.html
- 选择补丁包
- 点击"加载补丁"按钮
curl -X POST "http://localhost:8080/api/hotpatch/load" \
-d "patchName=UserService&version=1.0.1"访问测试接口验证补丁是否生效:
# 测试用户服务
curl "http://localhost:8080/api/test/user"
# 热补丁配置
hotpatch.enabled=true
hotpatch.path=./patches
### JVM 启动参数
```bash
-javaagent:target/springboot-hot-patch-agent.jar
-XX:+UnlockDiagnosticVMOptions
-XX:+DebugNonSafepoints