在Apache Karaf中,功能与捆绑与依赖关系与先决条件与要求之间有何区别?

问题描述 投票:1回答:1

不幸的是,名为Karaf的OSGi容器实现文档记录很少。概念被忽略了,术语之间没有关系。

阅读Karaf开发人员撰写的文章后我的结论(我猜?):

  • “先决条件”不允许我的“特殊服务器”包在其他包(我称之为依赖项)在OSGi容器中不可用时启动。
  • 依赖是相同的
  • 这两个都不会导致Karaf自动获取并启动这些依赖项
  • 根据文档https://karaf.apache.org/manual/latest/provisioning,要求将导致Karaf自动获取并启动这些依赖性/先决条件/要求。
  • 存储库位于我的features.xml中,供开发人员知道从何处获取依赖项/先决条件/要求,但不会自动添加到Karaf中。

请填写我的

这是我通过maven-resources-plugincopy-resources目标运行的features.xml的示例,以便插入${var}s。

<?xml version="1.0" encoding="UTF-8"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.0.0 http://karaf.apache.org/xmlns/features/v1.0.0"
    name="special-server-features">

    <!-- Special Server -->
    <feature name="special-server" version="1.0.0" install="auto" resolver="(obr)">

        <details>
            A feature is just a group of bundles that should all be installed together.
            When an OSGi container adds a bundle, it goes through a resolution process
            to make sure that the bundle’s dependencies are met (and that it does not
            conflict with other installed bundles). However, that resolution process
            does not include any ability to obtain any dependencies; it just checks to
            see if they are available and delays or prevents the bundle from starting
            if a required dependency is missing.

            Requirements can tell the feature resolver to
            automatically install the bundles to satisfy the requirements.

            Dependencies vs. prerequisites:
        </details>



        <!-- Required feature repositories (containing all bundles) -->
        <repository>mvn:org.apache.camel.karaf/apache-camel/${camel.version}/xml/features</repository>
        <repository>mvn:org.apache.cxf.karaf/apache-cxf/${camel.version}/xml/features</repository>

        <bundle version="${camel.version}" prerequisite="true">camel-core</bundle>
        <bundle version="${camel.version}" prerequisite="true">cxf</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-blueprint</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jackson</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-cxf</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-http</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jaxb</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-jsch</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-log</bundle>
        <bundle version="${camel.version}" prerequisite="true">camel-stream</bundle>
    </feature>

</features>
apache-karaf karaf
1个回答
3
投票

Apache Karaf文档基本上扩展了OSGi规范的术语,这意味着假设您对OSGi有一定的了解。

说到这一点,你提到的不同术语可以清楚地显示在OSGi或Karaf中。

术语“Bundle”,“Dependency”和“Requirement”属于OSGi Core规范。而“功能”和“先决条件”是Apache Karaf的特定术语。

现在到你的清单:

问:“先决条件”不允许我的“特殊服务器”包在OSGi容器中没有其他包(我会调用依赖项)时启动。

答:首先,请注意“先决条件”不适用于捆绑依赖项,仅适用于功能依赖项(顺便说一句。您的XSD已过时,请查看current XSD),是的,它只是一个特殊化的依赖。为此,documentation非常明确:

如果要将先决条件属性添加到依赖功能标记,则在安装实际功能之前,它将强制安装并激活依赖功能中的捆绑包。

问:依赖关系是一样的

答:是的,不是。由于“先决条件”依赖关系仍然只是依赖于特征的安装/激活生命周期的不同行为,它们仍然只是描述依赖关系,但行为略有不同。

如果你引用了bundle依赖的特殊属性,例如: <bundle dependency="true">...,那么这意味着如果捆绑(在指定的情况下尊重可接受的版本)已在系统中可用,则不会再次安装。

问:这两个都不会导致Karaf自动获取并启动这些依赖项

答:在这两种情况下,Karaf都会根据需要安装相关功能和捆绑包。启动发生在之前(具有“先决条件”功能)或功能安装(除非您已禁用)。

问:根据文档,要求将导致Karaf自动获取并启动这些依赖性/先决条件/要求。

答:如果你指的是功能“requirements”,那么是和否。是的,因为解析器会尝试找到提供要求的其他功能或捆绑(这称为“Capability”)并在找到某些功能时安装它。如果系统的当前状态已经提供了要求,则不会发生任何事情,并且可以立即安装该功能。如果找不到任何捆绑或功能,则功能安装将失败。不,因为他们不会立即开始。启动功能本身时会启动。

