12.4. 使用 Fabric8 Karaf 功能


Fabric8 提供对 Apache Karaf 的支持,使其更易于开发用于 Kubernetes 的 OSGi 应用程序。

Fabric8 的重要功能如下:

  • 解析 Blueprint XML 文件中的占位符不同。
  • 环境变量
  • 系统属性
  • 服务
  • Kubernetes ConfigMap
  • Kubernetes Secret
  • 使用 Kubernetes 配置映射来动态更新 OSGi 配置管理。
  • 为 OSGi 服务提供 Kubernetes 健康检查。

12.4.1. 添加 Fabric8 Karaf 功能

要使用该功能,请将 fabric8-karaf-features 依赖项添加到项目 POM 文件中。

流程

  1. 打开项目的 pom.xml 文件并添加 fabric8-karaf-features 依赖项。
<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>fabric8-karaf-features</artifactId>
  <version>${fabric8.version}</version>
  <classifier>features</classifier>
  <type>xml</type>
</dependency>
Copy to Clipboard Toggle word wrap

fabric8 karaf 功能将安装到 Karaf 服务器中。

12.4.2. 添加 Fabric8 Karaf Core 捆绑包功能

捆绑包 fabric8-karaf-core 提供了 Blueprint 和 ConfigAdmin 扩展使用的功能。

流程

  1. 打开项目的 pom.xml,并将 fabric8-karaf-core 添加到 startupFeatures 部分。

    <startupFeatures>
      ...
      <feature>fabric8-karaf-core</feature>
      ...
    </startupFeatures>
    Copy to Clipboard Toggle word wrap

    这将在自定义 Karaf 发行版中添加 fabric8-karaf-core 功能。

12.4.3. 设置 Property Placeholder 服务选项

捆绑包 fabric8-karaf-core 会使用以下接口导出服务 PlaceholderResolver

public interface PlaceholderResolver {
    /**
     * Resolve a placeholder using the strategy indicated by the prefix
     *
     * @param value the placeholder to resolve
     * @return the resolved value or null if not resolved
     */
    String resolve(String value);

    /**
     * Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
     *
     * @param source the string to replace in
     * @return the result of the replace operation
     */
    String replace(String value);

    /**
     * Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
     *
     * @param value the builder to replace in
     * @rerurn true if altered
     */
    boolean replaceIn(StringBuilder value);

    /**
     * Replaces all the occurrences of variables within the given dictionary
     *
     * @param dictionary the dictionary to replace in
     * @rerurn true if altered
     */
    boolean replaceAll(Dictionary<String, Object> dictionary);

    /**
     * Replaces all the occurrences of variables within the given dictionary
     *
     * @param dictionary the dictionary to replace in
     * @rerurn true if altered
     */
    boolean replaceAll(Map<String, Object> dictionary);
}
Copy to Clipboard Toggle word wrap

PlaceholderResolver 服务充当不同属性占位符解析策略的收集器。默认情况下提供的解析策略列在表 解析策略 中。要设置属性占位符服务选项,您可以使用系统属性或环境变量或两者。

流程

  1. 要访问 OpenShift 上的 ConfigMap 需要查看权限。为服务帐户添加查看权限。

    oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)
    Copy to Clipboard Toggle word wrap
  2. 将 secret 挂载到 Pod,因为通过 API 访问 secret 可能会被限制。
  3. Pod 上作为卷挂载可用的 secret 映射到名为 secret 的目录,如下所示

    containers:
      -
       env:
       - name: FABRIC8_K8S_SECRETS_PATH
         value: /etc/secrets
         volumeMounts:
       - name: activemq-secret-volume
         mountPath: /etc/secrets/activemq
         readOnly: true
       - name: postgres-secret-volume
         mountPath: /etc/secrets/postgres
         readOnly: true
    
    volumes:
      - name: activemq-secret-volume
      secret:
      secretName: activemq
      - name: postgres-secret-volume
       secret:
      secretName: postgres
    Copy to Clipboard Toggle word wrap

12.4.4. 添加自定义属性占位符解析器

您可以添加自定义占位符解析器来支持特定需求,如自定义加密。您还可以使用 PlaceholderResolver 服务将解析器提供给 Blueprint 和 ConfigAdmin。

流程

  1. 将以下 mvn 依赖项添加到项目 pom.xml 中。

    pom.xml

    ---
    <dependency>
      <groupId>io.fabric8</groupId>
      <artifactId>fabric8-karaf-core</artifactId>
    </dependency>
    ---
    Copy to Clipboard Toggle word wrap

  2. 实施 PropertiesFunction 接口,并使用 SCR 将其注册为 OSGi 服务。

    import io.fabric8.karaf.core.properties.function.PropertiesFunction;
    import org.apache.felix.scr.annotations.Component;
    import org.apache.felix.scr.annotations.ConfigurationPolicy;
    import org.apache.felix.scr.annotations.Service;
    
    @Component(
        immediate = true,
        policy = ConfigurationPolicy.IGNORE,
        createPid = false
    )
    @Service(PropertiesFunction.class)
    public class MyPropertiesFunction implements PropertiesFunction {
        @Override
        public String getName() {
            return "myResolver";
        }
    
        @Override
        public String apply(String remainder) {
            // Parse and resolve remainder
            return remainder;
        }
    }
    Copy to Clipboard Toggle word wrap
  3. 您可以按照如下所示引用配置管理中的解析器:

    属性

    my.property = $[myResolver:value-to-resolve]
    Copy to Clipboard Toggle word wrap

12.4.5. 解决方案策略列表

PlaceholderResolver 服务充当不同属性占位符解析策略的收集器。它默认提供的解析策略列在表中。

  1. 解决方案策略列表
Expand

prefix

示例

描述

env

env:JAVA_HOME

从 OS 环境变量查找 属性。

`sys

sys:java.version

从 Java JVM 系统属性中查找 属性。

'service

service:amq

使用服务命名约定从 OS 环境变量查找属性。

service.host

service.host:amq

使用服务命名约定从 OS 环境变量查找属性,仅返回 hostname 部分。

service.port

service.port:amq

使用服务命名约定从 OS 环境变量查找 属性,仅返回端口部分。

k8s:map

k8s:map:myMap/myKey

从 Kubernetes ConfigMap(通过 API)查找属性

k8s:secret

k8s:secret:amq/password

从 Kubernetes Secret(通过 API 或卷挂载)查找属性

12.4.6. Property Placeholder 服务选项列表

属性占位符服务支持以下选项:

  1. 属性占位符服务选项列表
Expand
名称default描述

fabric8.placeholder.prefix

$[

占位符前缀

fabric8.placeholder.suffix

]

占位符后缀

fabric8.k8s.secrets.path

null

以逗号分隔的映射 secret 的路径列表

fabric8.k8s.secrets.api.enabled

false

通过 API 启用/禁用 secret

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat