• sharding-jdbc精确分片配置


    sharding:
      jdbc:
        config:
          sharding:
            tables:
              myorder:
                key-generator-column-name: id  #主键
                actual-data-nodes: db$->{0..1}.myorder_$->{0..1}
                #分库策略
                database‐strategy:
                  standard:
                    sharding-column: id
                    #精确分库分片配置类
                    precise-algorithm-class-name: org.test.sharding.ShardingConfig
                #分表策略
                table-strategy:
                  standard:
                    sharding-column: id
                    #精确分表分片配置类
                    precise-algorithm-class-name: org.test.sharding.ShardingTableConfig
              orderitem:  #test
                key-generator-column-name: id  #主键
                actual-data-nodes: db$->{0..1}.orderitem_$->{0..2}    #数据节点,均匀分布
                #分库策略
                database‐strategy:
                  inline:
                    sharding-column: id
                    algorithm-expression: db$->{id%2}
                table-strategy:
                  inline:
                    sharding-column: id
                    algorithm-expression: orderitem_$->{id%2+1}
            #当两个分表的表进行关联查询的时候一定要将这两个表设置绑定表,否则会出现笛卡尔积
            binding-tables[0]: orderitem,myorder
          props:
            sql:
              show: true
    
        datasource:
          names: db0,db1
          db0:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost:3306/demo
            username: root
            password: 123456
          db1:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost:3306/demo1
            username: root
            password: 123456

    精确分库策略配置类

    /**
     * @author huQi
     * @email
     * @data 2021/1/20 13:04
     */
    
    import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
    import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
    import org.springframework.stereotype.Component;
    import java.util.*;
    @Component
    public class ShardingConfig implements PreciseShardingAlgorithm<Long> {
    
        /**
         *  在开发的环境中一定不要用随机数来决定是在哪个库,会出现无法准确的定位库的情况
         * @param collection  库名集合
         * @param preciseShardingValue 分片列
         * @return
         */
        
        @Override
        public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
            // 分片字段值
            Long value = preciseShardingValue.getValue();
            // 现在算法是:%2 求余如果是0则ds0.xmjbq_user,如果是1则ds0.xmjbq_user。但是由于id是字符串而且是很长的,所以截取最后一位然后转为Integer类型再求余
    
            Random random = new Random();
            int pick = random.nextInt(1);
            for (String s : collection) {
                if(s.contains(String.valueOf(pick))){
                    return s;
                }
            }
            throw new UnsupportedOperationException();
        }
    
    }

    精确分表配置类

    import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
    import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
    import org.springframework.stereotype.Component;
    
    import java.util.Collection;
    import java.util.Random;
    
    /**
     * @author huQi
     * @email
     * @data 2021/1/20 14:19
     */
    @Component
    public class ShardingTableConfig  implements PreciseShardingAlgorithm<Long> {
        /**
         * 在开发的环境中一定不要用随机数来决定是在哪个表,会出现无法准确的定位表的情况
         *
         * */
        @Override
        public String doSharding(Collection<String> tables, PreciseShardingValue<Long> preciseShardingValue) {
            // 分片字段值
            Long value = preciseShardingValue.getValue();
            // 现在算法是:%2 求余如果是0则ds0.xmjbq_user,如果是1则ds0.xmjbq_user。但是由于id是字符串而且是很长的,所以截取最后一位然后转为Integer类型再求余
    
            Random random = new Random();
            int pick = 0;
            if(tables.size()>1){
                pick = random.nextInt(3);
            }
    
            for (String s : tables) {
                System.out.println("sharding:"+s+"随机数"+pick);
                    if(s.contains(String.valueOf(pick))){
                        return s;
                    }
            }
    
            throw new UnsupportedOperationException();
        }
    }

    数据库

    demo:myorder_0,myorder_1,orderitem_0,orderitem_1,orderitem_2

    demo1:myorder_0,myorder_1,orderitem_0,orderitem_1,orderitem_2

  • 相关阅读:
    将字符串格式化为csv数据
    以RTMP的方式播放录制好的视频
    as3signals的使用介绍
    rootkit hook之[四] IDT Hook
    .net调试WEB程序时页面无法显示的原因分析及解决办法
    rootkit hook之[六] sysenter Hook
    CUDA初探:GPU的并行计算
    rootkit hook 之[五] IRP Hook全家福
    rootkit之[七]IAT Hook HybridHook之终极打造
    CUDA初探:CUDA的初始化
  • 原文地址:https://www.cnblogs.com/huqi96/p/14426728.html
Copyright © 2020-2023  润新知