java 具有多租户的 Spring Boot + Spring Data
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26898896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot + Spring Data with multi tenancy
提问by zonski
Is it possible to configure Spring Boot to use a MultiTenantConnectionProvider so that each client of my system connects to their own private database?
是否可以将 Spring Boot 配置为使用 MultiTenantConnectionProvider,以便我系统的每个客户端都连接到自己的私有数据库?
Specifically I am looking to use the built-in hibernate support for multi-tenancy:
具体来说,我希望对多租户使用内置的休眠支持:
And this is an example of the sort of config I am after, but I can't figure out how to use this in a Spring Boot setup:
这是我所追求的那种配置的一个例子,但我不知道如何在 Spring Boot 设置中使用它:
I've tried adding these properties to application.properties
:
我试过将这些属性添加到application.properties
:
spring.jpa.hibernate.multiTenancy=DATABASE
spring.jpa.hibernate.tenant_identifier_resolver=com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.hibernate.multi_tenant_connection_provider=com.mystuff.MyMultiTenantConnectionProviderImplX
I've also tried coding up my own CurrentTenantIdentifierResolver
and MultiTenantConnectionProvider
and tried serving these up from my main @Configuration bean:
我也尝试过自己编写代码CurrentTenantIdentifierResolver
,MultiTenantConnectionProvider
并尝试从我的主要 @Configuration bean 中提供这些:
@Bean
public CurrentTenantIdentifierResolver currentTenantIdentifierResolver() {
return new CurrentTenantIdentifierResolver() {
public String resolveCurrentTenantIdentifier() {
// this is never called ...
}
public boolean validateExistingCurrentSessions() {
// this is never called ...
}
};
}
@Bean
public MultiTenantConnectionProvider multiTenantConnectionProvider() {
return new AbstractMultiTenantConnectionProvider() {
protected ConnectionProvider getAnyConnectionProvider() {
// this is never called ...
}
protected ConnectionProvider selectConnectionProvider(String s) {
// this is never called ...
}
};
}
None of this seems to have any affect so my question is really how to get spring-boot / spring-data to use these multi-tenant classes?
这些似乎都没有任何影响,所以我的问题真的是如何让 spring-boot / spring-data 使用这些多租户类?
Thanks for your help!
谢谢你的帮助!
采纳答案by M. Deinum
Any propertyfor JPA/Hibernate that isn't defined can be set using the spring.jpa.properties
property in the application.properties
.
任何属性未定义用于JPA /休眠可以使用被设置spring.jpa.properties
在属性application.properties
。
The sample you link to has 3 properties for multitenancy:
您链接到的示例具有 3 个多租户属性:
<prop key="hibernate.multiTenancy">SCHEMA</prop>
<prop key="hibernate.tenant_identifier_resolver">com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.webapp.persistence.utility.MultiTenantContextConnectionProvider</prop>
That converted to Spring Boot would be the following properties in the application.properties
file.
转换为 Spring Boot 的将是application.properties
文件中的以下属性。
spring.jpa.properties.hibernate.multiTenancy=SCHEMA
spring.jpa.properties.hibernate.tenant_identifier_resolver=com.mystuff.MyCurrentTenantIdentifierResolver
spring.jpa.properties.hibernate.multi_tenant_connection_provider=com.webapp.persistence.utility.MultiTenantContextConnectionProvider
For your situation (as stated in your question).
对于您的情况(如您的问题所述)。
spring.jpa.properties.hibernate.multiTenancy=DATABASE
spring.jpa.properties.hibernate.tenant_identifier_resolver=com.webapp.persistence.utility.CurrentTenantContextIdentifierResolver
spring.jpa.properties.hibernate.multi_tenant_connection_provider=com.mystuff.MyMultiTenantConnectionProviderImplX
It will not work with Spring manged beans as hibernate controls the lifecycle of those instances.
它不适用于 Spring 管理的 bean,因为休眠控制着这些实例的生命周期。
For more properties see the the Spring Boot reference guide.
有关更多属性,请参阅 Spring Boot参考指南。