暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

自定义水平分库分表的策略【精准分片】

680

背景:

    在某些情景下我们使用sharding jdbc所提供的分库分表算法无法满足我们的业务需要,需要我们自定义分库分表算法的时候使用。精准分片的要求是只能使用单个分片键。


1、编写自定义分库类,实现PreciseShardingAlgorithm<T>,范型T是分片键的数据类型

    public class CustomDBPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {


    /**
    * @param dataSourceNames 数据源集合
    * 在分库时值为所有分片库的集合 databaseNames
    * 分表时为对应分片库中所有分片表的集合 tablesNames

    * @param preciseShardingValue 分片属性,包括
    * logicTableName 为逻辑表,
    * columnName 分片健(字段),
    * value 为从 SQL 中解析出的分片健的值
    * @return
    */
    @Override
    public String doSharding(Collection<String> dataSourceNames, PreciseShardingValue<Long> preciseShardingValue) {


    for(String datasourceName : dataSourceNames){


    String value = preciseShardingValue.getValue() % dataSourceNames.size() + "";
    //ds0、ds1
    if(datasourceName.endsWith(value)){
    return datasourceName;
    }
    }
    return null;
    }
    }

    2、编写自定义分表类,实现PreciseShardingAlgorithm<>

      public class CustomTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {


      /**
      *
      * @param dataSourceNames 数据源集合
      * 在分库时值为所有分片库的集合 databaseNames
      * 在分表时值为所有分片表的集合 tablesNames
      *
      * @param preciseShardingValue 分片属性,包括
      * logicTableName 为逻辑表,
      * columnName 分片健(字段),
      * value 为从 SQL 中解析出的分片健的值
      * @return
      */
      @Override
      public String doSharding(Collection<String> dataSourceNames, PreciseShardingValue<Long> preciseShardingValue) {


      for(String datasourceName : dataSourceNames){


      String value = preciseShardingValue.getValue() % dataSourceNames.size() + "";
      //product_order_0
      if(datasourceName.endsWith(value)){
      return datasourceName;
      }
      }
      return null;
      }
      }


      3、修改配置文件application.properties

      核心:在精准分库策略时指定的是分库全类名,在精准分表策略时指定的是分表全类名

        spring.application.name=xdclass-sharding-jdbc
        server.port=8080




        # 打印执行的数据库以及语句
        spring.shardingsphere.props.sql.show=true


        # 数据源 db0
        spring.shardingsphere.datasource.names=ds0,ds1


        # 第一个数据库
        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/xdclass_shop_order_0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
        spring.shardingsphere.datasource.ds0.username=root
        spring.shardingsphere.datasource.ds0.password=liangxx12xyyy




        # 第二个数据库
        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/xdclass_shop_order_1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
        spring.shardingsphere.datasource.ds1.username=root
        spring.shardingsphere.datasource.ds1.password=liangxx12xyyy




        #配置workId
        spring.shardingsphere.sharding.tables.product_order.key-generator.props.worker.id=1


        #id生成策略
        spring.shardingsphere.sharding.tables.product_order.key-generator.column=id
        spring.shardingsphere.sharding.tables.product_order.key-generator.type=SNOWFLAKE


        #精准分片-水平分表
        # 指定product_order表的数据分布情况,配置数据节点,在 Spring 环境中建议使用 $->{...}
        spring.shardingsphere.sharding.tables.product_order.actual-data-nodes=ds$->{0..1}.product_order_$->{0..1}


        #指定精准分片算法(水平分库) 根据user_id分库
        spring.shardingsphere.sharding.tables.product_order.database-strategy.standard.sharding-column=user_id
        spring.shardingsphere.sharding.tables.product_order.database-strategy.standard.precise-algorithm-class-name=net.xdclass.strategy.CustomDBPreciseShardingAlgorithm


        #指定精准分片算法(水平分表) 根据订单id分表
        spring.shardingsphere.sharding.tables.product_order.table-strategy.standard.sharding-column=id
        spring.shardingsphere.sharding.tables.product_order.table-strategy.standard.precise-algorithm-class-name=net.xdclass.strategy.CustomTablePreciseShardingAlgorithm


        分库算法的调试


        分表算法的调试



        关于leetcode算法训练营:

            加我微信号私聊参加训练营,尤其是想进入大厂工作的同学,算法是绕不过去的坎,我自己花了三年时间刷算法,总结思路,刷各种数据结构课程,加入我的训练营,我手把手以在线直播课的形式带你理思路,手把手带你写代码,让你真正体会算法之美~,同时遇到不明白的地方可以直接课上和我沟通,彻底解决你的代码困难症~

        本人用c++刷了800道左右的算法,java语言刷了600道左右的算法题,并对这些题做了详细的个人总结。本科期间系统学习了数据结构与算法课程,同时考研过程中写完了率辉主编的《2020年数据结构高分笔记》和《数据结构1000题》,看完的视频包括《mooc浙大数据结构国家精品课程》和《王道考研408数据结构课程》,《王道2019年算法题讲解视频》,最终以初试专业第三名进入了北理工软件工程专业。熟悉并掌握常见的数据结构,比如链表、数组、树、图、队列、堆栈等等,精通数据结构教材中的所有算法,比如常见的遍历算法、动态规划,递归,回溯,剪枝,并查集,最短路径,拓扑排序等,所以快加入训练营吧,我们一起进步

        奔跑的小梁,公众号:梁霖编程工具库我决定了,算法文档开源!!


        文章转载自梁霖编程工具库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

        评论