Spring Boot 2.0 Hot Deployment Guide

Source: Internet
Author: User

Spring Boot 2.0 supports hot deployment with a simple implementation method

Spring Boot 2.0 has several hot reload options. The recommended approach is to use Spring-boot-devtools

Because it provides additional development time features, such as support for fast application restarts and livereload, as well as reasonable development-time configuration (such as template caching). Devtools works by monitoring changes in classpath. This means that a static resource change must be "established" for the change to take effect. By default, this occurs automatically in eclipse when you save your changes. In IntelliJ idea, the Make Project command triggers the necessary build. Because of the default restart exclusion, changes to static resources do not trigger an application restart . However, they do trigger a real-time reload.

1. Static Resource Hot Deployment

If you are using a Thymeleaf template, add it directly in the Application.properties

Spring.thymeleaf.cache=false

If you are using a freemarker template, add it directly in the Application.properties

Spring.freemarker.cache=false

If you are using a Groovy template, add it directly in the Application.properties

Spring.groovy.template.cache=false
2. Spring-boot-devtools Java Thermal Deployment Configuration

The Spring-boot-devtools module contains support for automatic application restarts.

Although not as fast as jrebel, it is usually much faster than "cold start".

How to use:

Add maven Dependency

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

Or

Add Gradle Dependency

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

Summarize:

If you have modified the Java file by using the war method, you must restart Tomcat.

If you deploy using the Jar method,

    • Save in Eclipse for hot deployment
    • Ideal Indea needs rebuild project

Most commonly used configurations:

# whether to use the template cache spring.thymeleaf.cache=false#spring.freemarker.cache=false#spring.groovy.template.cache=false# If you do not want to use the restart feature, you can use the Spring.devtools.restart.enabled property to disable it spring.devtools.restart.enabled=true# when used with Livereload. The automatic restart works very well. spring.devtools.livereload.enabled=true# configuration monitor changes to other paths spring.devtools.restart.additional-paths=src/main/java# exclude only/ Static you will set the following: spring.devtools.restart.exclude=static/**
3. Spring-boot-devtools Hot Deployment Using 3.1 properties Default value

The Spring-boot-devtools module automatically applies a reasonable development-time configuration and does not require manual configuration.

Devtoolspropertydefaultspostprocessor

    static {map<string, object> devtoolsproperties = new hashmap<> ();        Devtoolsproperties.put ("Spring.thymeleaf.cache", "false");        Devtoolsproperties.put ("Spring.freemarker.cache", "false");        Devtoolsproperties.put ("Spring.groovy.template.cache", "false");        Devtoolsproperties.put ("Spring.mustache.cache", "false");        Devtoolsproperties.put ("Server.servlet.session.persistent", "true");        Devtoolsproperties.put ("spring.h2.console.enabled", "true");        Devtoolsproperties.put ("Spring.resources.cache.period", "0");        Devtoolsproperties.put ("Spring.resources.chain.cache", "false");        Devtoolsproperties.put ("Spring.template.provider.cache", "false");        Devtoolsproperties.put ("Spring.mvc.log-resolved-exception", "true");        Devtoolsproperties.put ("Server.servlet.jsp.init-parameters.development", "true");        Devtoolsproperties.put ("spring.reactor.stacktrace-mode.enabled", "true"); PROPERTIES = COLLECTIONS.UNMOdifiablemap (devtoolsproperties); }
3.2 Automatic restart

Applications that use Spring-boot-devtools will automatically restart whenever a file on the classpath changes. When working in the IDE, this can be a useful feature because it provides a very fast feedback loop for code changes. By default, any entry in the Classpath of a folder is monitored to make changes. Note that some resources, such as static assets and view templates, do not require the application to be restarted.

By default, only exclude/static and/public, you will set the following properties:

