Original: Little sister taste

A senior developer, using SpringBoot for the first time, decided to print a classic HelloWorld.

He was so excited that he mistyped a letter and became HalloWorld.

Hello? How vulgar how no grade, just like the mouth with a penang, can not vomit breath, absolutely need to be corrected.

As a result, after changing to Hello, Gao Gong found that the application had to be restarted to take effect. It took more than ten seconds for the system to dawdle into life.

Some things are good over time, but intolerable for this kind of code debugging scenario.

Gao Gong spit out his mouth and thought, it’s time to develop a tool to speed up debugging.

That’s where SBDT comes in.

spring-boot-devtools

SpringBoot, StringBuilder, StringBuffer SpringBoot, in particular, is very handy thanks to its Autoconfig, which regulates development by convention.

The problem is that SpringBoot loads so many jars that each boot takes too long. For the SpringBoot service, spring-boot-DevTools is a timely shower of rain for students staring blandishly at the reboot screen.

It’s been around for a long time, but I find that in real projects, people don’t use it much. But it’s very simple to use.

Just add the following JAR package to the project’s POM file to get the second-level service reloads (hot deployment).

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
Copy the code

Since the default value in the starter file is true, the following configuration in YML is not required.

spring:
  devtools:
    restart:
      enabled: true
Copy the code

So let’s verify that. It’s coming fast.

Create a new simple controller and export Halloworld.

@Controller
public class DemoController {
    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "halloworld";
    }}
Copy the code

Modify the code to change hallo to Hello. The console will start scrolling out the log and loading the project code. At this point, I visit the browser and see that our changes have taken effect.

Started MbyeApplication in 1.731 seconds (JVM running for 51.115)Copy the code

The console also outputs the restart time, which takes less than 2 seconds, which is pretty fast.

In order to trigger compilation in real time after code modification, you need to do the following configuration in IDEA. If this configuration does not work, manually click Build (note not rebuild).

 

Why is hot deployment reloading so fast? Because it doesn’t restart the entire application, it just restarts our application code.

By configuring the meta-INF /spring-devtools.properties file, you can specify that third-party JAR packages are loaded on each reboot. But it’s rare. Of course, include and exclude are included as shown in the following example.

restart.exclude.somejar=/somejar-[\\w-]+\.jar
restart.include.ajar=/ajar-[\\w-]+\.jar
Copy the code

I noticed an interesting thing. When we start with IDEA, the console output looks like this.

The 21:33:59 2020-09-18. 4635-495 the INFO/restartedMain C.G.J avarunfast. Mbye. MbyeApplication: Starting MbyeApplication on LYCYs-MacBook-Pro.local with PID 4635 (/target/classes started by xjjdog in / Users/xjjdog/codes / 21:33:59 javarunfast/mbye) 2020-09-18. 4635-495 the INFO [restartedMain] c.g.javarunfast.mbye.MbyeApplication : No active profile set, falling back to default profiles: Default 21:34:00 2020-09-18. 4635-355 the INFO [restartedMain] S.D.R.C.R epositoryConfigurationDelegate: Multiple Spring Data modules found, entering strict repository configuration mode! The 2020-09-18 21:34:00. 4635-355 the INFO [restartedMain]. S.D.R.C.R epositoryConfigurationDelegate: Bootstrapping Spring Data Elasticsearch repository in DEFAULT mode. 2020-09-18 21:34:00.357 INFO 4635 -- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data Repository scanning in 1ms. Found 0 Elasticsearch Repository interfaces. 2020-09-18 21:34:00 INFO 4635 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! The 2020-09-18 21:34:00. 4635-362 the INFO [restartedMain]. S.D.R.C.R epositoryConfigurationDelegate: Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.Copy the code

The starting thread inside is restartedMain. But when we start using Java -jar *jar, the main process is not restartedMain, but main.

This is because it doesn’t make sense to have DevTools on in an online environment.

That’s for now, because it hits you in the face.

More functions

To see what DevTools does, take a look at its source directory structure.

 

Filewatch and Classpath, of course, can be hot booted by listening for file changes. The principle is to use a separate ClassLoader (specifically RestartClassLoader) to complete the load replacement.

Look at this section of the code to get a better understanding of Java class loaders.


LiveReload

Next up is the Livereload feature.

LiveReload is used a lot when you’re doing front-end development.

Devtools also starts a LiveReload Server in the background, and the browser maintains a long connection to the Server. When a backend resource changes, the browser will be notified to refresh the Server for hot deployment.

Here is the Remote Live Reload plugin for Chrome. Install to have this cool feature.

https://chrome.google.com/webstore/detail/remotelivereload/jlppknnillhjgiengoigajegdpieppei?hl=en-GB
Copy the code

Remote deployment

This is much more interesting. We mentioned above that devTools is meaningless in an online environment, so let’s face it now.

You may have low performance on your own machine and have your code run remotely while you develop your code locally. At this point, remote hot deployment can be used.

There are a few steps to turn this feature on.

Step 1.

You need to make the following changes to the spring-boot-Maven-plugin in POM.xml.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludeDevtools>false</excludeDevtools>
    </configuration>
</plugin>
Copy the code

Step 2.

Set a key in YML to connect the server to the debugger.

spring:
  devtools:
    remote:
      secret: test
Copy the code

Step 3.

Package the SB service as a JAR and start it.

mvn -Dmaven.test.skip=true -Pdev package java -jar -Xdebug \ -Xrunjdwp:server=y,transport=dt_socket,suspend=n \ mbye 0.0.1 - the SNAPSHOT. The jarCopy the code

As you can see, we added a lot of parameters to boot, which is what it means to turn on remote molesting.

Step 4.

Edit a Java file in local IDEA and insert our server address (same as the application address) into the startup variable.

import org.springframework.boot.devtools.RemoteSpringApplication;
/**
 * @date 2020/09/19
 */
public class Remote {
    public static void main(String[] args) {
        RemoteSpringApplication.main(new String[]{"http://localhost:8080"});
    }}
Copy the code

Step 5.

Verify. Edit any file you want to see and click Build.

Below is a screenshot of the IDEA development side.

 

The following is a screenshot of the server. You can see that the service has been reloaded, but very quickly.

 

Listening for remote restart updates on /.~~spring-boot! ~/ start MbyeApplication in 1.961 seconds (JVM running for 249.452)Copy the code

Access the Web page and find that the code has been uploaded successfully.

End

In fact, spring-boot-DevTools is not the most powerful. Because it reloads the project’s class file each time using ClassLoader. If your project has too many files, it will be slow.

There’s an even better tool called JRebel, which is really a great tool for development. Even better, it works with any Java project, not just SpringBoot projects. It’s a bit heavy, though, and there’s a charge. We’ll talk about it next time, or maybe you’ve already tried it out.