我们正在开发一个在 Apache Karaf 上运行的 OSGi 应用程序。我们的应用程序使用 Apache Camel 和 MongoDB。
首先,我们成功地使用了一个很旧的“mongo-java-driver”版本(3.12.11):通过将此依赖项添加到模块
pom.xml
和osgi feature repository
,我们能够启动我们的应用程序并连接到 MongoDB:
pom.xml(maven 模块 1)
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.11</version>
</dependency>
feature.xml:
<repository>mvn:org.apache.camel.karaf/apache-camel/3.18.4/xml/features</repository>
<feature name="module1" description="An OSGi module" version="1.0.1-SNAPSHOT">
<feature>scr</feature>
<feature prerequisite="true">aries-blueprint</feature>
<feature>camel-core</feature>
<feature>camel-blueprint</feature>
<feature>camel-cxf</feature>
<feature>camel-xslt-saxon</feature>
<feature>camel-jetty</feature>
<feature>camel-rabbitmq</feature>
<feature>camel-openapi-java</feature>
<feature>camel-jackson</feature>
<capability>osgi.service;objectClass=org.apache.aries.blueprint.NamespaceHandler;osgi.service.blueprint.namespace=http://camel.apache.org/schema/blueprint;effective:=active;
</capability>
<bundle dependency="true">mvn:org.mongodb/mongo-java-driver/3.12.11</bundle>
<bundle>mvn:my.own.project/module1/1.0.1-SNAPSHOT</bundle>
</feature>
但是那个驱动是遗留的,很旧并且缺少重要的功能,所以我们想使用一个现代的驱动,即 mongodb-driver-sync(版本 4.8.2)。
我们用“mongodb-driver-sync”替换了之前的驱动:
pom.xml(maven 模块 1)
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.8.2</version>
</dependency>
feature.xml:
<repository>mvn:org.apache.camel.karaf/apache-camel/3.18.4/xml/features</repository>
<feature name="module1" description="An OSGI module" version="1.0.1-SNAPSHOT">
<feature>scr</feature>
<feature prerequisite="true">aries-blueprint</feature>
<feature>camel-core</feature>
...
<!-- <bundle dependency="true">mvn:org.mongodb/mongo-java-driver/3.12.11</bundle>-->
<bundle dependency="true">mvn:org.mongodb/mongodb-driver-sync/4.8.1</bundle>
<bundle>mvn:my.own.project/module1/1.0.1-SNAPSHOT</bundle>
</feature>
在 Karaf 中启动该功能时失败:
Error executing command: Unable to resolve root: missing requirement [root] osgi.identity;
osgi.identity=mesh; type=karaf.feature; version="[1.0.1.SNAPSHOT,1.0.1.SNAPSHOT]";
filter:="(&(osgi.identity=mesh)(type=karaf.feature)(version>=1.0.1.SNAPSHOT)(version<=1.0.1.SNAPSHOT))"
[caused by: Unable to resolve mesh/1.0.1.SNAPSHOT: missing requirement [mesh/1.0.1.SNAPSHOT] osgi.identity; osgi.identity=mesh-vms-tso01; type=karaf.feature
[caused by: Unable to resolve mesh-vms-tso01/1.0.1.SNAPSHOT: missing requirement [mesh-vms-tso01/1.0.1.SNAPSHOT] osgi.identity; osgi.identity=mesh-vms-tso01; type=osgi.bundle; version="[1.0.1.SNAPSHOT,1.0.1.SNAPSHOT]"; resolution:=mandatory
[caused by: Unable to resolve mesh-vms-tso01/1.0.1.SNAPSHOT: missing requirement [mesh-vms-tso01/1.0.1.SNAPSHOT] osgi.wiring.package; filter:="(&(osgi.wiring.package=com.btc.mesh.core.camel)(version>=1.0.0)(!(version>=2.0.0)))"
[caused by: Unable to resolve mesh-core/1.0.1.SNAPSHOT:
missing requirement [mesh-core/1.0.1.SNAPSHOT] osgi.wiring.package; filter:="(&(osgi.wiring.package=com.mongodb)(version>=4.8.0)(!(version>=5.0.0)))"]]]]
我还尝试了 4.1.0 版,因为一份较旧的文档提到它是一个有效的 OSGI 包:
mongodb-driver-sync 工件是一个有效的 OSGi 包,其符号 名称是 org.mongodb.driver-sync。”
https://mongodb.github.io/mongo-java-driver/4.1/driver/getting-started/installation/
但我们也没有运气。 符号名称有什么帮助吗?
在版本 4.3.x 之后,文档从
mongodb.github.io
移动到 www.mongodb.com
并且对 OSGi 的引用已被删除。所有提到的驱动程序的 MANIFEST 文件看起来非常相似,包括捆绑包信息。所以,据我所知,OSGi 应该可以工作。非常混乱。
所以,我们需要你的帮助
谢谢
实际上我今天下午在下面的注释中找到了一个解决方案,它适用于 4.8.2 版的 mongodb 同步驱动程序:
如果将驱动程序 JAR 文件直接集成到项目中,那么您 还应该添加 bson 和 mondodb.driver.core 的 JAR。
显然,在为 OSGi 定义依赖项时,这也是必要的:
<feature name="sample version="1.0.1-SNAPSHOT">
...
<bundle dependency="true">mvn:org.mongodb/bson/4.8.2</bundle>
<bundle dependency="true">mvn:org.mongodb/mongodb-driver-core/4.8.2</bundle>
<bundle dependency="true">mvn:org.mongodb/mongodb-driver-sync/4.8.2</bundle>
</feature>
我想知道这是否真的是正确的方法。也许有人可以为我证实这一点。
当然如果大家能在文档中找到解决方案就好了。也许可以整合?!
整体上OSGi似乎不再很普遍了,尤其是在文档方面。恕我直言,OSGi 仍然是一项出色的技术,它代表了微服务(每个服务在其自己的 Java 运行时中运行)和(部署)单体之间的巨大妥协!