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 类:
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
(每个数据库名称一个)和多个其他配置类.这仍然没有让我的存储库接口使用正确的模板.如果有人尝试过这样的事情,请告诉我.如果我弄清楚了,我会在这里发布答案.
Where on earth would I stick multiple database names? I have to make multiple database names, multiple MongoTempate
s (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()
方法.此方法调用数据库以确保数据库中存在带注释的索引.MongoTemplate
的构造函数在 Spring
启动时被调用,所以我什至没有机会设置 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.
感谢您的回复.
相关文章