SpringBoot多租戶三種架構(gòu)實現(xiàn)方案詳解
環(huán)境:SpringBoot3.3.0
1. 簡介
多租戶表示應(yīng)用程序的單個運行實例同時為多個客戶機(租戶)服務(wù)的體系結(jié)構(gòu)。這在SaaS解決方案中非常常見。在這些系統(tǒng)中,隔離與各種租戶相關(guān)的信息(數(shù)據(jù)、定制等)是一個特殊的挑戰(zhàn)。這包括存儲在數(shù)據(jù)庫中的每個租戶擁有的數(shù)據(jù)。以下是三種常用的多租戶架構(gòu)實現(xiàn)方案:
1.1 獨立數(shù)據(jù)庫(Separate database)
圖片
每個租戶的數(shù)據(jù)都保存在一個物理上獨立的數(shù)據(jù)庫實例中。JDBC連接將專門指向每個數(shù)據(jù)庫,因此任何池都將按租戶進行。這里,一種通用的應(yīng)用程序方法是為每個租戶定義JDBC連接池,并根據(jù)與當前登錄用戶相關(guān)聯(lián)的租戶標識符來選擇要使用的池。
優(yōu)點:
- 數(shù)據(jù)隔離級別高,安全性好
- 可以根據(jù)租戶的需求進行數(shù)據(jù)庫優(yōu)化和擴展
- 備份和恢復操作相對簡單
缺點:
- 成本較高,需要為每個租戶購買和維護獨立的數(shù)據(jù)庫實例
- 可能存在硬件資源浪費,因為每個租戶可能只使用了數(shù)據(jù)庫的一部分功能
1.2 獨立Schema(Separate schema)
每個租戶的數(shù)據(jù)都保存在單個數(shù)據(jù)庫實例上的不同數(shù)據(jù)庫Schema中。這里有兩種不同的定義JDBC連接的方法:
- 連接可以特定地指向每個Schema,就像單獨的數(shù)據(jù)庫方法中那樣。這是一個選項,前提是驅(qū)動程序支持在連接URL中命名默認Schema,或者池機制支持命名用于其連接的Schema。使用這種方法,我們將為每個租戶創(chuàng)建一個不同的JDBC連接池,使用的連接池將基于與當前登錄用戶相關(guān)聯(lián)的“租戶標識符”進行選擇。
- 連接可以指向數(shù)據(jù)庫本身(使用某些默認Schema),但使用SQL SET schema(或類似的)命令可以更改連接。使用這種方法,我們將有一個JDBC連接池用于為所有租戶提供服務(wù),但在使用連接之前,它將被更改為引用由與當前登錄用戶關(guān)聯(lián)的“租戶標識符”命名的模式。
優(yōu)點:
- 降低了數(shù)據(jù)庫成本,因為多個租戶共享一個數(shù)據(jù)庫實例
- 數(shù)據(jù)隔離級別仍然較高,因為每個租戶使用獨立的模式
缺點:
- 模式之間可能存在資源競爭和性能瓶頸
- 備份和恢復操作可能更加復雜,因為需要針對每個模式進行單獨操作
1.3 分區(qū)數(shù)據(jù)(Partitioned (discriminator) data)
所有數(shù)據(jù)都保存在一個數(shù)據(jù)庫Schema中。通過使用分區(qū)列對每個租戶的數(shù)據(jù)進行分區(qū)。這種方法將使用單個連接池為所有租戶提供服務(wù)。但是,在這種方法中,應(yīng)用程序需要對每個SQL語句添加分區(qū)列(查詢時where條件加入分區(qū)列作為查詢條件)。
優(yōu)點:
- 成本最低,因為所有租戶都共享同一個數(shù)據(jù)庫實例和模式
- 數(shù)據(jù)訪問和查詢效率可能較高,因為數(shù)據(jù)都在同一個表中
缺點:
- 數(shù)據(jù)隔離級別最低,可能存在安全風險
- 需要通過應(yīng)用程序邏輯來確保數(shù)據(jù)的正確隔離和訪問控制
- 數(shù)據(jù)備份和恢復操作可能非常復雜,因為需要考慮到所有租戶的數(shù)據(jù)
接下來我會對分區(qū)數(shù)據(jù)和獨立數(shù)據(jù)庫2種架構(gòu)進行詳細的介紹。獨立Schema方案其實與獨立數(shù)據(jù)庫模式挺像的,如果基于MySQL其實對應(yīng)的就是不同數(shù)據(jù)庫(可以是同一個MySQL實例,通過use xxx切換數(shù)據(jù)庫),基于Oracle就是對應(yīng)不同的用戶上(并非schema與用戶等同)。
2. 實戰(zhàn)案例
2.1 分區(qū)數(shù)據(jù)
注:請先確保你當前使用的SpringBoot版本(Spring Data JPA)整合的Hibernate版本至少是6.0版本以上。
實體定義
@Entity
@Table(name = "t_person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
private String name ;
private Integer age ;
@TenantId
private String tenantId ;
}
這里通過@TenantId注解標注,該字段專門用來分區(qū)租戶的,Hibernate在查詢數(shù)據(jù)時會自動添加該查詢條件,如果你使用的本地SQL(自己編寫SQL),那么需要你自行添加該條件(租戶ID條件)。
編寫DAO&Service
// DAO
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {
}
// Service
@Service
public class PersonService {
private final PersonRepository personRepository ;
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository ;
}
// 查詢所有Person數(shù)據(jù)
public List<Person> persons() {
return this.personRepository.findAll() ;
}
}
Controller接口
@GetMapping("")
public List<Person> persons() {
return this.personService.persons() ;
}
以上是開發(fā)一個業(yè)務(wù)功能的基本操作,接下來才是重點
租戶標識解析處理
該的作用獲取當前租戶ID,這里基于ThreadLocal實現(xiàn)
public class TenantIdResolver implements CurrentTenantIdentifierResolver<String> {
private static final ThreadLocal<String> CURRENT_TENANT = new ThreadLocal<>();
public void setCurrentTenant(String currentTenant) {
CURRENT_TENANT.set(currentTenant);
}
@Override
public String resolveCurrentTenantIdentifier() {
// 注意這里不能返回null
return Optional.ofNullable(CURRENT_TENANT.get()).orElse("default") ;
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
上面的組件用來從當前的ThreadLocal中獲取租戶ID,接下來就是像ThreadLocal存入租戶ID。
Web攔截器
該攔截器的作用用來從請求Header中獲取租戶ID,存入ThreadLocal中。
@Component
public class TenantIdInterceptor implements HandlerInterceptor {
private final TenantIdResolver tenantIdResolver;
public TenantIdInterceptor(TenantIdResolver tenantIdResolver) {
this.tenantIdResolver = tenantIdResolver;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String tenantId = request.getHeader("x-tenant-id");
tenantIdResolver.setCurrentTenant(tenantId);
return true ;
}
}
最后一步就是配置hibernate,設(shè)置租戶ID的解析器。
配置租戶標識解析器
spring:
jpa:
properties:
hibernate:
'[tenant_identifier_resolver]': 'com.pack.tenant.config.TenantIdResolver'
完成以上類及配置的編寫后就實現(xiàn)了基于列區(qū)分(分區(qū))的多租戶架構(gòu)方案。
測試
準備數(shù)據(jù):
圖片
圖片
圖片
SQL執(zhí)行情況:
圖片
自動添加了tenant_id查詢條件。
2.2 獨立數(shù)據(jù)庫
每租戶對應(yīng)一個數(shù)據(jù)庫,這需要在項目中配置多個數(shù)據(jù)源,同時提供一個數(shù)據(jù)源路由的核心類。
定義多數(shù)據(jù)源配置
你也可以將數(shù)據(jù)源的信息專門存放在數(shù)據(jù)表中。
pack:
datasource:
defaultDs: ds1
config:
ds1:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tenant-01
username: tenant01
password: xxxooo
type: com.zaxxer.hikari.HikariDataSource
ds2:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tenant-02
username: tenant02
password: oooxxx
type: com.zaxxer.hikari.HikariDataSource
在Spring實現(xiàn)多數(shù)據(jù)源切換,可以通過繼承AbstractRoutingDataSource。
public class PackRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.get() ;
}
}
public class DataSourceContextHolder {
private static final ThreadLocal<String> HOLDER = new InheritableThreadLocal<>() ;
public static void set(String key) {
HOLDER.set(key) ;
}
public static String get() {
return HOLDER.get() ;
}
public static void clear() {
HOLDER.remove() ;
}
}
配置數(shù)據(jù)源Bean
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource(MultiDataSourceProperties properties) {
PackRoutingDataSource dataSource = new PackRoutingDataSource(properties.getDefaultDs()) ;
Map<Object, Object> targetDataSources = new HashMap<>() ;
// PackDataSourceProperties類僅僅就是繼承DataSourceProperties
Map<String, PackDataSourceProperties> configs = properties.getConfig() ;
configs.forEach((key, props) -> {
targetDataSources.put(key, createDataSource(props, HikariDataSource.class)) ;
});
dataSource.setTargetDataSources(targetDataSources) ;
return dataSource ;
}
private static <T> T createDataSource(PackDataSourceProperties properties, Class<? extends DataSource> type) {
// 這里沒有考慮池的配置
return (T) properties.initializeDataSourceBuilder().type(type).build();
}
}
接下來定義攔截器,設(shè)置當前要操作的數(shù)據(jù)源。
Web攔截器
@Component
public class TenantIdInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String tenantId = request.getHeader("x-tenant-id");
DataSourceContextHolder.set(tenantId) ;
return true ;
}
}
以上就完成了多數(shù)據(jù)源的所有類及配置的編寫。