第 25 章 Eclipse MicroProfile
25.1. 使用 Eclipse MicroProfile 配置管理配置 复制链接链接已复制到粘贴板!
Eclipse Microprofile Config 仅作为技术预览提供。技术预览功能不包括在红帽生产服务级别协议(SLA)中,且其功能可能并不完善。因此,红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。
如需有关技术预览功能支持范围的信息,请参阅红帽客户门户网站中的技术预览功能支持范围。
25.1.1. 关于 MicroProfile Config SmallRye 子系统 复制链接链接已复制到粘贴板!
配置数据可以动态变化,应用需要能够在不重新启动服务器的情况下访问最新的配置信息。Eclipse MicroProfile 配置提供了配置数据的可移植外部化。这使得应用和微服务可以配置为在多个环境中运行,无需修改或重新打包。Eclipse MicroProfile 配置功能在 JBoss EAP 中使用 SmallRye Config 组件实施,由 microprofile-config-smallrye 子系统提供。此子系统包含在默认的 JBoss EAP 7.3 配置中。
MicroProfile 配置属性可以是不同格式的,可以来自不同位置,由 ConfigSources 提供,它们是 org.eclipse.microprofile.config.spi.ConfigSource 接口的实施。
MicroProfile 配置规范提供下列默认 ConfigSource 实施,用于检索配置值:
-
使用
System.getProperties(). -
使用
System.getenv(). -
来自类路径上的所有
META-INF/microprofile-config.properties文件。
The microprofile-config-smallrye 子系统支持以下额外类型的 ConfigSource 资源,用于检索配置值:
- (从属性中 )。
- 以及目录中的一个文件。
-
(来自
ConfigSource类 )。 -
(来自
ConfigSourceProvider类 )。
虽然以下部分使用管理 CLI 配置 microprofile-config-smallrye 子系统,但您也可以通过导航到 Configuration
如需可用于配置 MicroProfile Config SmallRye 子系统的属性列表,请参阅 MicroProfile Config SmallRye 子系统。
从属性获取 ConfigSource 配置
您可以通过添加 config-source 属性并将属性指定为列表,直接存储 ConfigSource 的属性 以供访问。
以下管理 CLI 命令创建一个 ConfigSource,其中包含 property1 和 properties 2 属性 的配置数据。
/subsystem=microprofile-config-smallrye/config-source=props:add(properties={property1=value1,property2=value2})
/subsystem=microprofile-config-smallrye/config-source=props:add(properties={property1=value1,property2=value2})
此命令将为 microprofile-config-smallrye 子系统生成以下 XML 配置:
从目录获取 ConfigSource 配置
您可以通过添加 config-source 属性并指定包含文件的目录来创建 ConfigSource 以从目录中的文件读取属性。dir 目录中每个文件的名称是属性的名称,文件内容则是 属性的值,
以下管理 CLI 命令创建一个 ConfigSource,它将从 /etc/config/numbers-app/ 目录中的文件读取配置属性。
/subsystem=microprofile-config-smallrye/config-source=file-props:add(dir={path=/etc/config/numbers-app})
/subsystem=microprofile-config-smallrye/config-source=file-props:add(dir={path=/etc/config/numbers-app})
此命令将为 microprofile-config-smallrye 子系统生成以下 XML 配置:
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source name="file-props">
<dir path="/etc/config/numbers-app"/>
</config-source>
</subsystem>
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source name="file-props">
<dir path="/etc/config/numbers-app"/>
</config-source>
</subsystem>
此结构与 OpenShift ConfigMap 使用的结构对应。The dir 值对应于 OpenShift 或 Kubernetes 中的 ConfigMap 定义中的 mountPath。
JBoss EAP 中部署的任何应用能够以编程方式访问 目录中存储的属性。
假设目录 /etc/config/numbers-app/ 包含以下两个文件:
-
num.size:此文件包含值5。 -
num.max:此文件包含值100。
在下面的代码示例中,num.size 返回 5 and num.max 返回 100。
从 ConfigSource 类获取 ConfigSource 配置
您可以创建和配置自定义 org.eclipse.microprofile.config.spi.ConfigSource 实施类,以提供配置值的来源。
以下管理 CLI 命令为名为 org.example.MyConfig ,它由名为 Source 的实施类创建一个 Config Sourceorg.example 的 JBoss 模块提供。
/subsystem=microprofile-config-smallrye/config-source=my-config-source:add(class={name=org.example.MyConfigSource, module=org.example})
/subsystem=microprofile-config-smallrye/config-source=my-config-source:add(class={name=org.example.MyConfigSource, module=org.example})
此命令将为 microprofile-config-smallrye 子系统生成以下 XML 配置:
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source name="my-config-source">
<class name="org.example.MyConfigSource" module="org.example"/>
</config-source>
</subsystem>
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source name="my-config-source">
<class name="org.example.MyConfigSource" module="org.example"/>
</config-source>
</subsystem>
此 ConfigSource 类提供的属性可用于任何 JBoss EAP 部署。
有关如何将全局模块添加到 JBoss EAP 服务器的详情,请参考定义全局模块。
从 ConfigSourceProvider 类获取 ConfigSource 配置
您可以创建和配置自定义 org.eclipse.microprofile.config.spi.ConfigSourceProvider 实施类,以注册多个 ConfigSource 实例的实施。
以下管理 CLI 命令为名为 org.example.MyConfigSourceProvider 的实施类创建一个 config-source-provider,该类由名为 org.example 的 JBoss 模块提供。
/subsystem=microprofile-config-smallrye/config-source-provider=my-config-source-provider:add(class={name=org.example.MyConfigSourceProvider, module=org.example})
/subsystem=microprofile-config-smallrye/config-source-provider=my-config-source-provider:add(class={name=org.example.MyConfigSourceProvider, module=org.example})
此命令将为 microprofile-config-smallrye 子系统生成以下 XML 配置:
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source-provider name="my-config-source-provider">
<class name="org.example.MyConfigSourceProvider" module="org.example"/>
</config-source-provider>
</subsystem>
<subsystem xmlns="urn:wildfly:microprofile-config-smallrye:1.0">
<config-source-provider name="my-config-source-provider">
<class name="org.example.MyConfigSourceProvider" module="org.example"/>
</config-source-provider>
</subsystem>
ConfigSourceProvider 实施提供的属性可用于任何 JBoss EAP 部署。
有关如何将全局模块添加到 JBoss EAP 服务器的详情,请参考定义全局模块。
25.1.3. 部署访问 ConfigSources 的应用程序 复制链接链接已复制到粘贴板!
在 Java 代码中访问 MicroProfile 配置的应用必须启用 CDI,方法是在部署存档 中包含 META-INF/beans.xml 或 WEB-INF/beans.xml 文件,或者包含 CDI bean 注释。
有关 CDI 的更多信息,请参阅 JBoss EAP 开发指南中的上下文和依赖 https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.3/html-single/development_guide/#contexts_and_dependency_injection 注入(CDI )。