ShardingSphere-JDBC 分库分表

打印 上一主题 下一主题

主题 931|帖子 931|积分 2793

springBoot  引入maven
  1.             <dependency>
  2.                 <groupId>org.apache.shardingsphere</groupId>
  3.                 <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  4.                 <version>4.0.0-RC1</version>
  5.             </dependency>
复制代码
application.yml配置
  1. spring:
  2.   shardingsphere:
  3.     datasource:
  4.       names: db1,db2
  5.       db1:
  6.         url: jdbc:mysql://localhost:3306/db1?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
  7.         username: root
  8.         password: root
  9.         driver-class-name: com.mysql.cj.jdbc.Driver
  10.         type: com.alibaba.druid.pool.DruidDataSource
  11.       db2:
  12.         url: jdbc:mysql://localhost:3306/db2?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
  13.         username: root
  14.         password: root
  15.         driver-class-name: com.mysql.cj.jdbc.Driver
  16.         type: com.alibaba.druid.pool.DruidDataSource
  17. #      masterslave:
  18. #        # 读写分离配置
  19. #        load-balance-algorithm-type: round_robin #负载均衡策略,round_robin是轮循
  20. #        # 最终的数据源名称
  21. #        name: dataSource #就是bean的名字
  22. #        # 主库数据源名称
  23. #        master-data-source-name: db1
  24. #        # 从库数据源名称列表,多个逗号分隔
  25. #        slave-data-source-names: db2
  26.     sharding:
  27.       default-database-strategy:
  28.         inline:
  29.           sharding-column: order_id #数据库分片策略字段
  30.           algorithm-expression: db${order_id % 2+1} # 配置t_order表分表策略
  31.       tables:
  32.         t_order:
  33.           actual-data-nodes: db$->{1..2}.t_order_$->{0..3}  #配置t_order表分库策略(inline-基于行表达式的分片算法)
  34.           table-strategy:
  35.             inline:  #只适合一个字段
  36.               sharding-column: order_id   #分表策略字段
  37.               algorithm-expression: t_order_$->{order_id % 2}  #根据order_id%2 找到对应表名
  38.           key-generator:
  39.             column: order_id
  40.             type: SNOWFLAKE
  41.     props:
  42.       sql:
  43.         show: true #打印sql
复制代码
table-strategy:指定表的分片策略,table-strategy有以下几种策略
1 ) none 表示不分片,所有数据都存储在同一个表中。
2 ) standard 表示使用标准分片策略,根据分片键的值进行范围匹配,将数据路由到对应的分片表中。

        对应StandardShardingStrategy,标准分片策略,根据分片键的值进行范围匹配,将数据路由到对应的分片表中,提供对SQL语句中的=, >, =, , =, {1..2}.t_order_$->{0..3}  #配置t_order表分库策略(inline-基于行表达式的分片算法)
          table-strategy:
            standard:
              sharding-column: order_id
              precise-algorithm-class-name: com.zjf.web.config.standard.MyTablePreciseShardingAlgorithm[/code]分片类
  1.       tables:<br>        t_order:<br>          actual-data-nodes: db$->{1..2}.t_order_$->{0..3}  #配置t_order表分库策略(inline-基于行表达式的分片算法)<br>          table-strategy:<br>            standard:<br>              sharding-column: order_id<br>              precise-algorithm-class-name: com.zjf.web.config.standard.MyTablePreciseShardingAlgorithm
复制代码
3 ) inline 表示使用行表达式分片策略,根据分片键的值通过表达式计算得到分片结果,将数据路由到对应的分片表中。
           对应InlineShardingStrategy,使用Groovy的表达式,提供对SQL语句中的=和IN的分片操作支持,只支持单分片键。对于简单的分片算法,可以通过简单的配置使用
  1. @Component
  2. @Slf4j
  3. public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {
  4.     /**
  5.      * @param tableNames 对应分片库中所有分片表的集合
  6.      * @param shardingValue 为分片属性,logicTableName 为逻辑表,columnName 分片键,value 为从 SQL 中解析出来的分片键的值
  7.      * @return
  8.      */
  9.     @Override
  10.     public String doSharding(Collection<String> tableNames, PreciseShardingValue<Integer> shardingValue) {
  11.         for (String tableName : tableNames) {
  12.             // 取模算法,分片键 % 表数量
  13.             String value = String.valueOf(shardingValue.getValue() % tableNames.size() );
  14.             log.info("tableName {} value============ {}",tableName,value);
  15.             if (tableName.endsWith(value)) {
  16.                 return tableName;
  17.             }
  18.         }
  19.         throw new IllegalArgumentException("分片失败,tableNames:" + tableNames);
  20.     }
  21. }
复制代码
4 ) complex 表示使用复合分片策略,可以同时使用多个分片键对数据进行分片计算,将数据路由到对应的分片表中。

        对应ComplexShardingStrategy。复合分片策略。提供对SQL语句中的=, >, =,
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表