springboot 热部署的实现

本文介绍了几种实现Java应用热部署的方法,包括使用spring-boot-devtools、JRebel插件及spring-loaded等工具,以提高开发效率。

在实际开发中,经常会遇到这样的问题:修改了一小段代码,必须重启应用服务器(如tomcat),才能使得修改生效。
在这种情况下,频繁的修改会导致我们的开发效率急剧下降。这时候我们就需要一种实时的加载工具,重新加载修改后的class文件或者重新部署更新后的项目,以达到 Hot Swapping 的目的。主要有以下几种解决方案:


一、使用【spring-boot-devtools】

1、添加Maven依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

当你将 devtools 的 optional 属性设置为 true ,在其他项目依赖当前项目的时候,devtools 的依赖不会被传递进去。具体可以参考maven optional

如果你使用的构建工具是Gradle,引入方式如下:

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}

2、配置maven-compiler插件的fork属性

这里写图片描述

当你使用 devtools 时,fork 属性会自动被配置成 true,表示process允许 fork。我们进行显式地配置以了解devtools的原理(devtools会fork一个线程去监视classpath下的file change事件,当得到file change的event时会使用Restarter重启springboot项目,这里的重启是比一般的重启快,具体可以参考devtools的双类加载器机制

配置如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
             <plugin>
        <plugins>
    </build>

3、配置自动编译

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart. In IntelliJ IDEA, building the project (Build -> Make Project) has the same effect.

假如你使用的IDE是IntelliJ IDEA,当我们修改了Java类后,IDEA默认是不自动编译的,而devtools又是监测classpath下的文件发生变化才会重启应用,所以你需要启用自动编译的功能(eclipse默认是开启的)。
操作如下:
      1)启用自动编译功能
这里写图片描述
      2)启动运行时编译
      键盘输入:shift + command +a(MAC),shift + ctrl + a(windows)
      输入:maintenance,选择registery,继而启用运行时编译的选项。
这里写图片描述

4、测试结果

修改类文件 –> 应用会重启
修改配置文件 –> 应用会重启
修改前端页面 –> 应用不会重启,但会重新加载修改的页面

With sensible “cache properties” and “automatic restarts” working, needing to manually click the browser refresh button each time something changes starts to become a little tedious. So to help save your mouse buttons, Spring Boot 1.3 DevTools includes an embedded LiveReload server. LiveReload is a simple protocol that allows your application to automatically trigger a browser refresh whenever things change. Browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.

前端页面的修改不会重启是因为devtools内嵌了一个LiveReload server,当资源发生改变时,会触发浏览器的自动刷新。

5、devtools的配置

建议:在开发的时候,我们尽量将spring.devtools.restart.enabled属性设置为flase,否则应用会频繁地重启。在进入测试的时候,我们将spring.devtools.restart.enabled重新设置为true,使得测试过程中的修改有效。


二、使用 JRebel 插件(收费)

JRebel is a productivity tool that allows developers to reload code changes instantly. It skips the rebuild, restart, and redeploy cycle common in Java development. JRebel enables developers to get more done in the same amount of time and stay in the flow while coding. JRebel supports a majority of real-world enterprise java stacks and is easy to install into existing development environments.

如果你使用的是 idea ,那么在plugins中搜索该插件,点击install安装,完成激活就可以正常使用。

操作如下:
这里写图片描述
因为这里我已经安装过了,所以显示的是uninstall。
安装完成后选择 help -》 JRebel -》Active,输入注册码,完成激活即可使用。
这里写图片描述


三、使用 spring-loaded

具体参考:https://blog.csdn.net/zemochen/article/details/53327518



参考资料:
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/maven-plugin/run-mojo.html

内容概要:本文介绍了基于ExtendSim软件构建的儿童保护模拟分析模型,旨在通过仿真技术评估不同工作包(Work Package)对儿童保护系统长期影响的效果。该模型自2014年起由澳大利亚某政府部门与Insight Acumen合作开发,用于支持政策决策,特别是在区域层面测试干预措施的实施效果。模型利用约数十万条历史数据自动生成概率分布(PD),模拟儿童在不同年龄、原住民身份、地区、初次或再次进入系统等条件下的路径发展。随着时间演进,模型经历了多次结构优化,从最初的7个区域调整为5个再扩展至6个,并实现了高度自动化,能够自动导入Excel原始数据并借助ModL编程语言完成初始化计算,显著提升了效率。目前模型每年提供74项关键指标输出,所有分析均由部门内部人员操作执行,大幅节省了人力成本并提高了测试与分析的有效性。; 适合人群:具备数据分析、系统建模背景,从事公共政策研究、社会服务管理或儿童福利领域的政府工作人员及咨询顾问。; 使用场景及目标:①评估儿童保护政策在不同区域和时间段的实施效果;②优化资源配置与工作包部署策略;③预测未来10-15年儿童保护系统的负荷与发展趋势;④提升政府部门在复杂社会系统中的决策科学性与响应效率。; 阅读建议:本模型强调实际应用与持续迭代,建议读者关注其数据自动化处理机制、ModL编程实现方式以及多维度概率建模方法,在复用时结合本地化数据结构进行适配与验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值