SpringBoot多租戶三種架構(gòu)實(shí)現(xiàn)方案詳解
環(huán)境:SpringBoot3.3.0
1. 簡介
多租戶表示應(yīng)用程序的單個(gè)運(yùn)行實(shí)例同時(shí)為多個(gè)客戶機(jī)(租戶)服務(wù)的體系結(jié)構(gòu)。這在SaaS解決方案中非常常見。在這些系統(tǒng)中,隔離與各種租戶相關(guān)的信息(數(shù)據(jù)、定制等)是一個(gè)特殊的挑戰(zhàn)。這包括存儲(chǔ)在數(shù)據(jù)庫中的每個(gè)租戶擁有的數(shù)據(jù)。以下是三種常用的多租戶架構(gòu)實(shí)現(xiàn)方案:
1.1 獨(dú)立數(shù)據(jù)庫(Separate database)
圖片
每個(gè)租戶的數(shù)據(jù)都保存在一個(gè)物理上獨(dú)立的數(shù)據(jù)庫實(shí)例中。JDBC連接將專門指向每個(gè)數(shù)據(jù)庫,因此任何池都將按租戶進(jìn)行。這里,一種通用的應(yīng)用程序方法是為每個(gè)租戶定義JDBC連接池,并根據(jù)與當(dāng)前登錄用戶相關(guān)聯(lián)的租戶標(biāo)識(shí)符來選擇要使用的池。
優(yōu)點(diǎn):
- 數(shù)據(jù)隔離級(jí)別高,安全性好
- 可以根據(jù)租戶的需求進(jìn)行數(shù)據(jù)庫優(yōu)化和擴(kuò)展
- 備份和恢復(fù)操作相對簡單
缺點(diǎn):
- 成本較高,需要為每個(gè)租戶購買和維護(hù)獨(dú)立的數(shù)據(jù)庫實(shí)例
- 可能存在硬件資源浪費(fèi),因?yàn)槊總€(gè)租戶可能只使用了數(shù)據(jù)庫的一部分功能
1.2 獨(dú)立Schema(Separate schema)
每個(gè)租戶的數(shù)據(jù)都保存在單個(gè)數(shù)據(jù)庫實(shí)例上的不同數(shù)據(jù)庫Schema中。這里有兩種不同的定義JDBC連接的方法:
- 連接可以特定地指向每個(gè)Schema,就像單獨(dú)的數(shù)據(jù)庫方法中那樣。這是一個(gè)選項(xiàng),前提是驅(qū)動(dòng)程序支持在連接URL中命名默認(rèn)Schema,或者池機(jī)制支持命名用于其連接的Schema。使用這種方法,我們將為每個(gè)租戶創(chuàng)建一個(gè)不同的JDBC連接池,使用的連接池將基于與當(dāng)前登錄用戶相關(guān)聯(lián)的“租戶標(biāo)識(shí)符”進(jìn)行選擇。
- 連接可以指向數(shù)據(jù)庫本身(使用某些默認(rèn)Schema),但使用SQL SET schema(或類似的)命令可以更改連接。使用這種方法,我們將有一個(gè)JDBC連接池用于為所有租戶提供服務(wù),但在使用連接之前,它將被更改為引用由與當(dāng)前登錄用戶關(guān)聯(lián)的“租戶標(biāo)識(shí)符”命名的模式。
優(yōu)點(diǎn):
- 降低了數(shù)據(jù)庫成本,因?yàn)槎鄠€(gè)租戶共享一個(gè)數(shù)據(jù)庫實(shí)例
- 數(shù)據(jù)隔離級(jí)別仍然較高,因?yàn)槊總€(gè)租戶使用獨(dú)立的模式
缺點(diǎn):
- 模式之間可能存在資源競爭和性能瓶頸
- 備份和恢復(fù)操作可能更加復(fù)雜,因?yàn)樾枰槍γ總€(gè)模式進(jìn)行單獨(dú)操作
1.3 分區(qū)數(shù)據(jù)(Partitioned (discriminator) data)
所有數(shù)據(jù)都保存在一個(gè)數(shù)據(jù)庫Schema中。通過使用分區(qū)列對每個(gè)租戶的數(shù)據(jù)進(jìn)行分區(qū)。這種方法將使用單個(gè)連接池為所有租戶提供服務(wù)。但是,在這種方法中,應(yīng)用程序需要對每個(gè)SQL語句添加分區(qū)列(查詢時(shí)where條件加入分區(qū)列作為查詢條件)。
優(yōu)點(diǎn):
- 成本最低,因?yàn)樗凶鈶舳脊蚕硗粋€(gè)數(shù)據(jù)庫實(shí)例和模式
- 數(shù)據(jù)訪問和查詢效率可能較高,因?yàn)閿?shù)據(jù)都在同一個(gè)表中
缺點(diǎn):
- 數(shù)據(jù)隔離級(jí)別最低,可能存在安全風(fēng)險(xiǎn)
- 需要通過應(yīng)用程序邏輯來確保數(shù)據(jù)的正確隔離和訪問控制
- 數(shù)據(jù)備份和恢復(fù)操作可能非常復(fù)雜,因?yàn)樾枰紤]到所有租戶的數(shù)據(jù)
接下來我會(huì)對分區(qū)數(shù)據(jù)和獨(dú)立數(shù)據(jù)庫2種架構(gòu)進(jìn)行詳細(xì)的介紹。獨(dú)立Schema方案其實(shí)與獨(dú)立數(shù)據(jù)庫模式挺像的,如果基于MySQL其實(shí)對應(yīng)的就是不同數(shù)據(jù)庫(可以是同一個(gè)MySQL實(shí)例,通過use xxx切換數(shù)據(jù)庫),基于Oracle就是對應(yīng)不同的用戶上(并非schema與用戶等同)。
2. 實(shí)戰(zhàn)案例
2.1 分區(qū)數(shù)據(jù)
注:請先確保你當(dāng)前使用的SpringBoot版本(Spring Data JPA)整合的Hibernate版本至少是6.0版本以上。
實(shí)體定義
@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注解標(biāo)注,該字段專門用來分區(qū)租戶的,Hibernate在查詢數(shù)據(jù)時(shí)會(huì)自動(dòng)添加該查詢條件,如果你使用的本地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ā)一個(gè)業(yè)務(wù)功能的基本操作,接下來才是重點(diǎn)
租戶標(biāo)識(shí)解析處理
該的作用獲取當(dāng)前租戶ID,這里基于ThreadLocal實(shí)現(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;
}
}
上面的組件用來從當(dāng)前的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的解析器。
配置租戶標(biāo)識(shí)解析器
spring:
jpa:
properties:
hibernate:
'[tenant_identifier_resolver]': 'com.pack.tenant.config.TenantIdResolver'
完成以上類及配置的編寫后就實(shí)現(xiàn)了基于列區(qū)分(分區(qū))的多租戶架構(gòu)方案。
測試
準(zhǔn)備數(shù)據(jù):
圖片
圖片
圖片
SQL執(zhí)行情況:
圖片
自動(dòng)添加了tenant_id查詢條件。
2.2 獨(dú)立數(shù)據(jù)庫
每租戶對應(yīng)一個(gè)數(shù)據(jù)庫,這需要在項(xiàng)目中配置多個(gè)數(shù)據(jù)源,同時(shí)提供一個(gè)數(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實(shí)現(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è)置當(dāng)前要操作的數(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ù)源的所有類及配置的編寫。