简体   繁体   English

Spring Boot 中的热交换

[英]Hot swapping in Spring Boot

I've been doing a POC with Spring Boot.我一直在用 Spring Boot 做 POC。

So far it's been going really good and promising, but there's one major drawback: I'm using an embedded server (ie, packaging the web app in a .jar ), so when developing I have to rebuild the jar and restart the server every time I change the CSS, HTML or JS files.到目前为止,它一直非常好,很有希望,但有一个主要缺点:我使用的是嵌入式服务器(即,将 web 应用程序打包在.jar ),因此在开发时,我必须重建 jar 并重新启动服务器每次当我更改 CSS、HTML 或 JS 文件时。 There's not hot-swap.没有热插拔。 This really slows down the UI development.这确实减慢了 UI 的开发速度。

I can think of several quick fixes, such as loading static resources off a different domain and serving it from a local nginx , and some more variations like this, but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?我可以想到几个快速修复,例如从不同的域加载静态资源并从本地nginx为其提供服务,以及更多类似的变体,但是在使用 IntelliJ/ 时没有某种内置选项蚀?

There are several options.有几种选择。 Running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also hotswapping of Java class changes).在 IDE 中运行(尤其是在调试时)是进行开发的好方法(所有现代 IDE 都允许重新加载静态资源,通常还可以热交换 Java 类更改)。 Spring Boot devtools is a cheap way to get quite a big boost (just add it to your classpath). Spring Boot devtools 是一种获得相当大提升的廉价方法(只需将其添加到您的类路径中)。 It works by restarting your application in a hot JVM when changes are detected.它的工作原理是在检测到更改时在热 JVM 中重新启动应用程序。 It also switches off things like thymeleaf caches while it is running, so you don't have to remember to do that yourself.它还会在运行时关闭 thymeleaf 缓存之类的东西,因此您不必记住自己这样做。 You can use it with an external css/js compiler process if you are writing that code with higher level tools.如果您使用更高级别的工具编写该代码,则可以将它与外部 css/js 编译器进程一起使用。

Spring Loaded is no longer recommended, but probably still in use.不再推荐使用Spring Loaded ,但可能仍在使用。 More sophisticated agent-based tools work much better if you need hot swapping with zero delay (eg JRebel).如果您需要零延迟热交换(例如 JRebel),则更复杂的基于代理的工具会更好地工作。

See the docs for some up to date content有关一些最新内容,请参阅文档

but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?但是在使用 IntelliJ/Eclipse 时是否有某种内置选项?

What helped me in IntelliJ 15.0, windows 10, was the following sequence:在 IntelliJ 15.0、Windows 10 中帮助我的是以下序列:

STEP 1: Added the following dependency in pom (This is mentioned everywhere but this alone dint solve it), as mentioned by @jonashackt第 1在 pom 中添加以下依赖项(到处都提到了这一点,但仅此一项就解决了),如@jonashackt 所述

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

STEP 2: Then from File->Settings-> Build-Execution-Deployment -> Compiler (make sure main compiler option is selected and not any of its sub-options)第 2然后从 File->Settings-> Build-Execution-Deployment -> Compiler(确保选择了主编译器选项而不是其任何子选项)

enable Make Project Automatically .启用自动生成项目 Click ok and close the dialog Note : In latest version it will be Build project automatically单击确定并关闭对话框注意:在最新版本中它将自动构建项目

STEP 3: Hold Shift + Ctrl + A (on windows) you will see a search dialog with title "Enter Action or option name", type registry.第 3按住Shift + Ctrl + A (在 Windows 上)您将看到一个标题为“输入操作或选项名称”的搜索对话框,键入注册表。 Double click the first option that says "Registry..." it will open another window.双击显示“注册表...”的第一个选项,它将打开另一个窗口。 Look for the following option:寻找以下选项:

compiler.automake.allow.when.app.running

and enable it, click close并启用它,单击关闭

STEP 4: Restart IDE第 4 步:重新启动 IDE

Elaborated from this source从这个来源详细说明

You can get hot swapping:您可以获得热插拔:

  • for java code: using spring-loaded对于java代码:使用spring-loaded
  • for Thymeleaf templates: disabling the cache对于 Thymeleaf 模板:禁用缓存

Check this post to see more details: http://blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/查看此帖子以查看更多详细信息: http : //blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/

I do not know how far this kind of support goes, but in case you use Eclipse IDE (or anyone reading this): starting up your Spring-Boot application via m2e in debug-mode (press the "Debug"-dropdown button and pick your maven run configuration item).我不知道这种支持有多远,但如果您使用 Eclipse IDE(或任何阅读本文的人):在调试模式下通过 m2e 启动您的 Spring-Boot 应用程序(按“调试”下拉按钮并选择您的 Maven 运行配置项)。

It works for me like a charm.它对我来说就像一种魅力。

My maven run configuration item is configured as follows:我的maven运行配置项配置如下:

  • goal is set to "spring-boot:run"目标设置为“spring-boot:run”
  • base directory is the project directory基目录是项目目录

