Spring-data-mongodb连接到一个Mongo实例中的多个数据库 [英] Spring-data-mongodb connect to multiple databases in one Mongo instance

查看:677
本文介绍了Spring-data-mongodb连接到一个Mongo实例中的多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的spring-data-mongodb(1.1.0.M2)和最新的Mongo Driver(2.9.0-RC1)。我有一种情况,我有多个客户端连接到我的应用程序,我想在同一个Mongo服务器中给每个客户端自己的架构/数据库。如果我直接使用驱动程序,这不是一项非常困难的任务:

I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application and I want to give each one their own "schema/database" in the same Mongo server. This is not a very difficult task to achieve if I was using the driver directly:

Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) );

DB client1DB = mongo.getDB( "client1" );
DBCollection client1TTestCollection = client1DB.getCollection( "test" );
long client1TestCollectionCount = client1TTestCollection.count();

DB client2DB = mongo.getDB( "client2" );
DBCollection client2TTestCollection = client2DB.getCollection( "test" );
long client2TestCollectionCount = client2TTestCollection.count();

看,很简单。但是spring-data-mongodb不允许使用多个数据库的简单方法。设置与 Mongo 的连接的首选方法是扩展 AbstractMongoConfiguration class:

See, easy. But spring-data-mongodb does not allow an easy way to use multiple databases. The preferred way of setting up a connection to Mongo is to extend the AbstractMongoConfiguration class:

您将看到您覆盖以下方法:

You will see that you override the following method:

getDatabaseName()

因此它强制您使用一个数据库名称。然后构建的存储库接口使用MongoTemplate中传递到 SimpleMongoRepository 类的数据库名称。

So it forces you to use one database name. The repository interfaces that you then build use that database name inside the MongoTemplate that is passed into the SimpleMongoRepository class.

我到底在哪里粘贴多个数据库名称?我必须创建多个数据库名称,多个 MongoTempate s(每个数据库名称一个),以及多个其他配置类。而且仍然没有让我的存储库接口使用正确的模板。如果有人尝试过这样的事情让我知道。如果我搞清楚,我会在这里发布答案。

Where on earth would I stick multiple database names? I have to make multiple database names, multiple MongoTempates (one per database name), and multiple other config classes. And that still doesn't get my repository interfaces to use the correct template. If anyone has tried such a thing let me know. If I figure it out I will post the answer here.

谢谢。

推荐答案

经过大量的研究和实验,我得出的结论是,目前的 spring-data-mongodb 项目尚不可能。我尝试了上面的baja方法并遇到了一个特定的障碍。 MongoTemplate 在其构造函数中运行其 ensureIndexes()方法。此方法调出数据库以确保数据库中存在带注释的索引。当 Spring 启动时,会调用 MongoTemplate 的构造函数,所以我甚至没有机会设置 ThreadLocal 变量。我必须在 Spring 启动时设置默认值,然后在请求进入时更改它。这是不允许的,因为我不想也没有默认值数据库。

So after much research and experimentation, I have concluded that this is not yet possibly with the current spring-data-mongodb project. I tried baja's method above and ran into a specific hurdle. The MongoTemplate runs its ensureIndexes() method from within its constructor. This method calls out the the database to make sure annotated indexes exist in the database. The constructor for MongoTemplate gets called when Spring starts up so I never even have a chance to set a ThreadLocal variable. I have to have a default already set when Spring starts, then change it when a request comes in. This is not allowable because I don't want nor do I have a default database.

尽管如此,所有人都没有丢失。我们最初的计划是让每个客户端在自己的应用服务器上运行,指向 MongoDB上的 MongoDB 数据库服务器。然后我们可以提供一个 -Dprovider = 系统变量,每个服务器只运行指向一个数据库。

All was not lost though. Our original plan was to have each client running on its own application server, pointed at its own MongoDB database on the MongoDB server. Then we can provide a -Dprovider= system variable and each server runs pointing only to one database.

我们是指示有一个多租户应用程序,因此尝试 ThreadLocal 变量。但由于它不起作用,我们能够按照我们最初设计的方式运行应用程序。

We were instructed to have a multi-tenant application, hence the attempt at the ThreadLocal variable. But since it did not work, we were able to run the application the way we had originally designed.

我相信有一种方法可以使这一切工作,它只需要比其他帖子中描述的更多。你必须自己创建 RepositoryFactoryBean 。以下是来自 Spring Data MongoDB参考文档。您仍然需要实现自己的 MongoTemplate 并延迟或删除 ensureIndexes()调用。但是你必须重写几个类以确保调用 MongoTemplate 而不是 Spring的。换句话说,做了很多工作。我希望看到的工作发生甚至做,我只是没有时间。

I believe there is a way though to make this all work, it just takes more than is described in the other posts. You have to make your own RepositoryFactoryBean. Here is the example from the Spring Data MongoDB Reference Docs. You would still have to implement your own MongoTemplate and delay or remove the ensureIndexes() call. But you would have to rewrite a few classes to make sure your MongoTemplate is called instead of Spring's. In other words, a lot of work. Work that I would like to see happen or even do, I just did not have the time.

感谢您的回复。

这篇关于Spring-data-mongodb连接到一个Mongo实例中的多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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