「ShardingSphere」水平分表实战

目录

pom配置

properties配置

实体类

数据库表结构

按照年份进行分表配置

按照ID进行分表配置


  • pom配置

shardingsphere-jdbc-core-spring-boot-starter版本

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent> 
     <!--分库分表-->
    <dependencies>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
            <version>5.1.2</version>
        </dependency>
    </dependencies>       
  • properties配置

# 数据库
spring.shardingsphere.datasource.names=ds0
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://ip:3306/shequ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
# 分布式序列算法类型
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.type=INLINE
# 分片算法属性配置,使用properties格式会爆红,但是不影响使用
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{year}
# 逻辑表名称
spring.shardingsphere.rules.sharding.tables.t_user.logic-table=t_user
# 主键
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.column=id
# 主键生成策略UUID、SNOWFLAKE、LEAF、CUSTOMIZED
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.key-generator-name=SNOWFLAKE
# 物理表
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=ds0.t_user_$->{['2021','2022','2023']}
# 基于哪个属性进行分片
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=year
# 分片算法名称
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-algorithm-name=table-inline-user

ShardingSphere 的 table-strategy 用于指定表的分片策略,其包括了以下几种类型:

1. standard

标准分片策略,表示使用标准的分片算法进行分片,通常需要指定分片列和具体的分片算法。

2. complex

复合分片策略,表示将多个分片键组合成复合分片键进行分片。例如,可以将 user_id 和 order_id 两个字段组合成复合分片键,然后使用具体的分片算法将数据分散到不同的数据节点中。

3. hint

基于 Hint 的分片策略,表示在 SQL 中手动指定分片节点信息。当程序无法根据分片键自动路由到正确的数据节点时,可以使用该分片策略手动指定分片节点信息。

4. inline

内联分片策略,表示使用类似于 Groovy 脚本的方式来计算分片,通常使用场景是一些特殊的分片需求。

5. none

不适用分片策略,表示直接将数据路由到默认的数据节点中,适用于不需要分片的表。

需要注意的是,在实际使用时,我们还需要根据具体的业务需求来选择具体的分片算法、分片键等。同时,由于不同的数据库产品间的语法支持差异,可能需要对配置进行微调和调整。

  • 实体类

@Data
@TableName("t_user")
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    private Integer year;
    private SexEnum sex;
}
  • 数据库表结构

create table t_user_2021
(
    id    bigint(64)  not null
        primary key,
    name  varchar(20) not null,
    age   int         null,
    email varchar(20) null,
    year  int         null comment '年份',
    sex   int         null comment '性别'
)charset = utf8;

create table t_user_2022
(
    id    bigint(64)  not null
        primary key,
    name  varchar(20) not null,
    age   int         null,
    email varchar(20) null,
    year  int         null comment '年份',
    sex   int         null comment '性别'
)charset = utf8;

create table t_user_2023
(
    id    bigint(64)  not null
        primary key,
    name  varchar(20) not null,
    age   int         null,
    email varchar(20) null,
    year  int         null comment '年份',
    sex   int         null comment '性别'
) charset = utf8;



  1. 按照年份进行分表配置

# 基于哪个属性进行分片
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=year
# 分片算法属性配置,使用properties格式会爆红,但是不影响使用
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{year}
  1. 按照ID进行分表配置

# 基于哪个属性进行分片
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=id
# 分片算法属性配置,使用properties格式会爆红,但是不影响使用
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{id%2}

此时的数据库表结构应该为:t_user_0、t_user_1

create table t_user_0
(
 ...
)charset = utf8;

create table t_user_1
(
 ...
)charset = utf8;

测试:直接基于Mybatis-plus进行增删改查即可 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术路上的探险家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值