"IDEA" code hot deployment and hot loading

Preface

In daily development, we need to frequently modify Java code, manually restart the project, and check the effect of the modification. If the project is small, the restart speed is faster and the waiting time is shorter. However, as the project gradually becomes larger, the restart speed becomes slower, and the waiting time of 1-2 minutes is more common.

We can use codehot loading and hot deployment to solve this problem. There are currently three main implementation solutions

  1. spring-boot-devtools [not recommended]
  2. IDEA comes with HowSwap function [recommended]
  3. JRebel plug-in [most recommended]

Strictly speaking, we need to distinguish between hot deployment and hot loading

Hot deployment redeploys the project while the server is running, that is, directly reloads the entire application. This method will free up memory and is cleaner and more thorough than hot reloading, but it is also more time-consuming.

Hot reloading reloads classes at runtime, thereby upgrading the application. The implementation principle of hot loading mainly relies on Java's class loading mechanism. The implementation method can be summarized as starting a background thread when the container is started, and regularly detecting changes in the timestamp of the class file. If the timestamp of the class changes, it will Class reloading. Compared with the reflection mechanism, reflection obtains class information at runtime and changes program behavior through dynamic calls; hot loading changes class information at runtime by reloading and directly changes program behavior.

spring-boot-devtools

Note: This method is too slow to reload and is not recommended.

pom.xmlAdd dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional> <!-- 可以防止将devtools依赖传递到其他模块中 -->
    </dependency>
</dependencies>

We can use IDEA in two ways to start

manual restart

Without any configuration, after we start the application, we can manually trigger the restart update (Ctrl+F9), or we can use mvn compile compile to trigger the restart update

Although this can reduce the loading time, it still needs to be started manually, which is inconvenient, so you can use the following automatic restart method

Automatic restart

First you need to follow the following stepsEnable IDEA runtime compilation

The configuration is as followsAllow automatic building of the project

image.png

Then configure againAllow runtime restart

image.png

Configurationapplication.yml

spring:
  devtools:
    restart:
      enabled: true  #设置开启热部署
      additional-paths: src/main/java #重启目录
      exclude: WEB-INF/**
  thymeleaf:
    cache: false #使用Thymeleaf模板引擎,关闭缓存

In fact, theoretically speaking, devtool itself is based on the restart method. This is still not a real hot replacement solution, so use it with caution in actual development.

If the overhead of automatic restart is not much different from manual restart, then it is better to restart manually (restart on demand)

In most cases, if it is a modification within a method or a modification of a static resource, it can be passed in IDEA< a i=3>Rebuild(Ctrl + Shift + F9) for hot update

HowSwap

This function is exclusive to IDEA Ultimate and does not support IDEA Community Edition.

Set the Spring Boot startup class and enable the HotSwap function. Then Debug runs the startup class and waits for the project to start.
Every time you modify the Java code, click the "Hot Loading" button in the lower left corner to achieve hot loading of the code.

image.png

image.png

IDEA comes with the HowSwap function, which has limited support and many modifications are not supported. for example:

  • Methods or fields can only be added but not reduced.
  • Visibility can only be increased but not decreased
  • You can only maintain the signature of existing methods but cannot modify them, etc.

To put it simply, it only supports hot loading of code modifications within methods, so if you want a relatively perfect solution, it is recommended to use the JRebel plug-in

Rebel

There are two ways to install, configure and activate JRebel:

method 1

Note: This method failed in actual measurement, please use method 2 (but method 2 needs to refer to method 1)

Install plug-ins directly from the IDEA plug-in market

image.png

image.png

After installing the JRebel plug-in, fill in the activation URL + generated GUID as the registration address, fill in the email address as you like, and then activate it directly

Activate the URL list, try to use the ones ranked first

http://jrebel-license.jiweichengzhu.com/{GUID}

https://jrebel.qekang.com/{GUID}

GUID can be generated online using the following address, and then just replace {GUID} above

GUID generation online link:Online GUID random generation tool-Bejson.com

You can enter your email address at will, select I agree, and finally submit.

image.png

Method 2

Download link:JRebel and XRebel - IntelliJ IDEs Plugin | Marketplace (jetbrains.com)

First manually download the  2022.4.1 version plug-in compressed package, and then manually download it from the disk in IDEA Install plug-ins

image.png

Restart after the installation is complete, and then follow the steps below. For the subsequentactivation URL and email address refer to method 1 to fill in

image.png

image.png

Then click the "Work Offline" button to set JRebel to offline to avoid activation failure due to network problems.

image.png

how to use

Note: Using JRebel also requires the configuration in settings in spring-boot-devtools above. If you do not configure it before, JRebel will automatically remind you when you run it for the first time and ask whether to turn it on automatically. Then click "Yes" to configure it automatically.

image.png

Every time we modify the code, it will be hot-loaded automatically after saving. Of course, if you want to update manuallyclick the refresh icon button

Reference link

This article is published by the blog post platform OpenWrite!

Guess you like

Origin blog.csdn.net/m0_63748493/article/details/134415070