spring.devtools.restart.exclude=static/**,public/**

Tips; If you want to keep these default values and add additional exclusions, use the spring.devtools.restart.additional-exclude property instead

3.2.1 Trigger Restart

Because the Devtools monitors the CLASSPATH resource, the only way to trigger a restart is to update the CLASSPATH. How you can cause a classpath update depends on the IDE you use.

In eclipse, saving a modified file causes the Classpath to be updated and a restart is triggered.

In IntelliJ idea, build projects (Build > Build project) have the same effect.

3.2.2 Livereload used together

When used with Livereload, the automatic restart works very well.

If you use Jrebel, auto-restart is disabled to support dynamic class reloading. Other devtools features, such as Livereload and property overrides, can still be used.

Add the following configuration in Application.properties

Spring.devtools.livereload.enabled=true
    • Only one livereload server can be run at a time. Before you start the application, make sure that no other LIVERELOAD servers are running. If you launch multiple applications from the IDE, only the first application supports Livereload.
    • The Devtools relies on the closing hook of the application context to close it during a restart. If you disable the close hook (Springapplication.setregistershutdownhook (false)), it will not work correctly.
    • Devtools automatically ignores the name spring-boot,spring-boot-devtools,spring-boot-autoconfigure when determining whether an entry in the classpath triggers a restart when the change occurs. Spring-boot-actuator and Spring-boot-starter projects.
    • Devtools needs to customize the Resourceloader used by ApplicationContext. If your application has already provided one, it will be packaged. Direct overwrite of the GetResource method on ApplicationContext is not supported.

Restart vs Reload

The restart technology provided by Spring boot works by using two class loaders. Classes that do not change (for example, classes from third-party jars) are loaded into the base class loader. The class you are developing is loaded into the Restart class loader. When the application restarts, the Restart class loader is discarded and a new class is created. This approach means that application restarts are usually much faster than cold boot because the base class loader is already available and populated.

If you find that the restart is not fast enough for your application, or if you encounter a class load problem, you might consider reloading the technology from Zeroturnaround, such as Jrebel. These work by overriding classes because they are loaded, making them easier to reload.

3.3 Record the changes in the condition evaluation

By default, reports that display conditional evaluation increments are logged each time the application restarts. The report shows changes that are automatically configured for your application when you make changes, such as adding or removing beans and setting configuration properties.

To disable logging for reports, set the following properties:

Spring.devtools.restart.log-condition-evaluation-delta=false
3.4 Excluding resources

Some resources do not necessarily need to trigger a restart when they change. For example, you can edit the thymeleaf template in place. By default, resources in the change/meta-inf/maven,/meta-inf/resources,/resources,/static,/Public or/templates do not trigger a restart, but will trigger a real-time reload. If you want to customize these exclusions, you can use the Spring.devtools.restart.exclude property. For example, to exclude/static and/public only, you will set the following properties:

spring.devtools.restart.exclude=static/**,public/**

Tips: If you want to keep these default values and add additional exclusions, use the Spring.devtools.restart.additional-exclude property instead.

3.5 See the extra path

You may want to restart or reload the application when you change a file that is not in the classpath. To do this, use the Spring.devtools.restart.additional-paths property to configure additional paths to monitor your changes. You can use the Spring.devtools.restart.exclude property described earlier to control whether changes under other paths trigger a full restart or a real-time reload.

#配置监视其他路径的更改spring. Devtools.restart.additional-paths=src/main/java
3.6 Disabling the restart

If you do not want to use the restart feature, you can disable it by using the Spring.devtools.restart.enabled property.

In most cases, you can set this property in Application.properties (this will still initialize the restart ClassLoader, but it will not monitor file changes).

If you need to disable restart support completely (for example, because it does not work for a particular library), call Springapplication.run (... Before, you need to set the Spring.devtools.restart.enabled system property to False, as shown in the following example:

Public static void Main (string[] args) {    system.setproperty ("spring.devtools.restart.enabled", "false");    Springapplication.run (Myapp.class, args);}
3.7 Using the trigger file

If you use an IDE that continually compiles changes to files, you may want to trigger a restart only at a specific time. To do this, you can use the trigger file, which is a special file that you must modify when you want to actually trigger a restart check. Only changing the file triggers the check and restarts only if Devtools detects that certain actions must be performed. Trigger files can be updated manually or using IDE plug-ins.

To use a trigger file, set the Spring.devtools.restart.trigger-file property to the path of the trigger file

You can configure global devtools settings by adding a file named. spring-boot-devtools.properties to your home folder (note the file name with ". Start). Any attributes added to this file apply to all spring boot applications on computers that use Devtools. For example, to configure a restart to always use a trigger file, you can add the following properties:

~/.spring-boot-devtools.properties.

Spring.devtools.reload.trigger-file=.reloadtrigger

You may need to set Spring.devtools.restart.trigger-file to global settings so that all items behave the same way.

3.8 Custom Restart Class loader

As described in the previous "Restart vs Reload" section, the restart function is implemented by using two ClassLoader. For most applications, this approach works well, but it can sometimes cause class loading problems.

By default, any open project in the IDE loads the "restart" ClassLoader, and any regular. jar files will load the "base" ClassLoader. If you are using multi-module projects, and not every module is imported into the IDE, you may need to customize it. To do this, you can create a meta-inf/spring-devtools.properties file.

The spring-devtools.properties file can contain attributes prefixed with restart.exclude and restart.include. The Include element is an item that should be pulled into the "restart" ClassLoader, while the exclusion element is an item that should be pushed down to the "basic" ClassLoader. The value of this property is the regular expression pattern that is applied to the classpath, as shown in the following example:

restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jarrestart.include.projectcommon=/mycorp-myproj-[\\ W-]+\.jar

All property keys must be unique. As long as the property starts with Restart.include. or restart. Exclude. has been considered.

Loads all the meta-inf/spring-devtools.properties in the classpath. You can package files into your project, or you can package them in a library that your project uses.

3.9 Notable restrictions

For objects that are deserialized using standard ObjectInputStream, the restart feature does not work correctly.

If you need to deserialize data, you may need to use spring's Configurableobjectinputstream and Thread.CurrentThread () together. Getcontextclassloader ().

Unfortunately, some third-party libraries are deserialized without regard to the context class loader. If you find such a problem, you need to request a fix from the original author.

Spring Boot 2.0 Hot Deployment Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.