Sharding-JDBC分表实践

一 Sharding-JDBC分表实践

1.0 背景

这周问了小组长有任务吗?组长说暂时没有任务了,让我了解下分表技术。原因是因为随着公司发展,用户量大量增加,业务也越来越繁杂。一张表的字段有几十个上百个,一张表存储的数据也很多。这样数据库的压力就比较大了。

之后我便打开了我的B站,开始了解一波。

2.0 垂直分表、垂直分库

(表和库都是不一样的)

2.1 垂直分表

  • 一般将不常用的、数据较大、长度较长的拆分到"扩展表"。

2.2 垂直分库

  • 一个数据库的表太多,按照一定业务逻辑进行垂直切,比如用户相关的表放在一个数据库里,订单相关的表放在一个数据库里。

3.0 水平分表、水平分库

(库和表都是一样的)

3.1 水平分表

  • 单表的数据量太大,按照某种规则,切分到多张表里面去(还是同一个库中)。

 

3.2 水平分库

  • 将单张表的数据切分到多个服务器上去,每个服务器具有相应的库与表,知识表中数据集合不同。

 

二 Sharding-JDBC

1.0 简介

  • 是轻量级的Java框架,是增强版的JDBC驱动

  • 简化对分库分表之后的数据相关操作

2.0 pom

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

2.1 properties

# 水平分库,配置两个数据源m1
spring.shardingsphere.datasource.names=m1

#一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

#配置数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=cf2016cgb580


#指定数据库分布情况
#spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2}
#代表m1库,表course1,表course2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course$->{1..2}  

#指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

#分片策两种实现方式
#方式一 :约定cid值偶数添加到course_1表,如果cid是奇数添加到course_2表
#table-strategy.inline.sharding-column:分片字段配置 table-strategy.inline.algorithm-expression:分片算法表达式
#spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
#spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}

#方式二:自定义
spring.shardingsphere.sharding.tables.course.table-strategy.standard.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.standard.precise-algorithm-class-name=com.atguigu.shardingjdbcdemo.algorithm.MyPreciseShardingAlgorithm


#打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.2 自定义分片算法

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() % 2+1+"")) {
                return tableName;
            }
        }
        throw new IllegalArgumentException();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值