热部署的方法,Boot开发者工具

Spring Boot 配置 IDEA和DevTools 热陈设的方法,ideadevtools

MAVEN 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.globalrave</groupId>
  <artifactId>bar-web</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>bar-web</name>
  <description>Bar project for Spring Boot</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <!--微信-->
    <dependency>
      <groupId>com.github.sd4324530</groupId>
      <artifactId>fastweixin</artifactId>
      <version>1.3.15</version>
    </dependency>
    <!--mysql 驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--mybatis ORM-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!--热部署配置-->
        <configuration>
          <fork>true</fork>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 IDEA 配置

•CTRL + SHIFT + A 查找 勾选 make project automatically 选项

•ctrl+shift+alt+/ 查找Registry 勾选 compiler.automake.allow.when.app.running 选项

DevTools 配置

默许退换 /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates 等目录文件,会再也重启项目,当然我们编辑静态文件不想重启项目方可安插

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

# 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.

总结

以上所述是作者给我们介绍的Spring Boot 配置 IDEA和DevTools 热布署的办法,希望对我们有着辅助,若是大家有其余疑问招待给自己留言,作者会及时还原我们的!

Boot 配置 IDEA和DevTools 热安排的艺术,ideadevtools MAVEN 配置 xml version="1.0" encoding="UTF-8"project xmlns="" xmlns:xsi="htt...

Spring Boot为Maven和Gradle提供创设工具插件。

Spring Boot Maven Plugin,提供了应用Maven创设Spring Boot 工程的支持。大家能够用那一个插件完结打包功效。辅助打可试行jar包, war包。该插件帮衬Maven 3.2 +版本。

选拔办法如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- ... --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.3.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>

在我们的工程pom.xml文件之中配备上述代码就可以。

Spring Boot Maven Plugin提供的goals 如下:

  • repackage: 创造机关可奉行的jar包或war包。
  • run: 运营你的Spring Boot 应用,能够配备部分options,参数parameters.
  • start: 管理Spring Boot应用的营造生命周期,默许绑定集成测量试验阶段的pre-integration-test
  • stop : 管理Spring Boot应用的营造生命周期,暗中同意绑定集成测量检验阶段的post-integration-test
  • build-info: 生成Actuator的营造音信。对应的吩咐如下:
mvn spring-boot:repackagemvn spring-boot:runmvn spring-boot:startmvn spring-boot:stopmvn spring-boot:build-info

Spring Boot Gradle Plugin 提供了应用Gradl创设Spring Boot 应用的援救。一样帮助打可执行 jar包或war包。运维 Spring Boot应用时,使用的是spring-boot-dependencies提供的依附管理。

利用示例:

plugins { id 'org.springframework.boot' version '1.5.3.RELEASE'}

在您的build.gradle配置文件增多上述配置就可以。那几个看起来,比使用maven的plugins要精简多了。这里也是groovy的DSL。

利用方面包车型地铁安顿,Spring Boot Gradle Plugin会达成使用spring-boot-starter-parent bom加载注重的做事。

接下来,大家在dependencies里面一向像上面那样使用

dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.thymeleaf:thymeleaf-spring4") compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")}

若果您想要打war,加上上面那句

apply plugin: 'war'

运维命令:

gradle bootRun

spring-boot-devtools 是三个为开拓者服务的贰个模块,其中最关键的功力正是热安顿。

当大家修改了classpath下的公文(满含类公事、属性文件、页面等)时,会再一次起动应用(由于其应用的双类加载器机制,那些运转会非常的慢,另外也足以选用选拔jrebel)。

spring-boot-devtools使用了五个类加载器来促成重启机制:

base类加载器(base ClassLoader), restart类加载器(restart ClassLoader)。

  • base ClassLoader:用于加载不会改换的jar(eg.第三方重视的jar)
  • restart ClassLoader:用于加载我们正在开采的jar(eg.整个项目里我们本身编写的类)。当使用重启后,原先的restart ClassLoader被撇下、重新new一个restart ClassLoader来加载那个更换过的事物,而base ClassLoader却无需动一下。那便是devtools重启速度快的缘故。

