shardindjdbc-complex策略

支持多分片键的复杂分片策略。
配置参数:complex.sharding-columns 分片键(多个);
complex.algorithm-class-name 分片算法实现类。
sql和源码
https://gitee.com/zhangjijige/shardingjdbc.git
配置

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    # 参数配置,显示 sql
    props:
      sql:
        show: true
    # 配置数据源
    datasource:
      # 数据源别名
      names: db0, db1, db2, db3
      # db1数据源信息
      db0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_0?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_2?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db3:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_3?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
    sharding:
      tables:
        # 逻辑表名
        tbl_order:
          # 指定数据节点
          actual-data-nodes: db$->{0..3}.tbl_order_$->{0..3}
          # 分库策略
          database-strategy:
            standard:
              sharding-column: user_id
              precise-algorithm-class-name: com.example.shardingjdbc.sharding.algorithm.DBPreciseShardingAlgorithm
          # 分表策略
          table-strategy:
            complex:
              sharding-columns: order_id,user_id
              # 复合分表分片配置类
              algorithm-class-name: com.example.shardingjdbc.sharding.algorithm.MyComplexShardingAlgorithm
mybatis:
  mapper-locations: classpath:mapper/*.xml

策略类

public class MyComplexShardingAlgorithm implements ComplexKeysShardingAlgorithm {
    private final static String ORDER_ID = "order_id";
    private final static String USER_ID = "user_id";

    @Override
    public Collection doSharding(Collection availableTargetNames,
                                         ComplexKeysShardingValue shardingValue) {

        Collection orderIds = getShardingValue(shardingValue, ORDER_ID);
        Collection userIds = getShardingValue(shardingValue, USER_ID);

        Set tables = new HashSet<>();
        for (Long userId : userIds) {
            for (Long orderId : orderIds) {
                int index = getIndex(userId, orderId, availableTargetNames);
                for (String tableName: availableTargetNames) {
                    if (tableName.endsWith(String.valueOf(index))) {
                        tables.add(tableName);
                    }
                }
            }
        }

        return tables;
    }

    private Collection getShardingValue(ComplexKeysShardingValue shardingValue, String key) {
        Collection valueList = new ArrayList<>();
        Map> columnNameAndShardingValuesMap =
                shardingValue.getColumnNameAndShardingValuesMap();
        if (columnNameAndShardingValuesMap.containsKey(key)) {
            valueList.addAll(columnNameAndShardingValuesMap.get(key));
        }

        return valueList;
    }

    private int getIndex(Long userId, Long orderId, Collection availableTargetNames) {
        String userIdStr = String.valueOf(userId);
        String orderIdStr = String.valueOf(orderId);
        int sum = Integer.parseInt(userIdStr.substring(userIdStr.length() - 1)) + Integer.parseInt(orderIdStr.substring(orderIdStr.length() - 1));
        return sum % availableTargetNames.size();
    }
}

doSharding方法的入参

image.png

会有疑问doSharding方法中的入参shardingValue是如何获取数据的
这个涉及到源码,主要是mybatis组装sql后,shardingjdbc会解析sql,并结合分片建获取分片建的数据,具体可以看源码方法
org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter#route
方法中的
OptimizeResult optimizeResult = OptimizeEngineFactory.newInstance(shardingRule, sqlStatement, parameters, generatedKey.orNull()).optimize();

版权声明:
作者:主机优惠
链接:https://www.techfm.club/p/44771.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>