天天看點

SpringBoot 內建 分庫分表ShardingSphere ShardingJDBC

一、前言

系統出現的第一個瓶頸往往是資料庫瓶頸。ShardingJDBC是ShardingSphere的一個子項,它提供資料庫分庫分表、讀寫分離等解決方案。像是京東、當當、唯品會、轉轉、中通、三隻松鼠等都在用ShardingSphere。

一句話總結:它是國産的、開源的、配置簡單的一套開源的分布式資料庫解決方案。他的作者是張亮 https://github.com/terrymanu

官網位址:https://shardingsphere.apache.org/

二、準備工作

建立

sharding_0

sharding_1

兩個資料庫。兩個資料庫表字段全部一樣,資料庫腳本如下:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `company`;
CREATE TABLE `company`  (
  `id` bigint(20) NOT NULL COMMENT '主鍵id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名稱',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '建立時間',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission`  (
  `id` bigint(20) NOT NULL COMMENT '主鍵id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名稱',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '建立時間',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `product`;
CREATE TABLE `product`  (
  `id` bigint(20) NOT NULL COMMENT '主鍵id',
  `company_id` bigint(20) NULL DEFAULT NULL COMMENT '公司id',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名稱',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '建立時間',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

           

三、pom.xml

主要添加

mybatis

mybatis-plus

mysql

sharding-jdbc

,如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.llh</groupId>
    <artifactId>spring-boot-sharding-jdbc</artifactId>
    <version>1.0.0</version>
    <name>spring-boot-sharding-jdbc</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <mybatis-spring-boot.version>2.1.4</mybatis-spring-boot.version>
        <mybatis-plus.version>3.4.2</mybatis-plus.version>
        <hutool.version>5.7.9</hutool.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>

    </dependencies>

</project>
           

四、application.properties

  • 這裡配置兩個資料庫節點

    ds0

    ds1

    ,分别對應兩個資料庫

    sharding_0

    sharding_1

  • 公司表

    company

    根據

    id

    分庫,規則為

    ds$->{id % 2}

    ,也就是偶數在

    sharding_0

    庫,奇數在

    sharding_1

    庫。
  • 商品表

    product

    根據

    company_id

    預設分庫,規則為

    ds$->{company_id % 2}

  • 權限表

    permission

    為廣播表,每個庫資料都一樣。
# 應用名稱
spring.application.name=sharding-jdbc
# mybatis相關
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
# sharding jdbc
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.props.sql.show=true
# ds0
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/sharding_0?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=0320
# ds1
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/sharding_1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=0320
# 預設資料庫分庫規則
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=company_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{company_id % 2}
# 廣播表
spring.shardingsphere.sharding.broadcast-tables=permission
# 自定義分片規則
spring.shardingsphere.sharding.tables.company.actual-data-nodes=ds$->{0..1}.company
spring.shardingsphere.sharding.tables.company.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.company.database-strategy.inline.algorithm-expression=ds$->{id % 2}

           

五、說明

Sharding-JDBC有邏輯sql和實際sql的概念。如下:

Logic SQL: SELECT id,name,create_time,update_time FROM company WHERE id=? 

Actual SQL: ds1 ::: SELECT id,name,create_time,update_time FROM company WHERE id=?  ::: [1]
           

比如查詢id為1的公司,這裡查找的資料源就是

ds1

像是多租戶的表,比如商品資料是屬于某個公司的,就根據公司id分庫分表

像是權限表、字典表、系統參數表等,資料量不大,但是通路頻繁,就是廣播表,每張表的資料都一樣。

六、結語

相關的接口業務代碼這裡就不貼了,可以通路github源碼檢視,自己動手試一下,很簡單的。

源碼位址:https://github.com/tigerleeli/xiaohuge-blog/tree/master/spring-boot-sharding-jdbc

同步微信公衆号:小虎哥的技術部落格

繼續閱讀