当前位置:Gxlcms > 数据库问题 > Sharding-JDBC实现水平拆分-单库分表

Sharding-JDBC实现水平拆分-单库分表

时间:2021-07-01 10:21:17 帮助过:86人阅读

-bean-definition-overriding: true shardingsphere: datasource: names: db1 # 主数据源 db1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_user?characterEncoding=utf-8 username: **** password: **** sharding: tables: user: # 分表配置 actual-data-nodes.db1: user_${0..3} # inline 表达式 table-strategy.inline.sharding-column: id table-strategy.inline.algorithm-expression: user_${id.longValue()%4} props: # 开启SQL显示,默认false sql: show: true
  • actual-data-nodes 配置分表信息,这边用的inline表达式,翻译过来就是db1.user0,db1.user1,db1.user2,db1.user3
  • inline.sharding-column 分表的字段,这边用id分表
  • inline.algorithm-expression 分表算法行表达式,需符合groovy语法,上面的配置就是用id进行取模分片

 

  如果我们有更复杂的分片需求,可以自定义分片算法来实现:

sharding:
      tables:
        user:
          # 分表字段
          table-strategy.standard.sharding-column: id
          # 自定义分表算法类
          table-strategy.standard.precise-algorithm-class-name: com.*.*.MyPreciseShardingAlgorithm

  算法类:

public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        for (String tableName : availableTargetNames) {
            if (tableName.endsWith(shardingValue.getValue() % 4 + "")) {
                return tableName;
            }
        }
        throw new IllegalArgumentException();
    }

}

  在doSharding方法中你可以根据参数shardingValue做一些处理,最终返回这条数据需要分片的表名称即可。

  除了单列字段分片,还支持多字段分片,大家可以自己去看文档操作一下。

  需要分表的进行配置,不需要分表的无需配置,数据库操作代码一行都不用改变。

  如果我们要在单库分表的基础上,再做读写分离,同样很简单,只要多配置一个从数据源就可以了,配置如下:

spring.shardingsphere.datasource.names=master,slave

# 主数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456

# 从数据源
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456

# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue()%4}

# 读写分离配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave

Sharding-JDBC实现水平拆分-单库分表

标签:实现   mys   支持   lib   bean   表达式   rri   available   核心   

人气教程排行