Java中使用Shardingsphere-JDBC进行分库分表的使用方法(精简)

摘要:
ServerTimezone=GMT%2B8用户名:root密码DruidDataSource驱动程序类名:com.mysql.cj.jdbc。驱动程序url:jdbc:mysql://localhost:3306/mydb?

1.pom.xml中添加依赖

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>

<!--相关基础依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>


2. application.yml中添加相关配

2.1 不分库分表的基础连接配置

 spring:
  shardingsphere:
   datasource:
    names: s1
    s1:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8
     username: root
     password: root
   props:
    sql:

     #是否打印sql
     show: true

*注意:这样配的话,跟操作单库单表的数据源配置效果一样


2.2 不分库但分表的连接配置

 spring:

  shardingsphere:
   datasource:
    names: s1
    s1:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8
     username: root
     password: root
   props:
    sql:
     show: true
   sharding:
    #分表
    tables:
     #参与分表的表名(虚拟表名)
     test:
      #分几个表,这里是两个,test_1和test_2
      actual-data-nodes: s1.test_$->{1..2}
      key-generator:
       #主键
       column: tid
       #主键填值,雪花
       type: SNOWFLAKE
      table-strategy:
       #主键分表路由策略
       inline:
        sharding-column: tid
        #这里采用偶数键值去test_1表,奇数键值去test_2表
        algorithm-expression: test_$->{tid%2+1}

2.3 分库分表的连接配置

 spring:

  shardingsphere:
   datasource:
    names: s1,s2
    s1:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/mydb1?serverTimezone=GMT%2B8
     username: root
     password: root

    s2:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3307/mydb2?serverTimezone=GMT%2B8
     username: root
     password: root
   props:
    sql:
     show: true
   sharding:
    #分表
    tables:
     #参与分表的表名(虚拟表名)
     test:
      #分库分表关键
      actual-data-nodes: s$->{1..2}.test_$->{1..2}
      key-generator:
       #主键
       column: tid
       #主键填值,雪花
       type: SNOWFLAKE
      table-strategy:
       #主键分表路由策略
       inline:
        sharding-column: tid
        #这里采用偶数键值去test_1表,奇数键值去test_2表
        algorithm-expression: test_$->{tid%2+1}

      data-strategy:
       #主键分库路由策略
       inline:
        sharding-column: tid
        #这里采用偶数键值去s1库,奇数键值去s2库
        algorithm-expression: s_$->{tid%2+1}

 

2.3 读写分离(mysql实现配置了主从架构)分库分表的连接配置

 spring:

  shardingsphere:
   datasource:
    names: m1,s1
    m1:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8
     username: root
     password: root

    s1:
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3307/mydb?serverTimezone=GMT%2B8
     username: root
     password: root
   props:
    sql:
     show: true
   sharding:
    #分表
    tables:
     #参与分表的表名(虚拟表名)
     test:
      #分库分表关键
      actual-data-nodes: ms0.test_$->{1..2}
      key-generator:
       #主键
       column: tid
       #主键填值,雪花
       type: SNOWFLAKE
      table-strategy:
       #主键分表路由策略
       inline:
        sharding-column: tid
        #这里采用偶数键值去test_1表,奇数键值去test_2表
        algorithm-expression: test_$->{tid%2+1}

    #主从规则

    master-slave-rules:

     #规则一,名字随意,我这里用ms0表示

     ms0:

      master-data-source-name: m0

      #这里可以看出从库可以配多个

      slave-data-source-names[0]: s0

      

 3.分库分表路由策略

 shardingsphere为我们提供了以下几种策略类型:

  inline(内联),standard(标准),complex(复杂),hint(强制)

 

3.1 inline

#分片策略(test是我们的逻辑表,这里对应实际表test_1和test_2)

spring.shardingsphere.sharding.tables.test.table-strategy.inline.sharding-column=tid
spring.shardingsphere.sharding.tables.test.table-strategy.inline.algorithm-expression=test_$->{tid%2+1}
#分库策略
spring.shardingsphere.sharding.tables.test.database-strategy.inline.sharding-column=tid
spring.shardingsphere.sharding.tables.test.database-strategy.inline.algorithm-expression=s$->{tid%2+1}

*注:只支持分片主键的精确路由,不支持范围查询、模糊查询、联表查询。

 

3.2 standard(需要实现PreciseShardingAlgorithm和RangeShardingAlgorithm接口)

#分片策略

spring.shardingsphere.sharding.tables.test.table-strategy.standard.sharding-column=tid
spring.shardingsphere.sharding.tables.test.table-strategy.standard.precise-algorithm-class-name=com.shardingDemo.algorithem.MyPreciseTableShardingAlgorithm
spring.shardingsphere.sharding.tables.test.table-strategy.standard.range-algorithm-class-name=com.shardingDemo.algorithem.MyRangeTableShardingAlgorithm
#分库策略

spring.shardingsphere.sharding.tables.test.database-strategy.standard.sharding-column=tid
spring.shardingsphere.sharding.tables.test.database-strategy.standard.precise-algorithm-class-name=com.shardingDemo.algorithem.MyPreciseDSShardingAlgorithm
spring.shardingsphere.sharding.tables.test.database-strategy.standard.range-algorithm-class-name=com.shardingDemo.algorithem.MyRangeDSShardingAlgorithm

