MongoDB的GORM动态多租户

时间:2017-09-08 11:51:35

标签: grails gorm multi-tenant grails-3.3.0

我正在使用Grails中的Angular项目探索GORM中的动态多租户。目前,我在我的application.yml

中有以下内容
grails:
    gorm:
        multiTenancy:
            mode: DATABASE
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.web.SubDomainTenantResolver
            connectionSourcesClass: org.grails.datastore.mapping.mongo.connections.MongoConnectionSources
            connectionsCollection: "myconnections"
        dataSource:
          dbCreate: create-drop
          url: 'mongodb://root:root@localhost:27017?ssl=false&authSource=admin'

这是我的application.yml

grails:
    gorm:
        multiTenancy:
            mode: DATABASE
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.web.SubDomainTenantResolver
            connectionSourcesClass: org.grails.datastore.mapping.mongo.connections.MongoConnectionSources
            connectionsCollection: "myconnections"
        dataSource:
          dbCreate: create-drop
          url: 'mongodb://root:root@localhost:27017?ssl=false&authSource=admin'

我在build.groovy中添加了以下内容并删除了org.grails.plugins:hibernate5

   compile "org.mongodb:mongodb-driver:3.4.2"
   compile 'org.grails.plugins:mongodb:6.1.2'

根据我是否为我的域类实现MultiTenant接口,在尝试这两件事之后发生了以下事情。

1.如果我实现了界面,那么我会收到以下错误

Caused by: java.lang.IllegalStateException: Either class [com.track.User] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
  1. 如果我没有实现它,那么一切都只是进入我的mongodb服务器上的一个数据库(名为test)。
  2. 我错过了什么?

    ****更新**** 从头开始创建一个新项目后,我收到以下异常

    Caused by: org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException: No tenantId found
        at org.grails.datastore.mapping.multitenancy.resolvers.NoTenantResolver.resolveTenantIdentifier(NoTenantResolver.groovy:17)
        at grails.gorm.multitenancy.Tenants.currentId(Tenants.groovy:73)
        at org.grails.datastore.gorm.GormEnhancer.findTenantId(GormEnhancer.groovy:270)
        at org.grails.datastore.gorm.GormEnhancer.findStaticApi(GormEnhancer.groovy:296)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormStaticApi(GormEntity.groovy:1349)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper.count(GormEntity.groovy:628)
        at grails.rest.RestfulController.countResources(RestfulController.groovy:275)
        at grails.rest.RestfulController.index(RestfulController.groovy:62)
        at grails.gorm.transactions.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:94)
        at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
        at grails.gorm.transactions.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:91)
        ... 14 common frames omitted
    

0 个答案:

没有答案