如何使用 java-ee8、microProfile 4.0、Openliberty 21 和 Docker 设置特定于阶段的微配置文件配置

Posted

技术标签:

【中文标题】如何使用 java-ee8、microProfile 4.0、Openliberty 21 和 Docker 设置特定于阶段的微配置文件配置【英文标题】:How to setup stage-specific microprofile config with java-ee8, microProfile 4.0, Openliberty 21 and Docker 【发布时间】:2021-09-02 04:46:48 【问题描述】:

我的应用程序在 docker 容器内使用 JavaEE8、Openliberty 21.0.0.5 和 Microprofile 4.0 时遇到问题。 我无法从图像“外部”设置 mpConfig-2.0-introduced mp.config.profile。 我的设置如下:

microprofile-config.properties
microprofile-config-dev.properties
microprofile-config-prod.properties

src/main/resources/META-INF/

功能在 server.xml 中正确激活,并在启动时正确安装。

<featureManager onError="WARN">
    <feature>microProfile-4.0</feature>
    <feature>javaee-8.0</feature>
</featureManager>

每个阶段我还有一个 server.env,它通过 docker run 的 --env-file 传递给我的容器。

我的第一种方法是在server.env 中设置mp.config.profile,因为它可以 在不同阶段运行图像时发生更改。 f.e. mp.config.profile=devserver-dev.env 等等。 但是 microprofile-config-dev.properties 的值从不用于此设置。

我能够正确激活配置文件的唯一方法是在使用时

<variable name="mp.config.profile" value="dev" />

server.xml内。

但是,server.xml 在 docker 映像中不是特定于阶段的,因此无法在不同阶段更改它。

有没有人建议这样做的正确方法是什么?

提前致谢

问候

【问题讨论】:

【参考方案1】:

如果您想从server.env 设置mp.config.profile,您可能需要改为设置MP_CONFIG_PROFILE

由于点在环境变量名称中无效,MP Config defines some mapping rules 从环境变量中查找配置时。它将检查请求的名称,其中非字母数字字符转换为下划线,然后再次将所有字母转换为大写。

【讨论】:

我很确定我已经尝试过带下划线的小写和大写,但显然我在尝试时遇到了其他一些错误配置。无论如何,它现在有效。谢谢!

以上是关于如何使用 java-ee8、microProfile 4.0、Openliberty 21 和 Docker 设置特定于阶段的微配置文件配置的主要内容,如果未能解决你的问题,请参考以下文章

如何正确关闭 java-ee websocket 连接

MicroProfile 的配置:如何注入自定义对象列表

如何将配置中的集合转换为 Microprofile/Quarkus/Smallrye 中的环境变量

Microprofile 的 Open Liberty 实现中的 MySql 连接

使用 mp.config.profile 设置 Microprofile 活动配置文件在 Liberty 中不起作用

Microprofile Config:容错度量标签的动态 ConfigSource 值