Sharding-Sphere实战:实现类多租户分库分表

分表模式

以分表模式实现类多租户的意思是,假定基础表是tb_order,那么租户A对应的表是tb_order_a,租户B对应的表是tb_order_b。接下来借助sharding-sphere-3.1.0这个优秀的分库分表中间件实现一个这样的需求:作为一个支付平台,给多个商户提供服务,要求每个商户的订单表独立且以商户名为后缀,例如taobao的订单表是tb_order_taobao,tmall的订单表是tb_order_tmall,aliyun的订单表是tb_order_aliyun。

订单表DDL如下:

CREATE TABLE tb_order (
  id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id int(11) NOT NULL COMMENT '姓名',
  order_no varchar(32) NOT NULL COMMENT '订单号',
  order_time datetime NOT NULL COMMENT '订单时间',
  merchant varchar(16) NOT NULL COMMENT '商户名称',
  UNIQUE KEY uk_order_no (order_no)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

核心Maven依赖:

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

集成sharding-sphere的核心配置文件部分内容如下:

# 数据库名称(因为只分表不分库,所以数据源只有一个)
sharding.jdbc.datasource.names=afei

sharding.jdbc.datasource.afei.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.afei.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.afei.url=jdbc:mysql://127.0.0.1:3306/afei?useUnicode=true&characterEncoding=utf8
sharding.jdbc.datasource.afei.username=root
sharding.jdbc.datasource.afei.password=root

sharding.jdbc.config.sharding.default-data-source-name=afei
# 默认数据源不用分库分表,所以不需要配置sharding.jdbc.config.sharding.default-database-strategy...

sharding.jdbc.config.sharding.tables.tb_order.actual-data-nodes=afei.tb_order_tmall,afei.tb_order_taobao,afei.tb_order_aliyun
# 分片键(重要)
sharding.jdbc.config.sharding.tables.tb_order.table-strategy.standard.sharding-column=merchant
# 自定义分表算法(重要)
sharding.jdbc.config.sharding.tables.tb_order.table-strategy.standard.precise-algorithm-class-name=com.afei.boot.util.OrderShardingAlgorithm
        
sharding.jdbc.config.props.sql.show=true

自定义分表算法OrderShardingAlgorithm.java的源码如下:

public class OrderShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<String> shardingValue) {
        String targetTable = "tb_order_" + shardingValue.getValue();
        if (availableTargetNames.contains(targetTable)){
            return targetTable;
        }

        throw new UnsupportedOperationException("无法判定的值: " + shardingValue.getValue());
    }
}

由于笔者采用springboot+mybatis+shardingsphere的方案,所以sql都在mapper.xml中,订单保存接口涉及的SQL如下:

<insert id="save" parameterType="com.afei.sjdbc.model.Order">
    insert into tb_order (user_id, order_no, order_time, merchant)
    values (#{userId}, #{orderNo}, #{orderTime}, #{merchant})
</insert>

这个实例只以测试用例启动验证,测试用例代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = OrderSpringBootTests.class)
@SpringBootApplication
@ActiveProfiles("sharding")
public class OrderSpringBootTests {

    @Resource
    private OrderService orderService;

    @Test
    public void save(){
        List<String> merchantList = Lists.newArrayList("aliyun", "taobao", "tmall");
        // 测试20笔订单,且所属商户随机产生
        for (int i = 0; i < 20; i++) {
            Order order = new Order();
            order.setUserId(i);
            order.setOrderNo(System.currentTimeMillis() + String.format("%06d", i));
            order.setOrderTime(new Date());
            order.setMerchant(merchantList.get(new Random().nextInt(merchantList.size())));
            orderService.save(order);

        }
    }

}

运行测试用例后的部分输出部分日志如下(需要sharding.jdbc.config.props.sql.show=true才能输出这些日志,是sharding-sphere记录的SQL重写前后的日志),从日志中我们可以看到,商户tmall的订单最终持久化在表tb_order_tmall中:

2019-06-02 21:27:43.270  INFO 15415 --- [           main] ShardingSphere-SQL                       : Logic SQL: insert into tb_order (user_id, order_no, order_time, merchant) values (?, ?, ?, ?)
... ...
2019-06-02 21:27:43.271  INFO 15415 --- [           main] ShardingSphere-SQL                       : Actual SQL: afei ::: insert into tb_order_tmall (user_id, order_no, order_time, merchant) values (?, ?, ?, ?) ::: [[6, 1559482063270000006, 2019-06-02 21:27:43.27, tmall]]

tb_order_tmall表中该订单记录如下:

