spring-boot - CNB 启动器未拾取 Spring Boot OCI 镜像的环境属性

标签 spring-boot docker gradle-kotlin-dsl buildpack

我正在构建一个 Spring Boot 3.1.0 应用程序,并通过 Gradle Kotlin DSL 构建脚本中的 Cloud Native Buildpack 来部署它。为了调整一些图像属性,我在 build.gradle.kts 脚本中设置以下内容:

tasks.named<BootBuildImage>("bootBuildImage") {
    verboseLogging.set(true)
    // ...
    environment.set(mapOf(
        "BPL_JMX_ENABLED" to "true",        
        "BPL_DEBUG_ENABLED" to "true",  
        "BPL_JAVA_NMT_ENABLED" to "false",
        "BPL_SPRING_CLOUD_BINDINGS_DISABLED" to "true"
    ))
}

我什至在构建日志中看到这些属性在构建阶段被识别:

...
[creator]    Launch Configuration:           
[creator]      $BPL_DEBUG_ENABLED       true         enables Java remote debugging support
[creator]      $BPL_DEBUG_PORT          8000         configure the remote debugging port
...
[creator]    Launch Configuration:
[creator]      $BPL_SPRING_CLOUD_BINDINGS_DISABLED  true   whether to auto-configure Spring Boot environment properties from bindings
...

但是,当应用程序作为 Docker 容器启动时,启动器不会获取任何属性。为了清楚起见,以下是容器标准输出的摘录,其中带有手动换行符:

Spring Cloud Bindings Enabled
2023-05-20T07:48:25.857843881Z Picked up JAVA_TOOL_OPTIONS: 
-Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties 
-XX:+ExitOnOutOfMemoryError 
-XX:ActiveProcessorCount=1 
-XX:MaxDirectMemorySize=10M 
-Xmx31494K 
-XX:MaxMetaspaceSize=97109K 
-XX:ReservedCodeCacheSize=240M 
-Xss1M 
-XX:+UnlockDiagnosticVMOptions 
-XX:NativeMemoryTracking=summary 
-XX:+PrintNMTStatistics 
-Dorg.springframework.cloud.bindings.boot.enable=true
2023-05-20T07:48:28.285693854Z 

我希望在这里看到“Spring Cloud Bindings Disabled”和一个 JVM 选项,如 -agentlib:jdwp=transport=dt_socket,server=y,suspend =n,地址=*:8000。因此,没有打开任何所需的端口。

在构建日志和运行日志中均未观察到任何错误或警告。

我如何了解出了什么问题以及如何解决?

最佳答案

two types of environment variable configuration properties that Paketo buildpacks understand :构建时间和启动时间(也称为运行时间)。

  • 构建时环境配置设置均以 BP_ 开头。
  • 启动时环境配置设置均以 BPL_ 开头。

您可以将构建时属性设置为传递给 pack (pack build -e ..) 或 Spring Boot 构建工具的环境变量,就像您在此处所做的那样。

要设置启动时间配置,您需要使用您选择的容器运行时来设置这些环境变量。这可能是 Docker、Kubernetes、CloudFoundry 等。

在本例中,您使用的是 docker run,因此您需要添加 -e BPL_JMX_ENABLED=true -e BPL_DEBUG_ENABLED=true -e BPL_JAVA_NMT_ENABLED=true -e BPL_SPRING_CLOUD_BINDINGS_DISABLED=true.

存在差异的原因是您可以打开/关闭这些设置,而无需重建容器。

如果您希望将这些设置烘焙到容器镜像中,以便它们是自动的,您可以这样做。 process is defined here .

简而言之,您可以像这样修改配置:

    environment.set(mapOf(
        "BPE_DEFAULT_BPL_JMX_ENABLED" to "true",        
        "BPE_DEFAULT_BPL_DEBUG_ENABLED" to "true",  
        "BPE_DEFAULT_BPL_JAVA_NMT_ENABLED" to "false",
        "BPE_DEFAULT_BPL_SPRING_CLOUD_BINDINGS_DISABLED" to "true"
    ))

然后当你构建时,这些环境变量将自动激活。您不需要将它们包含在运行时配置中。

正如我上面提到的缺点是,当您想要更改图像时,您需要重建图像。

关于spring-boot - CNB 启动器未拾取 Spring Boot OCI 镜像的环境属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76294215/

相关文章:

java - Spring Boot 应用程序类可以扩展什么?

java - 如何在拥有 html、css 和 js 文件的情况下构建 Angular6 项目

docker - 在内容更改时重新加载Nginx容器

docker - 启动 docker 实例的问题

node.js - 如何使用nginx作为s3 aws的代理?

android - 使用 maven 插件和 kotlin dsl 发布 aar

java - 两次 Web 服务调用之间响应实体主体丢失

java - Mod_Cluster LifecycleListeners Spring Boot

spring-boot - 使用Apache Jmeter Core时出现Stackoverflow错误

gradle - 未找到 Kotlin Gradle 脚本中导入外部类