SrpingBoot 热启动与热部署

2019-09-09 05:32:55

代码热部署


当我们已经启动了一个服务,然后修改代码之后,会自动重新部署。Spring项目通常有新内容修改后需要重新编译然后运行。通过配置Spring boot的热启动配置,可以实现自动编译重启项目,通常要比手动停止,启动项目快。


1、引入spring-boot-devtools

pom中添加响应的jar包及插件


    <!-- 热启动,热部署依赖包 -->

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-devtools</artifactId>

                <optional>true</optional>

            </dependency>

1

2

3

4

5

6

2、引入对应的插件


<!-- 热启动,热部署依赖插件 -->

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <fork>true</fork>

                </configuration>

            </plugin>

1

2

3

4

5

6

7

8

通过上面的2步,就可以实现代码的热部署了。

IDEA中 修改代码后需要通过 Ctrl+F9 重新编译


原理介绍


    spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。

    原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动还要更快,

    更快指的不是节省出来的手工操作的时间。

1

2

3

其深层原理是:


    使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),

    另一个ClassLoader加载会更改的类,称为 restart ClassLoader,

    这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,

    重新创建一个restart ClassLoader,由于需要加载的类相比较少,

    所以实现了较快的重启时间(5秒以内)。

1

2

3

4

5

部署方式:

热部署 :在服务器运行时重新部署项目,直接重新加载整个应用,更多是在生产环境使用

热加载:在运行时重新加载class,更多是在开发环境使用


热部署



                <dependencies>

                    <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>springloaded</artifactId>

                        <version>1.2.8.RELEASE</version>

                    </dependency>

                </dependencies>

1

2

3

4

5

6

7

mvn spring-boot:run


以上是spring boot 热部署的方式之一。

还有2种方式,一种是通过下载springbootloaded jar包,在vm中启动

另外一种 比较复杂可以通过加参数完成启动。


  1. 然后使用快捷键“Ctrl+Shift+A”,搜索“Registry”,如图所示。

    IntelliJ IDEA怎么热启动springboot项目

  2. 然后把如图所示的key进行勾选就可以了,如图所示。

    IntelliJ IDEA怎么热启动springboot项目



  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。

  • 2017-11-23 02:00:51

    js 分页插件twbs-pagination

    ​cdn地址 http://www.bootcdn.cn/twbs-pagination/ 官网地址 可以在cdn地址上面查看到