mysql> select * from tb_order_tmall where order_no='1559482063270000006';
+----+---------+---------------------+---------------------+----------+
| id | user_id | order_no            | order_time          | merchant |
+----+---------+---------------------+---------------------+----------+
|  6 |       6 | 1559482063270000006 | 2019-06-02 08:27:43 | tmall    |
+----+---------+---------------------+---------------------+----------+
1 row in set (0.00 sec)

分库模式

有些业务的需求可能是每个接入的商户对应一个对立的数据库实例,这样的话,底层存储会更清晰,权限也比较好控制。

如此一来,商户taobao的订单表是pay_taobao.tb_order(即表示数据库pay_taobao中的表tb_order),商户tmall的订单表是pay_tmall.tb_order,商户aliyun的订单表是pay_aliyun.tb_order。这种设计,相比前面的分表模式,改动的不大,主要改动涉及如下几个地方。

首先,需要创建多个数据库,例如pay_aliyun,pay_taobao,pay_tmall。每个库中都有表tb_order。

其次,核心配置文件有比较大的改动,分库模式配置如下:

# 3个商户,3个数据库
sharding.jdbc.datasource.names=pay_aliyun,pay_tmall,pay_taobao

sharding.jdbc.datasource.pay_aliyun.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.pay_aliyun.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.pay_aliyun.url=jdbc:mysql://127.0.0.1:3306/pay_aliyun?useUnicode=true&characterEncoding=utf8
sharding.jdbc.datasource.pay_aliyun.username=root
sharding.jdbc.datasource.pay_aliyun.password=root

sharding.jdbc.datasource.pay_taobao.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.pay_taobao.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.pay_taobao.url=jdbc:mysql://127.0.0.1:3306/pay_taobao?useUnicode=true&characterEncoding=utf8
sharding.jdbc.datasource.pay_taobao.username=root
sharding.jdbc.datasource.pay_taobao.password=root

sharding.jdbc.datasource.pay_tmall.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.pay_tmall.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.pay_tmall.url=jdbc:mysql://127.0.0.1:3306/pay_tmall?useUnicode=true&characterEncoding=utf8
sharding.jdbc.datasource.pay_tmall.username=root
sharding.jdbc.datasource.pay_tmall.password=root

sharding.jdbc.config.sharding.tables.tb_order.actual-data-nodes=pay_tmall.tb_order,pay_taobao.tb_order,pay_aliyun.tb_order
# 根据merchant列进行分库
sharding.jdbc.config.sharding.default-database-strategy.standard.sharding-column=merchant
# 自定义分库算法
sharding.jdbc.config.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.afei.boot.util.DbShardingAlgorithm

sharding.jdbc.config.props.sql.show=true

自定义分库算法DbShardingAlgorithm.java的源码如下:

public class DbShardingAlgorithm implements PreciseShardingAlgorithm<String> {
    private static final String DB_NAME_PREFIX = "pay_";
    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<String> shardingValue) {
        String targetTable = DB_NAME_PREFIX + shardingValue.getValue();
        if (availableTargetNames.contains(targetTable)){
            return targetTable;
        }
        throw new UnsupportedOperationException("无法判定的值: " + shardingValue.getValue());
    }
}

再次,测试用例和SQL都没有任何改动,业务代码只需要知道往表tb_order中插入订单即可,至于实际会插入哪个数据库中,sharding-sphere会帮你搞定。

最后,运行测试用例,得到部分sharding-sphere输出日志如下,由日志可知,tmall这个商户的订单保存在pay_tmall这个数据库中:

2019-06-02 22:28:56.091  INFO 15578 --- [           main] ShardingSphere-SQL                       : Logic SQL: insert into tb_order (user_id, order_no, order_time, merchant)values (?, ?, ?, ?)
2019-06-02 22:28:56.091  INFO 15578 --- [           main] ShardingSphere-SQL                       : Actual SQL: pay_tmall ::: insert into tb_order (user_id, order_no, order_time, merchant)values (?, ?, ?, ?) ::: [[2, 1559485736091000002, 2019-06-02 22:28:56.091, tmall]]

pay_tmall库中tb_order表这条订单数据如下所示:

mysql> select * from pay_tmall.tb_order where order_no='1559485736091000002';
+----+---------+---------------------+---------------------+----------+
| id | user_id | order_no            | order_time          | merchant |
+----+---------+---------------------+---------------------+----------+
|  5 |       2 | 1559485736091000002 | 2019-06-02 09:28:56 | tmall    |
+----+---------+---------------------+---------------------+----------+
1 row in set (0.00 sec)

代码地址

https://github.com/feelwing1314/shardingsphere-multitenancy