使用devtools ,只要求增多其借助就可以 :

Maven

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

Gradle.

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

devtools的效应在指令行运营jar包

 java -jar XXX.jar 

依旧,当使用运维在内定的 classloader的时候, 自动失效(思量到,只怕是在生育蒙受)。

DevTools通过检查测量检验classpath的财富文件 resources的改变来触发应用的重启。那个跟大家在 英特尔liJ IDEA中, 使用Build -> Make Project,重新创设筑工程程的作用是平等的。

暗许意况下,

/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public

那些文件夹下的文书修改不会使利用重启,可是会重新加载(devtools内嵌了八个LiveReload server,当财富产生转移时,浏览器刷新)。

万一想改造暗许的装置,能够本身设置不重启的目录:

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

那样的话,就只有那多少个目录下的文件修改不会导致restart操作了。要是要在保留私下认可设置的根底上还要加上别的的铲除目录:

spring.devtools.restart.additional-exclude

假设想要使妥帖非classpath下的文书爆发变化时行使能够重启,使用:

spring.devtools.restart.additional-paths

如此devtools就能够将该目录列入了监听范围。

在application.properties文件中,关于DevTools的键值如下:

# ----------------------------------------# DEVTOOLS PROPERTIES# ----------------------------------------# 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.

另外,使用英特尔lij的可能会遇见那些题目,纵然项目选拔了spring-boot-devtools,修改了类照旧html、js等,idea依然不会自动重启,非要手动去make一下依旧重启,就更不曾动用热安插同样。出现这种景色,而不是您的铺排难点,其根本原因是因为英特尔lij IEDA和Eclipse分歧,Eclipse设置了自行编译之后,修改类它会活动编写翻译,而IDEA在非RUN或DEBUG情状下才会自行编译(前提是您早已设置了Auto-Compile)。

先是,IDEA设置里面Build project automatically这里打勾

图片 1

然后 Shift+Ctrl+Alt+/(Mac: Shift+Command+Alt+/),选择Registry

图片 2

进去以往,找到如下图所示的选项,打勾

图片 3

OK了,重启一下品种,然后改一下类里面包车型客车内容,IDEA就能活动去make了。

作者在采用maven-scala-plugin + spring-boot-devtools进度中,有个难题这里提一下。在spring-boot-devtools跟maven-scala-plugin一同使用,使用命令行

#!/usr/bin/env bashmvn clean scala:compile scala:run -Dlauncher=app

运营会报错(直接在IDEA中运维main入口类不报错,这是scala-maven-plugin跟spring-boot-devtools集成宽容性bug)。报错日志如下:

00:52:18.748 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'delegatingApplicationListener' 00:52:18.748 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Creating new Restarter for thread Thread[main,5,main] 00:52:18.749 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Immediately restarting application 00:52:18.749 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1bfafd6 00:52:18.749 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Starting application com.springboot.in.action.LightSwordApplication with URLs [file:/Users/jack/book/lightsword/target/test-classes/, file:/Users/jack/book/lightsword/target/classes/] 00:52:18.751 [restartedMain] INFO scala.App - Started App in 30.547 seconds (JVM running for 31.682) java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org_scala_tools_maven_executions.MainHelper.runMain(MainHelper.java:161) at org_scala_tools_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)Caused by: org.springframework.boot.devtools.restart.SilentExitExceptionHandler$SilentExitException at org.springframework.boot.devtools.restart.SilentExitExceptionHandler.exitCurrentThread(SilentExitExceptionHandler.java:90) at org.springframework.boot.devtools.restart.Restarter.immediateRestart(Restarter.java:183) at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:162) at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:545) at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartedEvent(RestartApplicationListener.java:68) at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:67) at org.springframework.boot.SpringApplicationRunListeners.started(SpringApplicationRunListeners.java:48) at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176) at com.springboot.in.action.LightSwordApplication$.delayedEndpoint$com$springboot$in$action$LightSwordApplication$1(LightSwordApplication.scala:6) at com.springboot.in.action.LightSwordApplication$delayedInit$body.apply(LightSwordApplication.scala:5) at scala.Function0.apply$mcV$sp(Function0.scala:34) at scala.Function0.apply$mcV$sp$(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App.$anonfun$main$1$adapted(App.scala:76) at scala.App$$Lambda$5/1464642111.apply(Unknown Source) at scala.collection.immutable.List.foreach(List.scala:389) at scala.App.main(App.scala:76) at scala.App.main$(App.scala:74) at com.springboot.in.action.LightSwordApplication$.main(LightSwordApplication.scala:5) at com.springboot.in.action.LightSwordApplication.main(LightSwordApplication.scala) ... 6 more

从日记内容,大家得以看看,系统在spring-boot-devtools的Restarter开首化的时候报错了。代码如下

 private void onApplicationStartedEvent(ApplicationStartedEvent event) { // It's too early to use the Spring environment but we should still allow // users to disable restart using a System property. String enabled = System.getProperty(ENABLED_PROPERTY); if (enabled == null || Boolean.parseBoolean { String[] args = event.getArgs(); DefaultRestartInitializer initializer = new DefaultRestartInitializer(); boolean restartOnInitialize = !AgentReloader.isActive(); Restarter.initialize(args, false, initializer, restartOnInitialize); } else { Restarter.disable(); } }

报错的代码在if分支

Restarter.initialize(args, false, initializer, restartOnInitialize);

身为,当大家尚无安顿spring.devtools.restart.enabled的值,可能值是true的时候,会步入那行代码。

绕过这么些荒唐的解决办法,是安排spring.devtools.restart.enabled的值是false。那样就用持续自动重启应用的功用。

package com.springboot.in.actionimport org.springframework.boot.SpringApplicationobject LightSwordApplication extends App { System.setProperty("spring.devtools.restart.enabled", "false") SpringApplication.run(classOf[AppConfig])}

由于spring-boot-devtools的完成原理是,在乎识代码有变动之后,重新起动应用。它利用了多少个ClassLoader,一个Classloader加载那多少个不会改换的类,另贰个ClassLoader加载会改造的类,称为 Restart ClassLoader, 这样在有代码改换的时候,原本的restart ClassLoader 被撇下,重新创建多少个restart ClassLoader,由于要求加载的类相非常少,所以完毕了异常快的重启时间。就是这么的落实机制,导致大家利用scala语言集成SpringBoot开采的时候,一齐行使scala-maven-plugin插件跟spring-boot-devtools的时候会报错。

鉴于对应的分级的语言的maven插件完结原理,比方说scala-maven-plugin:

在应用运营的时候,实行二次如下逻辑C:

先用其编写翻译api scalac, 把scala代码编写翻译成.class文件,然后调用ClassLoader对.class文件实行读写操作, 寻找classpath上边包车型客车类, 加载到jvm中。最终在jvm中执行.class字节码。

而后续的scala代码的改观,便未有实时调用到插件的逻辑C,动态编译成.class文件。所以,spring-boot-devtools的在监测动态更新ClassLoader的时候,不能够监测到scala代码的改动,也就无法达成机关心敬重启热布置了。要想完毕对应的scala集成SpringBoot热陈设,必要新鲜定制spring-boot-devtools-scala,监测scala代码更改,动态编写翻译scala代码到类路线。那样spring-boot-devtools就会监测到类的动态改换了。

一时候会遭遇一些难点:开拓条件是正规的,而线上情状是有标题,而此刻就要求中距离调节和测量试验来定位难点。

动用Spring Boot开辟应用程序,帮衬远程调试。 运转远程调节和测试,遵照如下配置就能够:

Maven

 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <jvmArguments> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 </jvmArguments> </configuration> </plugin>

Gradle

在build.gradle的bootRun职分里增添jvmArgs属性,如下配置

bootRun { jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"}

然后,在IDEA中开启远程debug就能够。在IDEA中的示例如下图

图片 4

越来越多关于spring-boot-devtools的效果与天性,能够参见[4]。

仿效资料:

1.

本文由365bet体育在线官网发布于365bet,转载请注明出处:热部署的方法,Boot开发者工具

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。