*注:需要定制实现精确路由和范围路由的逻辑类,也就是自己编码路由逻辑。

 

3.3 complex(需要实现ComplexKeysShardingAlgorithm接口)

#分片策略(这里用了两个表的主键tid和uid)

spring.shardingsphere.sharding.tables.test.table-strategy.complex.sharding-columns= tid, uid
spring.shardingsphere.sharding.tables.test.table-strategy.complex.algorithm-class-name=com.shardingDemo.algorithem.MyComplexTableShardingAlgorithm

#分库策略(这里用了两个表的主键tid和uid)
spring.shardingsphere.sharding.tables.test.database-strategy.complex.sharding-columns=tid, uid
spring.shardingsphere.sharding.tables.test.database-strategy.complex.algorithm-class-name=com.shardingDemo.algorithem.MyComplexDSShardingAlgorithm

*注:需要定制实现多表关联路由逻辑类。

 

3.4 hint(需要实现HintShardingAlgorithm接口)

 spring.shardingsphere.sharding.tables.course.table-strategy.hint.algorithm-class-name=com.shardingDemo.algorithem.MyHintTableShardingAlgorithm

*注:需要定制实现指定路由逻辑类,在使用查询语句时加上此规则后,可以去指定库表查询数据。

用法举例:  

  HintManager hintManager = HintManager.getInstance();
  hintManager.addTableShardingValue("test", 2);  //去test_2表查数据
  List<Test> tests= testMapper.selectList(null);
  tests.forEach(test-> System.out.println(test));
  hintManager.close();

 

4. 关于广播表和绑定表

 

4.1 广播表

分库以后每个库都有的全数据表这种表的数据一般变动很小,例如:字典表和状态表,这些表的数据在每一个库里都应该是一致的。

spring.shardingsphere.sharding.broadcast-tables=t_dict
spring.shardingsphere.sharding.tables.t_dict.key-generator.column=dict_id
spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE

 

4.2 绑定表

当我们的查询条件依赖广播表时,也就是某逻辑表的字段的值依赖于一张广播表时,为避免联表查询产生笛卡尔积,则需要使用绑定规则。

spring.shardingsphere.sharding.binding-tables[0]=test,t_dict

 

5. 总结

使用分库分表的初衷是为了解决海量数据查询较慢的问题,列入当一张表的数据记录超过500万条或者单表容量大于2GB是则需要考虑分库分表,

但是分库分表会带来许多设计上和生产上的麻烦,增加逻辑复杂度和查询语句的使用限制,所以不要滥用分库分表,能不分就不分。另外分库分

表最好是在程序设计之初提前预估当前业务的数据增长量是否需要设计成分库分表架构,如果某个业务表比如订单表,他的增常量肯定每年都很

大,这个时候这张业务表就需要设计成分表,而用户表这种业务表起初可能增长势头很猛,但是经过两三年,数据基本上就稳定了,不会有较大

变动,那么,可以考虑设计此表时将表字段进行垂直切割,将常用字段放进用户表,而不常用字段放进用户关联表,但不做分表或分库。总之,

这是一个考验你业务和程序架构双项经验的事情。

免责声明:文章转载自《Java中使用Shardingsphere-JDBC进行分库分表的使用方法(精简)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kaptcha Java验证码网页上显示当前的时间和日期下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

windows命令绕过

windows命令绕过 forfies 使用方式如下: 实际使用: forfiles /c c:windowssystem32calc.exe 确认任务的父进程为forfiles.exe pcalua 实际使用: pcalua.exe -a c:windowssystem32calc.exe 主进程就是运行的进程 SyncAppvPu...

SQL Server 2008 删除大量数据

一、写在前面 - 想说爱你不容易   为了升级数据库至SQL Server 2008 R2,拿了一台现有的PC做测试,数据库从正式库Restore(3个数据库大小夸张地达到100G+),而机器内存只有可怜的4G,不仅要承担DB Server角色,同时也要作为Web Server,可想而知这台机器的命运是及其惨烈的,只要MS SQL Server一启动,内...

oracle 中的truncate 和delete

一、查询表大小,块多少语句 Select SEGMENT_Name,BYTES,BLOCKS,Extents From dba_segments Where segment_name In('BAI_NEW_SESSION_HISTORY2') order by segment_name; (2)查表空间 Select Tablespace_Name,...

sklearn中的数据集的划分

sklearn数据集划分方法有如下方法: KFold,GroupKFold,StratifiedKFold,LeaveOneGroupOut,LeavePGroupsOut,LeaveOneOut,LeavePOut,ShuffleSplit,GroupShuffleSplit,StratifiedShuffleSplit,PredefinedSplit,...

shell脚本编写

一、shell script概念 可以将shell终端解释器作为人与计算机硬件之间的“翻译官”,作为用户与Linux系统内部的通信媒介。 shell脚本命令的工作方式: 1.交互式(Interactive):用户每输入一条命令就立刻执行。 2.批处理(Batch):由用户事先编写好一个完整的shell脚本,脚本会一次性执行完所有的命令。 在shell sc...

shell用法总结

shell基础的一些东西   ●umask   --查看当前用户创建文件或文件夹时的默认权限  [test@szbirdora 1]$umask 0002 [test@szbirdora 1]$ls -lh -rw-rw-r-- test test myfile drwxrwxr-x test test 1 上面的例子中我们...