问:存储库位于我的features.xml中,供开发人员知道从何处获取依赖关系/先决条件/要求,但不会自动添加到Karaf。

答:显然没有。您添加存储库以让Karaf解析器知道在哪里可以找到依赖功能的定义,而不是捆绑包。如果您没有定义中的其他功能的依赖项,则没有理由添加存储库。 documentation提供了更多细节。

TL; DR

您正在抱怨文档但是您自己混淆了术语,并且最终可能会出现错误的假设或期望。但我同意在一些细节上,卡拉夫的术语可能更好,更直观。

关于你的features.xml

  1. 请将架构更新为v1.3.0
<features xmlns="http://karaf.apache.org/xmlns/features/v1.3.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
            http://karaf.apache.org/xmlns/features/v1.3.0
            http://karaf.apache.org/xmlns/features/v1.3.0"
          name="special-server-features">
  1. 如果要安装Apache Camel和CXF,只需安装功能,而不是捆绑包,例如:
    <feature name="special-server" version="1.0.0" install="auto" resolver="(obr)">
        <feature>camel-blueprint</feature>
        ...
    </feature>
  1. 你对<bundle>依赖的声明是完全错误的。您指定了功能,而不是捆绑。
  2. prerequisite标签没有<bundle>属性,从来没有(请遵守XSD)
  3. <repository>只能在顶层声明,而不是在功能内部(也违反XSD)

示例功能存储库

根据您的示例,我编译了一个包含注释的示例功能存储库,以试图澄清更实用的问题:

<?xml version="1.0" encoding="UTF-8"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.4.0 http://karaf.apache.org/xmlns/features/v1.4.0"
    name="special-server-features">

    <!-- Required feature repositories -->
    <!-- We don't need to define this since Apache Camel already does it
    <repository>mvn:org.apache.cxf.karaf/apache-cxf/3.3.1/xml/features</repository>
    -->
    <repository>mvn:org.apache.camel.karaf/apache-camel/3.0.0.M2/xml/features</repository>

    <!-- Special Server -->
    <feature name="special-server" version="1.0.0" install="auto">
        <!--
            Require Java 8 at least.
        -->
        <requirement>osgi.ee;filter:=&quot;(&amp;(osgi.ee=JavaSE)(version&gt;=1.8))&quot;</requirement>

        <!--
            Every <feature> declares a dependency to another feature declaration
            (either available in this <features> repository or an external one.

            The dependency is bascially made up by referencing the "name" of
            another <feature> declaration.

            dependency="true"
                the feature will not be installed if already available

            prerequisite="true"
                the feature will be installed before ours and all bundles will
                be started
        -->
        <feature dependency="true" prerequisite="true">cxf</feature>
        <feature prerequisite="true">camel-core</feature>
        <feature prerequisite="true">camel-cxf</feature>

        <!--
            These features will just be installed as part of installing the
            current feature.
        -->
        <feature>camel-blueprint</feature>
        <feature>camel-jackson</feature>
        <feature>camel-http4</feature>
        <feature>camel-jaxb</feature>
        <feature>camel-jsch</feature>
        <feature>camel-stream</feature>

        <!--
            Every <bundle> declares a dependency to a standard OSGi Bundle using
            a URL including a protocol to uniquely identify the artifact.

            For Apache Karaf the most common protocol is to rely on Maven:
            https://ops4j1.jira.com/wiki/spaces/paxurl/pages/3833866/Mvn+Protocol

            Here, you also need to know that Apache Karaf also provides an
            internal Maven repository which is asked first and contains all
            Bundles that are already installed. This Maven repository usually
            exists at the Karaf installation sub-directory "system".
        -->

        <!--
            This bundle needs to be available, but we certainly don't want to
            "wrap" it again if it is already there.

            See also: https://ops4j1.jira.com/wiki/spaces/paxurl/pages/3833898/Wrap+Protocol
        -->
        <bundle dependency="true">wrap:mvn:org.minidns/minidns-core/0.3.3</bundle>

        <!--
            Now this is our own bundle which requires all of the above to do
            it's work properly.
        -->
        <bundle>mvn:com.mycorp.servers/special-server/1.0.0</bundle>
    </feature>
</features>
© www.soinside.com 2019 - 2024. All rights reserved.