Spring Boot 在 docker 容器内实时重新加载不起作用 [英] Spring Boot live reload inside a docker container not working

查看:58
本文介绍了Spring Boot 在 docker 容器内实时重新加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个在 docker 容器中运行的 Spring boot gradle 项目,它使用一个 docker 卷.Spring devtools 实时重新加载功能与以下属性一起使用.

We've a Spring boot gradle project running inside a docker container, which uses a docker volume. Spring devtools live reload feature is used with following properties.

spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=.
spring.devtools.restart.additional-exclude=src/main/java/**

我们正在使用 docker 卷来更改容器内的源文件.文件 src/main/resources/reload-trigger.txt 将在需要实时重新加载时更新.

We are using the docker volume to change the source files inside the container. The file src/main/resources/reload-trigger.txt will be updated whenever a live reload is needed.

容器日志显示重新加载有效,但更改没有影响.重新启动容器后,更改会反映出来.

The container logs shows that the reloading works, but the changes are not affecting. On restarting the container, the changes reflected.

例如步骤:

  • 在卷内创建一个控制器文件,使用 GET API,并更新触发文件
  • GET API 在邮递员中不起作用
  • 停止并启动容器
  • 现在 API 正在运行

Dockerfile

FROM gradle:5.6.2-jdk8
WORKDIR /app
COPY . /app
RUN ./gradlew getDeps
EXPOSE 8000
CMD ["gradle", "bootRun", "-PskipDownload=true"]

推荐答案

Spring Dev Tools 热重载机制仅从已经构建的类文件中重载/重启项目;它不会编译/构建源文件.因此,在 docker 容器中,即使源文件发生更改,Java 类文件也不会更改.因此,更改不会反映出来,并且不会发布新添加的 GET API.

The Spring Dev Tools hot reload mechanism only reloads/restarts the project from already built class files; it won't compile/build the source files. So inside a docker container, even if the source files are changed, the Java class files won't. Thus the change won't reflect and newly added GET API won't be published.

当容器重新启动时,它会再次调用 Dockerfile 中指定的 gradle bootRun.这会将更改后的 Java 文件构建到类文件中,并反映更改.

When the container is restarted, it again calls gradle bootRun as specified in Dockerfile. This will build the changed Java files into class files and the change will be reflected.

当我们在 IDE 中使用它时,IDE(默认情况下)会在源文件发生更改时构建源文件.所以 Spring 热重载总是加载更新的类文件,但这不会发生在 IDE 之外,除非我们有一些机制来观察源代码更改并构建它们,例如 gradle build --continuous

When we use this in an IDE, the IDE (by default) builds the source file whenever it is changed. So Spring hot reload always loads the updated class files, but this won't happen outside an IDE, unless we have some mechanism to watch source changes and build them, like gradle build --continuous

这些是我现在的设置.

application.properties

# scan for trigger file from root
# trigger file should not be in classpath, or it will go to infinite build loop
spring.devtools.restart.additional-paths=.
spring.devtools.restart.trigger-file=reload-trigger.txt

Dockerfile

FROM gradle:6.7-jdk11
WORKDIR /app
COPY . /app
EXPOSE 8000

# fetch dependencies
RUN chmod +x start.sh && gradle getDeps

# script which watches source file changes in background and executes bootRun
CMD ["sh", "start.sh"]

start.sh

这个文件应该在 root 中,因为 Dockerfile 不应该在类路径中

This file should be in root as per the Dockerfile should not be in classpath

# buildAndReload task, running in background, watches for source changes
(sleep 60; gradle buildAndReload --continuous -PskipDownload=true -x Test)&
gradle bootRun -PskipDownload=true

build.gradle

# -----other codes above----- #
task buildAndReload {
    dependsOn build
    mustRunAfter build    // buildAndReload must run after the source files are built into class files using build task
    doLast {
        new File(".", "reload-trigger.txt").text = "${System.currentTimeMillis()}" // update trigger file in root folder for hot reload
    }
}

通过这些设置,每当更改某些源文件时,都会执行 buildAndReload 任务.由于此任务依赖于 build 任务,更新的源代码在此之前被构建到类文件中.这个自定义任务,然后更新触发器文件,Spring 加载更新的类文件并重新启动应用程序.

With these settings, whenever some source files are changed, the buildAndReload task is being executed. As this task depends on build task, the updated source are built into class file before that. This custom task, then updates the trigger file and Spring loads the updated class files and restarts the application.

这篇关于Spring Boot 在 docker 容器内实时重新加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