我在Docker容器中的JBoss WildFly 16.0.0.Final上部署并运行了Java EE 11应用程序。
容器ID图像命令
创建的状态端口
名称867d2b0c179b gostophandle:latest“ / opt / jboss / wildfly /…” 4秒前上3秒0.0.0.0:32826->8080/tcp
Elegant_shtern
我有一个也在Docker容器中运行的MongoDB实例。
容器ID图像命令
创建的状态端口名称 1c064a161638 mongo“ docker-entrypoint.s…” 2 分钟前上2分钟27017 / tcp goStopHandle
这是我的“ persistence.xml”,用于连接到MongoDB的“ localhost”实例:
<persistence-unit name="goStopHandleMongoDBPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<!-- <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.2"/> -->
<property name="jboss.as.jpa.providerModule" value="org.hibernate:5.3"/>
<!-- <property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.8"/> -->
<property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.10"/>
<!-- <property name="hibernate.transaction.jta.platform" value="JBossTS"/> -->
<!-- <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAS"/> -->
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
<property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
<property name="hibernate.ogm.datastore.grid_dialect" value="org.hibernate.ogm.datastore.mongodb.MongoDBDialect"/>
<property name="hibernate.ogm.datastore.database" value="goStopHandleDB"/>
<property name="hibernate.ogm.datastore.host" value="127.0.0.1:27017"/>
</properties>
</persistence-unit>
我已经跑了:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' aea9f245ff90
并获得MongoDB容器的IP地址:
172.17.0.2
我将persistence.xml更改为使用:
<property name="hibernate.ogm.datastore.host" value="172.17.0.2:27017"/>
启动WildFly容器时,我得到:
[0m[0m19:39:18,823 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle: Name or service not known
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.UnknownHostException: gostophandle: Name or service not known
at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
... 5 more
[0m[0m19:39:19,327 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.UnknownHostException: gostophandle
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
... 5 more
[0m[0m19:39:28,841 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle: Name or service not known
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.UnknownHostException: gostophandle: Name or service not known
at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
... 5 more
[0m[0m19:39:29,344 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ca50be6c9e77c00671d9ec5', description='null'}-gostophandle:27017) Exception in monitor thread while connecting to server gostophandle:27017: com.mongodb.MongoSocketException: gostophandle
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.UnknownHostException: gostophandle
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
at org.hibernate.ogm.mongodb:5.4@5.4.1.Final//com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
... 5 more
关于如何配置“ persistence.xml”的任何建议?
答案 0 :(得分:1)
类似于我尝试使用Robo 3T连接到MongoDB容器时遇到的问题。
需要公开端口:
docker run --name my-mongo -p 27017:27017 -d mongo
现在,Robo 3T和我的EAR都连接到MongoDB数据库。
答案 1 :(得分:0)
Hibernate OGM生成的MongoClient可能有问题。
解决此问题的一种方法是,确保用于客户端的配置正确无误,也许通过创建客户端并检查其连接是否正确的测试即可。
比使用OGM中的属性应用相同的配置。您可以使用前缀hibernate.ogm.mongodb.driver.*
为驱动程序设置属性。有关OGM中所有可用属性的列表,我将带您进入文档:https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_configuring_mongodb
您还可以扩展类MongoDBDatastoreProvider
并覆盖方法:
protected MongoClient createMongoClient(MongoDBConfiguration config) { ... }
然后将新类传递给属性hibernate.ogm.datastore.provider
。