sharing-jdbc实现读写分离及分库分表


需求:

分库:按业务线business_id将不同业务线的订单存储在不同的数据库上;

分表:按user_id字段将不同用户的订单存储在不同的表上,为方便直接用非分片字段order_id查询,可使用基因法;

读写分离:为缓解主库的压力,读操作访问从库;

库表SQL

-- 主库
CREATE DATABASE `database_103`;
CREATE DATABASE `database_112`;

-- 从库
CREATE DATABASE `database_slave_103`;
CREATE DATABASE `database_slave_112`;

--每个库上分别建立如下表
CREATE TABLE `t_order_0` (
  `id` bigint(20) NOT NULL,
  `order_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `business_id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `t_order_1` (
  `id` bigint(20) NOT NULL,
  `order_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `business_id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `t_order_2` (
  `id` bigint(20) NOT NULL,
  `order_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `business_id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `t_order` VALUES (1,1,112);

CREATE TABLE `t_order_3` (
  `id` bigint(20) NOT NULL,
  `order_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `business_id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

pom.xml

        
        
            com.dangdang
            sharding-jdbc-core
            1.4.2
        
        
            com.dangdang
            sharding-jdbc-config-spring
            1.4.0
        

spring-database.xml

<?xml version="1.0" encoding="UTF-8"?>

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd

                        http://www.dangdang.com/schema/ddframe/rdb
                        http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd">

    class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        
        
        
        
        
        
        
        
    

    class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        
        
        
        
        
        
        
        
    

    class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        
        
        
        
        
        
        
        
    

    class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        
        
        
        
        
        
        
        
    

    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        shardingDataSource"/>
        
        
    
    class="org.mybatis.spring.SqlSessionTemplate">
        
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
    

    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        shardingDataSource"/>
    
    
    
    rbb_112" master-data-source-ref="database_112"
                                  slave-data-sources-ref="database_slave_112"/>
    rbb_103" master-data-source-ref="database_103"
                                  slave-data-sources-ref="database_slave_103"/>
    
    databaseShardingStrategy" sharding-columns="business_id"
                  algorithm-expression="rbb_${business_id.longValue()}"/>
    
    tableShardingStrategy" sharding-columns="user_id,order_id"
                  algorithm-class="com.lc.sharding.algorithm.MultipleKeysTableShardingAlgorithmImpl"/>

    
        
            
                
                                table-strategy="tableShardingStrategy"/>
            
        
        
            true
            true
        
    


基因法多列分片

public class MultipleKeysTableShardingAlgorithmImpl implements MultipleKeysTableShardingAlgorithm {
    public Collection doSharding(Collection tableNames, Collection> shardingValues) {
        List shardingSuffix = new ArrayList();
        long partId = 0;
        for (ShardingValue value : shardingValues) {
            if (value.getColumnName().equals("user_id")) {
                partId = ((Long) value.getValue()) % 4;
                break;
            } else if (value.getColumnName().equals("order_id")) {
                partId = ((Long) value.getValue()) % 4;
                break;
            }
        }
        for (String name : tableNames) {
            if (name.endsWith(partId + "")) {
                shardingSuffix.add(name);
                return shardingSuffix;
            }
        }
        return shardingSuffix;
    }
}

什么是基因法分片?

在订单数据oid生成时,order_id末端加入分片基因,让同一个user_id下的所有订单都含有相同基因,落在同一个表上

资料:https://mp.weixin.qq.com/s/PCzRAZa9n4aJwHOX-kAhtA

根据user_id生成order_id:

    public long bulidOrderId(long userId) {
        //取用户id后4位
        userId = userId & 15;
        //先取60位唯一id
        long uniqueId = this.nextId();
        //唯一id左移4位、拼接userId后4位
        return (uniqueId << 4) | userId;
    }
this.nextId();//使用雪花算法生成60位分布式唯一id:1位符号位+41位时间戳+5位workId+5位datacenterId+6位序列号+4位基因片

 小结

数据分片:

  • 支持分库+分表;
  • 可支持 = , BETWEEN,IN等多维度分片,也支持多分片键共用;
  • 支持聚合,分组,排序,分页,关联等复杂查询语句;
  • 分片灵活,支持多分片键共用,支持inline表达式;
  • 基于Hint的强制路由;
  • 支持分布式主键

读写分离:

  • 支持一主多从的读写分离;
  • 支持分库分表与读写分离共同使用
  • 支持分布式生成全局主键。

柔性事务:

  • 最大努力到达型事务

分布式治理:

  • 支持配置中心,可动态修改
  • 支持客户端熔断和失效转移

引用:http://shardingsphere.io/