I am not using any further libraries (not even spring-boot-devtools).没有使用任何其他库(甚至没有使用spring-boot-devtools)。

That's it.就是这样。

You can also use JRebel - it will reload all changes (better hotswap) including spring beans.您还可以使用 JRebel - 它会重新加载所有更改(更好的热交换),包括 spring bean。 It is easily integratred with both Intellij and Eclipse.它很容易与 Intellij 和 Eclipse 集成。

Assuming you are using gradle;假设您使用的是 gradle; use the following config in your build.gradle在 build.gradle 中使用以下配置

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'

applicationDefaultJvmArgs = ["-agentlib:jdwp=transport=dt_socket,address=localhost:7000,server=y,suspend=n"]

mainClassName = "package.ApplicationRunner"

Run the application from the IDE or command line using the command gradle build run使用命令gradle build run从 IDE 或命令行运行应用程序

Now the IDE can connect to the remote JVM (on port 7000) where the spring boot application runs.现在,IDE 可以连接到运行 spring boot 应用程序的远程 JVM(在端口 7000 上)。 It also supports hot deployment of static files.它还支持静态文件的热部署。

or even you can run the main class from intelliJ if the dependencies are properly managed in the IDE.或者,如果在 IDE 中正确管理依赖项,您甚至可以从 intelliJ 运行主类。 The main class is the class that contains the main method which will call SpringApplication.run("classpath:/applicationContext.xml", args);主类是包含将调用SpringApplication.run("classpath:/applicationContext.xml", args);的 main 方法的SpringApplication.run("classpath:/applicationContext.xml", args);

In Intellij, I can get this behavior.在 Intellij 中,我可以得到这种行为。 When the program is running in debug mode, select Run > Reload Changed Classes当程序在调试模式下运行时,选择Run > Reload Changed Classes

Note: After Intellij completes the action, it might say Loaded classes are up to date. Nothing to reload注意:Intellij 完成操作后,它可能会说Loaded classes are up to date. Nothing to reload Loaded classes are up to date. Nothing to reload . Loaded classes are up to date. Nothing to reload This is misleading, because it actually DID reload your classpath resources.这是一种误导,因为它实际上确实重新加载了您的类路径资源。

My environment/setup includes:我的环境/设置包括:
Intellij 13智能 13
Embedded Tomcat嵌入式Tomcat
Run/Debug configuration of type 'Application' (which just uses a main class) “应用程序”类型的运行/调试配置(仅使用主类)
Serving static html, css and js (no jsp)提供静态 html、css 和 js(无 jsp)

try using this spring-boot-devtools tag in pom.xml尝试在 pom.xml 中使用这个 spring-boot-devtools 标签

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

http://mytechnologythought.blogspot.com/2017/07/how-to-run-server-of-spring-boot-auto.html http://mytechnologythought.blogspot.com/2017/07/how-to-run-server-of-spring-boot-auto.html

I recommend Thymeleaf (template engine), jRebel for personal developer .我推荐Thymeleaf (模板引擎), jRebel 给个人开发者 Thymeleaf template files are just HTML resources. Thymeleaf 模板文件只是 HTML 资源。 So, they`re changed immediately after you edit template files.因此,它们会在您编辑模板文件后立即更改。

From 1.3.0.从 1.3.0. (now in Milestone 2) on, you can use the spring-boot-devtools for that as a lightweigt approach - see the docs or this blogpost . (现在在里程碑 2)上,您可以使用 spring-boot-devtools 作为一种轻量级的方法 - 请参阅文档这篇博文 Simply upgrade to >= 1.3.0.只需升级到 >= 1.3.0。 and add the following to your pom.xml:并将以下内容添加到您的 pom.xml 中:

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

Than start your SpringBootApplication with Run As... and you´re fine.比用 Run As 启动你的 SpringBootApplication... 你很好。

If you're using maven, the spring-boot-maven-plugin in your pom.xml needs to be like this to get the hot swap:如果您使用的是 maven,则 pom.xml 中的 spring-boot-maven-plugin 需要像这样才能获得热插拔:

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>

and if you're using thymeleaf, add this to your application properties:如果您使用的是百里香叶,请将其添加到您的应用程序属性中:

spring.thymeleaf.cache=false

But remember something: Don't use this in your production environment..但请记住:不要在您的生产环境中使用它。

How to perform Hot Swap in Springboot Application如何在 Springboot 应用程序中执行热插拔

  1. When using gradle include following in the dependency: compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.1.RELEASE' & providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')使用 gradle 时,在依赖项中包含以下内容: compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.1.RELEASE' & providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

  2. In application.properties add the property spring.devtools.restart.additional-paths=.application.properties 中添加属性spring.devtools.restart.additional-paths=.

  3. Build Gradle and then run application as bootRun构建 Gradle,然后将应用程序作为 bootRun 运行

The application is ready to perform hot swap on modification of classes应用程序已准备好对类的修改执行热插拔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM