Hibernate OGM与云MongoDB Atlas M0(免费层)的结合使用 [英] Usage of Hibernate OGM with cloud MongoDB Atlas M0 (Free Tier)

查看:49
本文介绍了Hibernate OGM与云MongoDB Atlas M0(免费层)的结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将MongoDB Atlas M0(免费套餐)用于我的JAVA EE应用程序,现在我正在使用:

I am trying to use MongoDB Atlas M0 (Free Tier) for my JAVA EE application, now I am using:

  • 本地MongoDB数据库(v4.0.4)

  • Local MongoDB database (v4.0.4)

Hibernate Core休眠核心5.3.6.Final"

Hibernate Core "hibernate-core 5.3.6.Final"

Hibernate OGM"hibernate-ogm-mongodb 5.3.1.Final"

Hibernate OGM "hibernate-ogm-mongodb 5.3.1.Final"

Java应用程序服务器WildFly 15.0.0.Final.

Java application server WildFly 15.0.0.Final.

在本地数据库中,一对MongoDB和Hibernate OGM就像一个魅力一样,但是当我尝试在免费层上将Hibernate与Mongo Atlas连接以测试云数据库时,我无法建立有效的连接,因为mongodb驱动程序引发异常 com.mongodb.MongoSocketReadException:过早到达流的结尾

With a local database a pair MongoDB and Hibernate OGM works like a charm, but when I tried to connect Hibernate with Mongo Atlas on free tier to test cloud database, I am was not able to have a working connection, because mongodb driver throws an exception com.mongodb.MongoSocketReadException: Prematurely reached end of stream

我将提供两个版本的persistence.xml,第一个与localhost配合正常,第二个是我用来连接云的内容.

I will provide two versions of my persistence.xml, first is working fine with localhost and second is what I used to connect to cloud.

工作本地主机版本:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">

    <!--   @@@   MongoDB   HIBERNATE   OGM   PERSISTENCE   UNIT   @@@   -->
    <persistence-unit name="PersistenceUnitNoSQL" transaction-type="JTA">

        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <class>org.companyname.model.UserEntity</class>
        <class>org.companyname.model.ItemEntity</class>

        <properties>

            <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
            <property name="hibernate.ogm.datastore.host" value="localhost:27017"/>
            <property name="hibernate.ogm.datastore.database" value="databasename"/>
            <property name="hibernate.ogm.datastore.create_database" value="true"/>

        </properties>

    </persistence-unit>

</persistence>

无法使用Atlas云版本:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">

    <!--   @@@   MongoDB   HIBERNATE   OGM   PERSISTENCE   UNIT   @@@   -->
    <persistence-unit name="PersistenceUnitNoSQL" transaction-type="JTA">

        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <class>org.companyname.model.UserEntity</class>
        <class>org.companyname.model.ItemEntity</class>

        <properties>

            <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
            <property name="hibernate.ogm.datastore.host" value="cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-01-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017"/>

            <property name="hibernate.ogm.datastore.database" value="databasename"/>
            <property name="hibernate.ogm.datastore.create_database" value="true"/>

        <property name="hibernate.ogm.datastore.username" value="atlas-user-name"/>
        <property name="hibernate.ogm.datastore.password" value="atlas-user-password"/>
        <property name="hibernate.ogm.mongodb.authentication_mechanism" value="SCRAM_SHA_1"/>

        </properties>

    </persistence-unit>

</persistence>

就像主机一样,我使用Atlas帐户的帮助窗口中的副本集从标准连接字符串"中获取URI,因为使用短SRV连接字符串"时,Hibernate会抛出 org.hibernate.service.spi.ServiceException:OGM000072:无法配置数据存储提供程序,因此我认为尚不支持这种类型的连接.

Like a host I used replica sets URIs from "standard connection string" from helping window in the Atlas account, because with "short SRV connection string" Hibernate throws org.hibernate.service.spi.ServiceException: OGM000072: Unable to configure datastore provider, so I think there is no support of this type of connection yet.

因此,使用最后的persistence.xml配置,我遇到以下错误:

So with this last persistence.xml configuration I had following error:

20:02:01,094 INFO  [org.mongodb.driver.cluster] (ServerService Thread Pool -- 78) Cluster description not yet available. Waiting for 30000 ms before timing out
20:02:01,175 INFO  [org.mongodb.driver.cluster] (cluster-ClusterId{value='5c5497a97aea6111622c7540', description='null'}-cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017)
Exception in monitor thread while connecting to server cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
    at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
    at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:106)
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.lang.Thread.run(Thread.java:748)

Hibernate尝试连接到每个分片(00-00、00-01、00-02),但是所有抛出此异常.

Hibernate tries to connect to each shard (00-00, 00-01, 00-02), but with all throws this exception.

我试图解决该问题的方法:

What I tried to do to fix the problem:

  • 使用mongo-java-driver版本3.9.1而不是内置的Hibernate 3.6.3,但是两个驱动程序都存在相同的问题

  • using mongo-java-driver version 3.9.1 instead of Hibernate 3.6.3 built-in, but both drivers work with the same problem

我的IP已添加到我的地图集帐户IP白名单中(我的应用是从笔记本电脑部署的)

my IP is added (my app is deployed from my laptop) to my atlas account IP Whitelist

我能够从Mongo Shell和MongoDB Compass很好地连接到集群

I am able to connect fine to cluster from Mongo Shell and MongoDB Compass

我对"hibernate.ogm.mongodb.authentication_mechanism"有疑问,但"SCRAM_SHA_1"和"BEST"对我没有帮助

I had a doubts about "hibernate.ogm.mongodb.authentication_mechanism", but "SCRAM_SHA_1" and "BEST" didn't worked for me

最后,我尝试直接从Java(没有Hibernate)与集群建立连接

and finally I tried to make a connection to the cluster from Java directly (without Hibernate)

使用

MongoClient mongoClient = MongoClients.create("mongodb+srv://atlas-user-name:atlas-user-password@cluster0-clustername-raa4n.mongodb.net/test?retryWrites=true");

MongoClient mongoClient = MongoClients.create("mongodb://atlas-user-name:atlas-user-password@cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-01-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-clustername-shard-0&authSource=admin&retryWrites=true");

两种情况都很好,我能够建立连接并使用数据库而没有问题.

and both cases worked fine, I was able to make a connection and to use the database without problems.

所以我的问题是为什么Hibernate抛出这种异常?

So my problem is why Hibernate throws this kind of exception?

推荐答案

Hibernate OGM创建客户端的方式可能不正确.

It's possible that something is not right in the way Hibernate OGM creates the client.

我认为现在为您检查的更简单方法是覆盖 MongoDBDatastoreProvider 并提供一个初始化的MongoClient.

I think the easier way to check this now for you is to override the MongoDBDatastoreProvider and provide an initialized MongoClient.

您可以通过扩展MongoDBDatastoreProvider并覆盖方法createMongoClient来实现.像这样:

You can do this by extending MongoDBDatastoreProvider and overriding the method createMongoClient. Something like:

package org.myprojects;

import org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider;

public class MYCustomMongoDBDatastoreProvider extends MongoDBDatastoreProvider {

    @Override
    protected MongoClient createMongoClient(MongoDBConfiguration config) {    
       return MongoClients.create(...);
    }

}

然后使用属性 OgmProperties.DATASTORE_PROVIDER 来使用数据存储提供程序:

then use the property OgmProperties.DATASTORE_PROVIDER to use your datastore provider:

hibernate.ogm.datastore.provider = org.myprojects.MYCustomMongoDBDatastoreProvider

在此示例中,我将其设置在hibernate.properties文件中,但可以在对您的项目更有意义的位置进行设置.

In this example I'm setting it in the hibernate.properties file but you can set it where it makes more sense for your project.

编辑:有关错误的其他说明.

EDIT: Additional explanations about the error.

编辑2 :该问题可能是由于缺乏对SSL的支持所致.问题已创建并包含更多信息

EDIT 2: The problem might be caused by a lack of support for SSL. An issue has been created and contains more information

这篇关于Hibernate OGM与云MongoDB Atlas M0(免费层)的结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