MybatisPlus 多租戶架構(Multi-tenancy)實現

在進行多租戶架構(Multi-tenancy)實現以前,先了解一下相關的定義吧:html

什麼是多租戶

多租戶技術或稱多重租賃技術,簡稱SaaS,是一種軟件架構技術,是實現如何在多用戶環境下(此處的多用戶通常是面向企業用戶)共用相同的系統或程序組件,而且可確保各用戶間數據的隔離性。
簡單講:在一臺服務器上運行單個應用實例,它爲多個租戶(客戶)提供服務。從定義中咱們能夠理解:多租戶是一種架構,目的是爲了讓多用戶環境下使用同一套程序,且保證用戶間數據隔離。那麼重點就很淺顯易懂了,多租戶的重點就是同一套程序下實現多用戶數據的隔離。java

數據隔離方案

多租戶在數據存儲上存在三種主要的方案,分別是:spring

獨立數據庫

即一個租戶一個數據庫,這種方案的用戶數據隔離級別最高,安全性最好,但成本較高。sql

  • 優勢:爲不一樣的租戶提供獨立的數據庫,有助於簡化數據模型的擴展設計,知足不一樣租戶的獨特需求;若是出現故障,恢復數據比較簡單。
  • 缺點:增多了數據庫的安裝數量,隨之帶來維護成本和購置成本的增長。

共享數據庫,獨立 Schema

多個或全部租戶共享Database,可是每一個租戶一個Schema(也可叫作一個user)。底層庫好比是:DB二、ORACLE等,一個數據庫下能夠有多個SCHEMA。數據庫

  • 優勢:爲安全性要求較高的租戶提供了必定程度的邏輯數據隔離,並非徹底隔離;每一個數據庫可支持更多的租戶數量。
  • 缺點:若是出現故障,數據恢復比較困難,由於恢復數據庫將牽涉到其餘租戶的數據;

共享數據庫,共享 Schema,共享數據表

即租戶共享同一個Database、同一個Schema,但在表中增長TenantID多租戶的數據字段。這是共享程度最高、隔離級別最低的模式。apache

簡單來說,即每插入一條數據時都須要有一個客戶的標識。這樣才能在同一張表中區分出不一樣客戶的數據,這也是咱們系統目前用到的(provider_id)api

  • 優勢:三種方案比較,第三種方案的維護和購置成本最低,容許每一個數據庫支持的租戶數量最多。
  • 缺點:隔離級別最低,安全性最低,須要在設計開發時加大對安全的開發量; 數據備份和恢復最困難,須要逐表逐條備份和還原。

<!--- more --->安全

利用MybatisPlus實現

這裏咱們選用了第三種方案(共享數據庫,共享 Schema,共享數據表)來實現,也就意味着,每一個數據表都須要有一個租戶標識(provider_id)服務器


如今有數據庫表(user)以下:mybatis

字段名 字段類型 描述
id BIGINT(20) 主鍵
provider_id BIGINT(20) 服務商ID
name VARCHAR(30) 姓名

provider_id視爲租戶ID,用來隔離租戶與租戶之間的數據,若是要查詢當前服務商的用戶,SQL大體以下:

SELECT * FROM user t WHERE t.name LIKE '%Tom%' AND t.provider_id = 1;

試想一下,除了一些系統共用的表之外,其餘租戶相關的表,咱們都須要不厭其煩的加上AND t.provider_id = ?查詢條件,稍不注意就會致使數據越界,數據安全問題讓人擔心。

好在有了MybatisPlus這個神器,能夠極爲方便的實現多租戶SQL解析器,官方文檔以下:
http://mp.baomidou.com/guide/...

這裏終於進入了正題,開始搭建一個極爲簡單的開發環境吧!

新建SpringBoot環境

POM文件以下,主要集成了MybatisPlus以及H2數據庫(方便測試)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wuwenze</groupId>
    <artifactId>mybatis-plus-multi-tenancy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatis-plus-multi-tenancy</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
