環境:SpringBoot3.3.0
多租戶表示應用程序的單個運行實例同時為多個客戶機(租戶)服務的體系結構。這在SaaS解決方案中非常常見。在這些系統中,隔離與各種租戶相關的信息(數據、定制等)是一個特殊的挑戰。這包括存儲在數據庫中的每個租戶擁有的數據。以下是三種常用的多租戶架構實現方案:
圖片
每個租戶的數據都保存在一個物理上獨立的數據庫實例中。JDBC連接將專門指向每個數據庫,因此任何池都將按租戶進行。這里,一種通用的應用程序方法是為每個租戶定義JDBC連接池,并根據與當前登錄用戶相關聯的租戶標識符來選擇要使用的池。
優點:
缺點:
每個租戶的數據都保存在單個數據庫實例上的不同數據庫Schema中。這里有兩種不同的定義JDBC連接的方法:
優點:
缺點:
所有數據都保存在一個數據庫Schema中。通過使用分區列對每個租戶的數據進行分區。這種方法將使用單個連接池為所有租戶提供服務。但是,在這種方法中,應用程序需要對每個SQL語句添加分區列(查詢時where條件加入分區列作為查詢條件)。
優點:
缺點:
接下來我會對分區數據和獨立數據庫2種架構進行詳細的介紹。獨立Schema方案其實與獨立數據庫模式挺像的,如果基于MySQL其實對應的就是不同數據庫(可以是同一個MySQL實例,通過use xxx切換數據庫),基于Oracle就是對應不同的用戶上(并非schema與用戶等同)。
注:請先確保你當前使用的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注解標注,該字段專門用來分區租戶的,Hibernate在查詢數據時會自動添加該查詢條件,如果你使用的本地SQL(自己編寫SQL),那么需要你自行添加該條件(租戶ID條件)。
// DAOpublic interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor<Person> {}// Service@Servicepublic class PersonService { private final PersonRepository personRepository ; public PersonService(PersonRepository personRepository) { this.personRepository = personRepository ; } // 查詢所有Person數據 public List<Person> persons() { return this.personRepository.findAll() ; }}
@GetMapping("")public List<Person> persons() { return this.personService.persons() ;}
以上是開發一個業務功能的基本操作,接下來才是重點
該的作用獲取當前租戶ID,這里基于ThreadLocal實現
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。
該攔截器的作用用來從請求Header中獲取租戶ID,存入ThreadLocal中。
@Componentpublic 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,設置租戶ID的解析器。
spring: jpa: properties: hibernate: '[tenant_identifier_resolver]': 'com.pack.tenant.config.TenantIdResolver'
完成以上類及配置的編寫后就實現了基于列區分(分區)的多租戶架構方案。
準備數據:
圖片
圖片
圖片
SQL執行情況:
圖片
自動添加了tenant_id查詢條件。
每租戶對應一個數據庫,這需要在項目中配置多個數據源,同時提供一個數據源路由的核心類。
你也可以將數據源的信息專門存放在數據表中。
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實現多數據源切換,可以通過繼承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() ; }}
@Configurationpublic 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(); }}
接下來定義攔截器,設置當前要操作的數據源。
@Componentpublic 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 ; }}
以上就完成了多數據源的所有類及配置的編寫。
本文鏈接:http://www.tebozhan.com/showinfo-26-91376-0.htmlSpringBoot多租戶三種架構實現方案詳解
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 聊聊主流消息隊列的認證和鑒權!