问题描述
我正在用Grails构建一个多租户应用程序,我想保留单独的数据库。
我需要在运行时动态地更改URL以将GORM指向不同的数据库。
我有一个前端充当向集群分发请求的平衡器后端主机。每个后端主机运行一个Grails 2.3.5实例和一个包含多个数据库的MySQL服务器(每个租户一个)。我想动态更改dataSource,以便GORM可以访问正确数据库上的域实体。
有什么想法?
感谢
您可以在DataSource.groovy中配置多个数据源,查看。
在你的域:添加您的域可以与之交互的数据源,例如,
static mapping = {
datasources([''所有数据源的dataSource1','dataSource2'])
}
或ALL例如,
static mapping = {
datasource'ALL'
}
code>
然后您可以使用您想要获取/设置数据的数据源名称进行查询,例如
def userClass = User.class
User user = userClass.dataSource1.findByName(用户名')
参考: - ,
I'm building a multi tenant application with Grails and I want to keep separate databases.I need to change the url dynamically at runtime to point GORM to different database.
I have a front-end acting as a balancer distributing requests to a cluster of backend hosts. Each backend host runs a Grails 2.3.5 instance and a mysql-server with several databases (one per tenant). I would like to change dataSource dynamically so that GORM can access domain entities on the right database.
Any ideas ?
Thanks
You can configure multiple data source in your DataSource.groovy, have a look in the blog.
In your domains: add which data source your domain can interact, eg.,
static mapping = {
datasources(['dataSource1', 'dataSource2'])
}
or "ALL" for all datasources, eg.,
static mapping = {
datasource 'ALL'
}
and then you can make queries with data source name to which you want to get/set data, eg.,
def userClass = User.class
User user = userClass.dataSource1.findByName('username')
Ref:- multipleDatasources, Querying on multiple datasource in grails
这篇关于Grails:在运行时更改dataSource url以实现多租户数据库分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!