成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringBoot多租戶三種架構實現方案詳解

開發 前端
每個租戶的數據都保存在一個物理上獨立的數據庫實例中。JDBC連接將專門指向每個數據庫,因此任何池都將按租戶進行。這里,一種通用的應用程序方法是為每個租戶定義JDBC連接池,并根據與當前登錄用戶相關聯的租戶標識符來選擇要使用的池。

環境:SpringBoot3.3.0

1. 簡介

多租戶表示應用程序的單個運行實例同時為多個客戶機(租戶)服務的體系結構。這在SaaS解決方案中非常常見。在這些系統中,隔離與各種租戶相關的信息(數據、定制等)是一個特殊的挑戰。這包括存儲在數據庫中的每個租戶擁有的數據。以下是三種常用的多租戶架構實現方案:

1.1 獨立數據庫(Separate database)

圖片圖片

每個租戶的數據都保存在一個物理上獨立的數據庫實例中。JDBC連接將專門指向每個數據庫,因此任何池都將按租戶進行。這里,一種通用的應用程序方法是為每個租戶定義JDBC連接池,并根據與當前登錄用戶相關聯的租戶標識符來選擇要使用的池。

優點:

  • 數據隔離級別高,安全性好
  • 可以根據租戶的需求進行數據庫優化和擴展
  • 備份和恢復操作相對簡單

缺點:

  • 成本較高,需要為每個租戶購買和維護獨立的數據庫實例
  • 可能存在硬件資源浪費,因為每個租戶可能只使用了數據庫的一部分功能

1.2 獨立Schema(Separate schema)

圖片

每個租戶的數據都保存在單個數據庫實例上的不同數據庫Schema中。這里有兩種不同的定義JDBC連接的方法:

  • 連接可以特定地指向每個Schema,就像單獨的數據庫方法中那樣。這是一個選項,前提是驅動程序支持在連接URL中命名默認Schema,或者池機制支持命名用于其連接的Schema。使用這種方法,我們將為每個租戶創建一個不同的JDBC連接池,使用的連接池將基于與當前登錄用戶相關聯的“租戶標識符”進行選擇。
  • 連接可以指向數據庫本身(使用某些默認Schema),但使用SQL SET schema(或類似的)命令可以更改連接。使用這種方法,我們將有一個JDBC連接池用于為所有租戶提供服務,但在使用連接之前,它將被更改為引用由與當前登錄用戶關聯的“租戶標識符”命名的模式。

優點:

  • 降低了數據庫成本,因為多個租戶共享一個數據庫實例
  • 數據隔離級別仍然較高,因為每個租戶使用獨立的模式

缺點:

  • 模式之間可能存在資源競爭和性能瓶頸
  • 備份和恢復操作可能更加復雜,因為需要針對每個模式進行單獨操作

1.3 分區數據(Partitioned (discriminator) data)

圖片

所有數據都保存在一個數據庫Schema中。通過使用分區列對每個租戶的數據進行分區。這種方法將使用單個連接池為所有租戶提供服務。但是,在這種方法中,應用程序需要對每個SQL語句添加分區列(查詢時where條件加入分區列作為查詢條件)。

優點:

  • 成本最低,因為所有租戶都共享同一個數據庫實例和模式
  • 數據訪問和查詢效率可能較高,因為數據都在同一個表中

缺點:

  • 數據隔離級別最低,可能存在安全風險
  • 需要通過應用程序邏輯來確保數據的正確隔離和訪問控制
  • 數據備份和恢復操作可能非常復雜,因為需要考慮到所有租戶的數據

接下來我會對分區數據獨立數據庫2種架構進行詳細的介紹。獨立Schema方案其實與獨立數據庫模式挺像的,如果基于MySQL其實對應的就是不同數據庫(可以是同一個MySQL實例,通過use xxx切換數據庫),基于Oracle就是對應不同的用戶上(并非schema與用戶等同)。

2. 實戰案例

2.1 分區數據

注:請先確保你當前使用的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條件)。

編寫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數據
  public List<Person> persons() {
    return this.personRepository.findAll() ;
  }
}

Controller接口

@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。

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,設置租戶ID的解析器。

配置租戶標識解析器

spring:
  jpa:
    properties:
      hibernate:
        '[tenant_identifier_resolver]': 'com.pack.tenant.config.TenantIdResolver'

完成以上類及配置的編寫后就實現了基于列區分(分區)的多租戶架構方案。

測試

準備數據:

圖片圖片

圖片圖片

圖片圖片

SQL執行情況:

圖片圖片

自動添加了tenant_id查詢條件。

2.2 獨立數據庫

每租戶對應一個數據庫,這需要在項目中配置多個數據源,同時提供一個數據源路由的核心類。

定義多數據源配置

你也可以將數據源的信息專門存放在數據表中。

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() ; 
  }
}

配置數據源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();
  }
}

接下來定義攔截器,設置當前要操作的數據源。

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 ;
  }
}

以上就完成了多數據源的所有類及配置的編寫。

責任編輯:武曉燕 來源: Spring全家桶實戰案例源碼
相關推薦

2025-02-18 16:27:01

2024-09-20 05:49:04

SpringBoot后端

2024-07-08 09:03:31

2016-09-12 14:07:14

Android 定時器

2019-11-04 08:38:45

分布式事務主流TCC

2011-09-05 12:43:23

Sencha Touc事件

2023-12-14 12:26:16

SaaS數據庫方案

2023-06-07 13:50:00

SaaS多租戶系統

2009-12-01 09:18:22

Linux版本

2021-11-23 10:30:35

Android技術代碼

2014-05-23 09:23:55

多屏

2022-07-22 20:00:01

高可用路由

2022-03-22 10:24:48

Linux開源Elasticsea

2017-07-03 18:24:39

MySQL數據冗余

2023-08-14 17:58:13

RequestHTTP請求

2024-11-06 09:28:52

架構客戶端靜態

2010-09-10 08:54:52

DIV居中

2021-05-15 09:00:43

全光網光纖網絡

2021-11-05 21:33:28

Redis數據高并發

2014-12-31 17:42:47

LBSAndroid地圖
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩在线免费 | 亚洲精品一区二区在线观看 | japanhdxxxx裸体 | 日韩亚洲欧美综合 | 狠狠操电影 | 欧美久久久久 | 992tv人人草 久久精品超碰 | 亚洲一区二区欧美 | 在线中文字幕视频 | 精品毛片视频 | 国产成人精品一区二区三区四区 | 欧美激情国产精品 | 亚洲一区在线观看视频 | 久久精品免费观看 | 午夜精品一区二区三区在线观看 | 99在线资源 | 中文字幕在线免费 | 九九热这里只有精品在线观看 | 国产精品久久久久久久久久软件 | 国产午夜精品一区二区三区四区 | 伊人春色在线观看 | 麻豆久久久久久久久久 | 中文字幕高清视频 | 亚洲天堂一区 | 亚洲精品456 | 99在线精品视频 | 99精品视频一区二区三区 | 亚洲综合日韩精品欧美综合区 | av一级在线观看 | 色一情一乱一伦一区二区三区 | 国产91丝袜在线播放 | 精品久久久久久中文字幕 | 91网站在线播放 | 国产中文视频 | 日韩国产在线观看 | av在线黄| 亚洲国产精品人人爽夜夜爽 | 精品国产一区二区三区久久久四川 | 国产在线一区二 | 91精品国产色综合久久 | 狠狠伊人 |