數據源配置(application.yml)
spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema.sql
    data: classpath:db/data.sql
    url: jdbc:h2:mem:test
    username: root
    password: test

logging:
  level:
    com.wuwenze.mybatisplusmultitenancy: debug
對應的H2數據庫初始化schema文件
#schema.sql
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主鍵',
    provider_id BIGINT(20) NOT NULL COMMENT '服務商ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    PRIMARY KEY (id)
);


#data.sql
INSERT INTO user (id, provider_id, name) VALUES (1, 1, 'Tony老師');
INSERT INTO user (id, provider_id, name) VALUES (2, 1, 'William老師');
INSERT INTO user (id, provider_id, name) VALUES (3, 2, '路人甲');
INSERT INTO user (id, provider_id, name) VALUES (4, 2, '路人乙');
INSERT INTO user (id, provider_id, name) VALUES (5, 2, '路人丙');
INSERT INTO user (id, provider_id, name) VALUES (6, 2, '路人丁');

MybatisPlus Config

基礎環境搭建完成,如今開始配置MybatisPlus多租戶相關的實現。

1) 核心配置:TenantSqlParser

@Configuration
@MapperScan("com.wuwenze.mybatisplusmultitenancy.mapper")
public class MybatisPlusConfig {

    private static final String SYSTEM_TENANT_ID = "provider_id";
    private static final List<String> IGNORE_TENANT_TABLES = Lists.newArrayList("provider");

    @Autowired
    private ApiContext apiContext;

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();

        // SQL解析處理攔截:增長租戶處理回調。
        TenantSqlParser tenantSqlParser = new TenantSqlParser()
                .setTenantHandler(new TenantHandler() {

                    @Override
                    public Expression getTenantId() {
                        // 從當前系統上下文中取出當前請求的服務商ID,經過解析器注入到SQL中。
                        Long currentProviderId = apiContext.getCurrentProviderId();
                        if (null == currentProviderId) {
                            throw new RuntimeException("#1129 getCurrentProviderId error.");
                        }
                        return new LongValue(currentProviderId);
                    }

                    @Override
                    public String getTenantIdColumn() {
                        return SYSTEM_TENANT_ID;
                    }

                    @Override
                    public boolean doTableFilter(String tableName) {
                        // 忽略掉一些表:如租戶表(provider)自己不須要執行這樣的處理。
                        return IGNORE_TENANT_TABLES.stream().anyMatch((e) -> e.equalsIgnoreCase(tableName));
                    }
                });
        paginationInterceptor.setSqlParserList(Lists.newArrayList(tenantSqlParser));
        return paginationInterceptor;
    }

    @Bean(name = "performanceInterceptor")
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
}

2) ApiContext

@Component
public class ApiContext {
    private static final String KEY_CURRENT_PROVIDER_ID = "KEY_CURRENT_PROVIDER_ID";
    private static final Map<String, Object> mContext = Maps.newConcurrentMap();

    public void setCurrentProviderId(Long providerId) {
        mContext.put(KEY_CURRENT_PROVIDER_ID, providerId);
    }

    public Long getCurrentProviderId() {
        return (Long) mContext.get(KEY_CURRENT_PROVIDER_ID);
    }
}

3) Entity、Mapper

@Data
@ToString
@Accessors(chain = true)
public class User {
    private Long id;
    private Long providerId;
    private String name;
}

public interface UserMapper extends BaseMapper<User> {

}

單元測試

com.wuwenze.mybatisplusmultitenancy.MybatisPlusMultiTenancyApplicationTests
@Slf4j
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.JVM)
@SpringBootTest(classes = MybatisPlusMultiTenancyApplication.class)
public class MybatisPlusMultiTenancyApplicationTests {


    @Autowired
    private ApiContext apiContext;

    @Autowired
    private UserMapper userMapper;

    @Before
    public void before() {
        // 在上下文中設置當前服務商的ID
        apiContext.setCurrentProviderId(1L);
    }

