SpringBoot 之热部署
默认情况下, 我们修改 class 或者 修改模板文件(templates目录 下面的文件) 等动态资源, 都不会立即自动生效。 在IDEA中, 我通过Ctrl + F9 , 仍然是无效。 当然, 静态资源的修改是可以立即更新的, 但是也是需要Ctrl + F9 编译一次, 另外前端浏览器需要F5刷新一遍。
有没有好的办法呢? 有!其实 boot 已经 提供了 devtools 这么一个工具。 在 pom.xml 的dependencies标签配置下面的内容后, 就可以了!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
对于 devtools , 我们还可以给设置optional 参数, 据说是可以提供编译速度:
<optional>true</optional>
有人说,还需要配置一个 fork:
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
- <fork>true</fork>
- </configuration>
- </plugin>
- </plugins>
我测试不是这样的, fork 是 true false 都不要紧。 fork的作用好像不是这个。 我看了官方文档是:
Flagging the dependency as optional is a best practice that prevents devtools from being transitively applied to other modules using your project
也就是说 避免 devtools 传递性的被应用到项目中其他的 module, 好像也不要紧,它仅仅是要给最佳实践。
devtools 还有一个作用是把 spring.thymeleaf.cache 之类的缓存功能给关闭了。
这样, 我们就可以随意修改java 源码和 动态资源文件了, 我测试过,新增删除java方法或者java 文件,或者 动态资源文件, 都是可以生效的。
但是, 在IDEA中, 我们需要 按Ctrl + F9 , 有没有办法自动编译呢? IDEA 默认不就是自动编译的吗? 为什么需要 Ctrl + F9? 有没有办法可以不 按Ctrl + F9呢, 当然也是可以的:
IDEA 配置
- CTRL + SHIFT + A 查找 勾选 make project automatically 选项
- ctrl+shift+alt+/ 查找Registry 勾选 compiler.automake.allow.when.app.running 选项
另外我观察到, 修改java 文件, 按Ctrl + F9 就相当于重启了一遍(观察控制台日志可知)。而模板文件不是这样的,它没有什么日志打印出来, 貌似是仅仅替换了那个文件。
而boot 为DevTools 也是提供了一些配置的。 这些配置可以控制 哪些修改 是否导致restart :
# DEVTOOLS (DevToolsProperties)
spring.devtools.livereload.enabled=true # Enable a livereload.com compatible server.
spring.devtools.livereload.port=35729 # Server port.
spring.devtools.restart.additional-exclude= # Additional patterns that should be excluded from triggering a full restart.
spring.devtools.restart.additional-paths= # Additional paths to watch for changes.
spring.devtools.restart.enabled=true # Enable automatic restart.
spring.devtools.restart.exclude=META-INF/maven/**,META-INF/resources/**,resources/**,static/**,public/**,templates/**,**/*Test.class,**/*Tests.class,git.properties # Patterns that should be excluded from triggering a full restart.
spring.devtools.restart.poll-interval=1000 # Amount of time (in milliseconds) to wait between polling for classpath changes.
spring.devtools.restart.quiet-period=400 # Amount of quiet time (in milliseconds) required without any classpath changes before a restart is triggered.
spring.devtools.restart.trigger-file= # Name of a specific file that when changed will trigger the restart check. If not specified any classpath file change will trigger the restart. # REMOTE DEVTOOLS (RemoteDevToolsProperties)
spring.devtools.remote.context-path=/.~~spring-boot!~ # Context path used to handle the remote connection.
spring.devtools.remote.debug.enabled=true # Enable remote debug support.
spring.devtools.remote.debug.local-port=8000 # Local remote debug server port.
spring.devtools.remote.proxy.host= # The host of the proxy to use to connect to the remote application.
spring.devtools.remote.proxy.port= # The port of the proxy to use to connect to the remote application.
spring.devtools.remote.restart.enabled=true # Enable remote restart.
spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support).
spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret.
默认改变 /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates 等目录文件,会重新重启项目?? ,当然我们编辑静态文件不想重启项目可以配置
spring.devtools.restart.exclude=static/**,public/**
我们可以通过:
spring.devtools.restart.enabled=false
禁止devtools 进行重启,但是,这样之后, 我们对java 文件的修改就不会生效,这时 Ctrl + F9 也没用, 因为class 文件不会替换。 所以,看来重启还是不可避免的。—— 不知道为什么devtools没有提供类似 jrebel 的class热加载的功能,通过重启来实现热加载实在是感觉有些low。
另外,我们可以设置 spring.devtools.restart.trigger-file= 属性。 它可以减少我们的重启次数(过多的重启也确实很烦哦),但是,这样之后,我们得记住那个特定的文件,当我们需要重启的时候,我们得手动修改它,或者插件修改。 手动修改的话,我试过,感觉还不如不用devtools 得了, 每次我想重启直接点击重启按钮不是一样的操作吗?(当然,devtools 多了个reload的功能) , 使用插件的话, 没找到什么好的插件。应该就是指 livereload ,jrebel之类的了吧。也感觉用起来不流畅。
总之,devtools 还是挺强大的,它提供了很多的配置。但仍有不足,期待能够集成jrebel变得更好。
参考:
https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#using-boot-devtools-restart
http://blog.csdn.net/isea533/article/details/70495714 这个完全就是上面官方文档的翻译。
http://www.logicbig.com/tutorials/spring-framework/spring-boot/trigger-file/
SpringBoot 之热部署的更多相关文章
- Springboot静态文件不更新的解决办法,以及Springboot实现热部署
Springboot静态文件不更新的解决办法,以及Springboot实现热部署 原文链接:https://www.cnblogs.com/blog5277/p/9271882.html 原文作者:博 ...
- idea+spring-boot+devtools热部署
idea+spring-boot+devtools热部署 标签: spring-boot 2017-03-20 14:45 2635人阅读 评论(1) 收藏 举报 分类: spring-boot m ...
- SpringBoot工程+热部署进行远程调试
本文转载自:https://blog.csdn.net/qq_31868349/article/details/78553901 SpringBoot工程+热部署进行远程调试 本地端添加配置 在pom ...
- springBoot开启热部署
springBoot开启热部署 这里使用devtools工具开启热部署 〇.搭建springbboot基础环境 一.添加依赖 <dependency> <groupId>org ...
- spring-boot项目热部署以及spring-devtools导致同类不能转换
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- SpringBoot工程热部署
SpringBoot工程热部署 1.在pom文件中添加热部署依赖 <!-- 热部署配置 --> <dependency> <groupId>org.springfr ...
- 从零开始学习springboot之热部署的配置
各位看官大家好,博主之前因为毕业设计以及毕业旅游耽搁了好长一段时间没有更新博客了,从今天起又会慢慢开始学习啦. 今天主要是来学习springboot热部署的配置. 一. 热部署 我们通常在修改某些文件 ...
- SpringBoot SpringCloud 热部署 热加载 热调试
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...
- springboot 配置热部署 及 热部署后依旧是404的坑
springboot配置热部署的教程网上一大堆: 个人喜欢这种方式: https://www.cnblogs.com/winner-0715/p/6666579.html 本文主要强调的是,大家如果配 ...
- springboot的热部署
SpringBoot 4.SpringBoot 整合 devtools 实现热部署 一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java. ...
随机推荐
- python: super原理
super() 的入门使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: ...
- LINK : fatal error LNK1104: cannot open file .exe' 重开application Experience 服务即可
这是一个坑, , 答案五花八门这个解决了我的痛点. 就这样了.
- 三、fgetc与fputc
fgetc 功能:从流中读取一个字符 原型:int fgetc(FILE *stream); 参数: stream:要读取的流指针 返回:读取到的字符,如果读完则返回EOF,EOF是end of fi ...
- ceph集群性能测试结果
对ceph存储集群(8台万兆服务器)从以下几个方面进行测试的结果 1.读写稳定性 无故障下的ceph集群性能完全满足业务对磁盘性能的需求. 测试数据结果如下表1-1,1-2 2.业务稳定性 ceph集 ...
- python2入门(2)
四.python条件语句 if语句基本语法if 判断条件: 执行语句块else if: 执行语句块else: 执行语句 五.循环语句 1 - while循环基本语法while 判断条件: 执行语句块w ...
- http/ftp等的URL匹配正则表达式 ZT
网上流传着多种匹配URL的正则表达式版本,但我经过试验,最好用的还是从stackoverflow上查到的: (https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_| ...
- JQuery 实现导航菜单的高亮显示
需求是这样的 点击不同的导航菜单实现当前点击的菜单是高亮的,点击导航下面的某个分类,分类所属的导航也必须是高亮的,点击某一篇文章,文章所属的导航菜单也必须是高亮的. 网上说的思路是这样的: 在菜单层的 ...
- MySQL配置文件my.ini或my.cnf的位置
1.Windows下MySQL的配置文件是my.ini,一般会在安装目录的根目录. 2.Linux下MySQL的配置文件是my.cnf,一般会放在/etc/my.cnf,/etc/mysql/my.c ...
- windows server 2012启动进入cmd解决方法
感谢网友http://sns.yhjy.cn/u/XperiaZ/Blog/t-4748 由于删除了framework 4.5引起的. windows server 2012默认安装framework ...
- Gym - 100796I:Shell Game(圆台的最大内接球半径)
pro:如题.给定上圆半径r,下圆半径R,高度h.问最大内接球半径. sol:由对称性,我们放到二维来看,即给这么一个梯形,问最大内接圆半径. 证明:如果是一个三角形的内接圆C,他内切于三边. 现在这 ...