springboot 热部署的实现

在实际开发中,经常会遇到这样的问题:修改了一小段代码,必须重启应用服务器(如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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值