    @Test
    public void insert() {
        User user = new User().setName("新來的Tom老師");
        Assert.assertTrue(userMapper.insert(user) > 0);

        user = userMapper.selectById(user.getId());
        log.info("#insert user={}", user);

        // 檢查插入的數據是否自動填充了租戶ID
        Assert.assertEquals(apiContext.getCurrentProviderId(), user.getProviderId());
    }

    @Test
    public void selectList() {
        userMapper.selectList(null).forEach((e) -> {
            log.info("#selectList, e={}", e);
            // 驗證查詢的數據是否超出範圍
            Assert.assertEquals(apiContext.getCurrentProviderId(), e.getProviderId());
        });
    }
}
運行結果
2018-11-29 21:07:14.262  INFO 18688 --- [           main] .MybatisPlusMultiTenancyApplicationTests : Started MybatisPlusMultiTenancyApplicationTests in 2.629 seconds (JVM running for 3.904)
2018-11-29 21:07:14.554 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.insert           : ==>  Preparing: INSERT INTO user (id, name, provider_id) VALUES (?, ?, 1)
2018-11-29 21:07:14.577 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.insert           : ==> Parameters: 1068129257418178562(Long), 新來的Tom老師(String)
2018-11-29 21:07:14.577 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.insert           : <==    Updates: 1
 Time:0 ms - ID:com.wuwenze.mybatisplusmultitenancy.mapper.UserMapper.insert
Execute SQL:INSERT INTO user (id, name, provider_id) VALUES (?, ?, 1) {1: 1068129257418178562, 2: STRINGDECODE('\u65b0\u6765\u7684Tom\u8001\u5e08')}

2018-11-29 21:07:14.585 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectById       : ==>  Preparing: SELECT id, provider_id, name FROM user WHERE user.provider_id = 1 AND id = ?
2018-11-29 21:07:14.595 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectById       : ==> Parameters: 1068129257418178562(Long)
2018-11-29 21:07:14.614 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectById       : <==      Total: 1
2018-11-29 21:07:14.615  INFO 18688 --- [           main] .MybatisPlusMultiTenancyApplicationTests : #insert user=User(id=1068129257418178562, providerId=1, name=新來的Tom老師)
 Time:19 ms - ID:com.wuwenze.mybatisplusmultitenancy.mapper.UserMapper.selectById
Execute SQL:SELECT id, provider_id, name FROM user WHERE user.provider_id = 1 AND id = ? {1: 1068129257418178562}

2018-11-29 21:07:14.626 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectList       : ==>  Preparing: SELECT id, provider_id, name FROM user WHERE user.provider_id = 1
 Time:0 ms - ID:com.wuwenze.mybatisplusmultitenancy.mapper.UserMapper.selectList
Execute SQL:SELECT id, provider_id, name FROM user WHERE user.provider_id = 1

2018-11-29 21:07:14.629 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectList       : ==> Parameters:
2018-11-29 21:07:14.630 DEBUG 18688 --- [           main] c.w.m.mapper.UserMapper.selectList       : <==      Total: 3
2018-11-29 21:07:14.632  INFO 18688 --- [           main] .MybatisPlusMultiTenancyApplicationTests : #selectList, e=User(id=1, providerId=1, name=Tony老師)
2018-11-29 21:07:14.632  INFO 18688 --- [           main] .MybatisPlusMultiTenancyApplicationTests : #selectList, e=User(id=2, providerId=1, name=William老師)
2018-11-29 21:07:14.632  INFO 18688 --- [           main] .MybatisPlusMultiTenancyApplicationTests : #selectList, e=User(id=1068129257418178562, providerId=1, name=新來的Tom老師)

從打印的日誌不難看出,這個方案至關完美,僅需簡單的配置,讓開發者徹底忽略了(provider_id)字段的存在,同時又最大程度的保證了數據的安全性,可謂是一箭雙鵰!

相關文章
相關標籤/搜索