SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

When you use SpringBoot to develop applications, you need to restart the application to modify the code to take effect. If your app is large enough, it can take several minutes to start. Is there any way to speed up the startup process so that we can develop application code more efficiently? Spring-boot-devtools is an official hot deployment tool for SpringBoot. After modifying the code, you can restart the application quickly and automatically.

spring-boot-devtoolsIntroduction to the

SpringBoot is an official development tool for hot deployment and remote debugging if your application is integrated with it.

Realize the principle of

Why does the tool launch faster? Mainly because it uses two different class loaders. Base class loaders are used to load classes that do not change (such as classes in third-party libraries), and restart class loaders are used to load classes in your application. When the application starts, the classes in the restart classloader will be replaced, which means that a restart will be faster than a cold start!

Hot deployment

Next we’ll integrate DevTools to demonstrate hot deployment capabilities.

  • Need first in the projectpom.xmlFile, add devTools dependencies;
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
Copy the code
  • In order to facilitate testing, we added the following test interfaces in the project;
/** * Created by macro on 2021/3/25. */
@api (tags = "TestController", description = "SpringBoot Dev Tools test ")
@Controller
@RequestMapping("/test")
public class TestController {

    @apiOperation (" Test modify ")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first(a) {
        String message = "Return message";
        return CommonResult.success(null,message); }}Copy the code
  • Then start the project, after the success of the launch by Swagger access interface, return the result as follows, access to the address: http://localhost:8088/swagger-ui.html
{
  "code": 200."message": "Return message"."data": null
}
Copy the code
  • Since DevTools automatically restarts the project when it is built, and IDEA does not use automatic build by default, you can modify the application startup configuration to automatically build the project when IDEA loses focus.

  • Change the code in the Controller, just change itmessageVariables;
/** * Created by macro on 2021/3/25. */
@api (tags = "TestController", description = "SpringBoot Dev Tools test ")
@Controller
@RequestMapping("/test")
public class TestController {

    @apiOperation (" Test modify ")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first(a) {
        String message = "Return message (modified)";
        return CommonResult.success(null,message); }}Copy the code
  • After losing focus, waiting for the project to build automatically, the access interface has a 404 problem.
{
  "timestamp": "The 2021-03-29 T07:09:05. 415 + 00:00"."status": 404."error": "Not Found"."message": "No message available"."path": "/test/first"
}
Copy the code
  • Due to the difference between devTools detection time and the compilation time of IDEA, DevTools restarted the application before THE compilation of IDEA was completed, which caused this problem and was modifiedapplication.ymlTo the configuration file, add the following configuration.
spring:
  devtools:
    restart:
      poll-interval: 2s
      quiet-period: 1s
Copy the code
  • When you visit the test interface again, it shows the following, and the modified code has been automatically applied.
{
  "code": 200."message": "Return message (modified)"."data": null
}
Copy the code

Remote debugging

In addition to supporting hot deployment, DevTools also supports remote debugging, so let’s deploy the application into a Docker container and try remote debugging!

  • Since SpringBoot does not include DevTools by default, we need to change this firstpom.xml;
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <! -- Don't exclude Devtools-->
        <excludeDevtools>false</excludeDevtools>
    </configuration>
</plugin>
Copy the code
  • And then you need toapplication.ymlFile to add the devTools remote access password;
spring:
  devtools:
    remote:
      secret: macro666
Copy the code
  • Next, package the project into a Docker image and run it with the following command;
docker run -p 8088:8088 --name mall-tiny-devtools \ --link mysql:db \ -v /etc/localtime:/etc/localtime \ -v / mydata/app/mall - tiny/logs: / var/logs \ d mall - tiny/mall - tiny - devtools: 1.0 the SNAPSHOTCopy the code
  • Add a startup configuration and change the startup class toorg.springframework.boot.devtools.RemoteSpringApplication, the configuration information is as follows:

  • If the console displays the following information, the remote connection is successful.
The 2021-03-29 15:49:50. 7848-991 the INFO [main] O.S.B.D evtools. RemoteSpringApplication: Starting RemoteSpringApplication v2.0.3. RELEASE on DESKTOP-5NIMJ19 with PID 7848 2021-03-29 15:49:51.003 INFO 7848 -- [  main] o.s.b.devtools.RemoteSpringApplication : No active profile set, falling back to default profiles: Default 15:49:51 2021-03-29. 664 WARN 7848 - [the main] O.S.B.D.R.C.R emoteClientConfiguration: The connection to http://192.168.5.78:8088 is insecure. You should use a URL starting with 'https://'. 2021-03-29 15:49:52. 7848-024 the INFO [main] O.S.B.D.A.O ptionalLiveReloadServer: LiveReload Server is running on port 35729 2021-03-29 15:49:52.055 INFO 7848 -- [main] O.S.B.D evtools. RemoteSpringApplication: Started RemoteSpringApplication in 2.52 seconds (JVM running for 4.236)Copy the code
  • Now let’s change the test code in the Controller again, just change itmessageVariables;
/** * Created by macro on 2021/3/25. */
@api (tags = "TestController", description = "SpringBoot Dev Tools test ")
@Controller
@RequestMapping("/test")
public class TestController {

    @apiOperation (" Test modify ")
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult first(a) {
        String message = "Return message (remote debugging)";
        return CommonResult.success(null,message); }}Copy the code
  • If remote debugging is built automatically, it will cause the remote service to restart frequently. At this time, we can use IDEA to manually build, and the build button can be found in the right menu of the project.

  • After the build is successful, you can find that the remote service will restart automatically and apply the modified code. The following information is returned when accessing the test interface.
{
  "code": 200."message": "Return message (remote debugging)"."data": null
}
Copy the code

conclusion

While hot deployment is possible using SpringBoot’s official DevTools, this approach is more like a hot restart, and JRebel is available if you want a faster hot deployment experience.

Project source code address

Github.com/macrozheng/…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!