ShardingJDBC实现水平分库、水平分表

本文涉及的产品
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
简介: ShardingJDBC实现水平分库、水平分表
  • ShardingJdbc是一个轻量级的java框架,是增强版的JDBC
  • 在完成了分库分表之后,使用shardingJDBC进行数据读取
  • ShardingJDBC作用:简化在分库分表之后对数据库的操作
  • 水平分库/分表与垂直分库分表的区别


image.png

image.png


  • model

public class Course {
    // 课程主键
    private Long cid;
    private String cname;
     // 用户主键
    private long userId;
    private int cstatus;
}


# Target



# 水平分表


  • 表1: course_1
  • 表2: course_2
  • 数据入表规则:
  • cid为偶数则数据入表course_1
  • cid为奇数则数据入表course_2


# 水平分库


  • 库1: es_spark
  • 库2: sharding_sphere_2
  • 数据入库规则:
  • user_id为偶数的入库 es_spark
  • user_id为奇数的入库 sharding_sphere_2


# 依赖



  • springboot
  • mybatis-plus
  • sharding-jdbc

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>


# 数据源,分库分表规则配置



  • 可使用 ${ expression }$->{ expression }标识行表达式
  • ${begin..end} 表示范围区间
  • ${[unit1, unit2, unit_x]} 表示枚举值
  • ${['online', 'offline']}_table${1..3} 将取笛卡尔积
  • ShardingJDBC内置的主键生成器
  • SNOWFLAKE 雪花算法
  • 能够保证不同进程主键的不重复性,以及相同进程主键的有序性
  • UUID UUID.randomUUID()

# 数据源别名
spring.shardingsphere.datasource.names=m1,m2
# m1 数据源的具体配置
spring.shardingsphere.datasource.m1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://xxx/es_spark
spring.shardingsphere.datasource.m1.username=x
spring.shardingsphere.datasource.m1.password=x
# m2 数据源配置
spring.shardingsphere.datasource.m2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.jdbc-url=jdbc:mysql://xxx:3361/sharding_sphere_2
spring.shardingsphere.datasource.m2.username=x
spring.shardingsphere.datasource.m2.password=x
# 数据库的分布情况和表的分布情况 数据库.表,笛卡尔积
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2}
# 主键的生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# 指定数据库的分片策略  默认的 对所有的数据的规则
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user_id % 2+1}
# 对具体某张表的分库规则
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id % 2+1}
# 指定表的分片策略,如:cid是奇数存储到course_1,cid是偶数存储到course_2
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.props.sql.show=true
# 一个实体类对应两张表
#spring.main.allow-bean-definition-overriding=true

# 测试


/**
 * @author futao@mysteel.com
 * @date 2021/8/31
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseMapper courseMapper;
    @PostMapping("/add")
    public void add() {
        for (int i = 0; i < 100; i++) {
            Course course = new Course();
            course.setCname("语文");
            course.setCstatus(i);
            course.setUserId(i);
            courseMapper.insert(course);
        }
    }
    @GetMapping()
    public Course find(Long cid) {
        return courseMapper.selectOne(Wrappers.<Course>lambdaQuery().eq(Course::getCid, cid));
    }
}


  • user_id为偶数入库es_spark,cid为偶数入course_1


image.png

user_id为偶数入库es_spark,cid为奇数入course_2

image.png


user_id为奇数入库sharding_sphere_2,cid为偶数入course_1

image.png

user_id为奇数入库sharding_sphere_2,cid为奇数入course_2


image.png

# Q:



  • 分库分表之后如何分页查询
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
Java 关系型数据库 中间件
分库分表(3)——ShardingJDBC实践
分库分表(3)——ShardingJDBC实践
1542 0
分库分表(3)——ShardingJDBC实践
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
673 0
|
3月前
|
移动开发 JavaScript 前端开发
在项目中引入和使用 SCSS 文件
在项目中引入和使用 SCSS 文件
617 156
|
存储 SQL 关系型数据库
数据库魔法师:使用ShardingSphere实现MySQL读写分离与分片指南跟着爆叔的节奏稳了!
数据库魔法师:使用ShardingSphere实现MySQL读写分离与分片指南跟着爆叔的节奏稳了!
564 0
|
1月前
|
SQL Java 数据库连接
SpringBoot整合MyBatis-Flex保姆级教程,看完就能上手!
MyBatis-Flex 作为一个现代化的 MyBatis 增强框架,在保持 MyBatis 灵活性的同时,提供了更多便捷的功能,特别适合需要复杂查询和高性能要求的项目。
326 0
|
8月前
|
关系型数据库 MySQL Java
SpringBoot集成Sharding-Jdbc分库分表
ShardingSphere是一套开源分布式数据库中间件解决方案,包含Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar三款产品,提供数据分片、分布式事务和数据库治理功能,适用于多样化应用场景。
1185 0
SpringBoot集成Sharding-Jdbc分库分表
|
监控 Java 数据库连接
Spring Boot中的健康检查和监控
Spring Boot中的健康检查和监控
1144 1
|
SQL 负载均衡 算法
使用ShardingJDBC实现分库分表
使用ShardingJDBC实现分库分表
|
自然语言处理 前端开发
收藏级5款免费wordpress主题分享
本文分享了5款值得收藏的免费WordPress主题:1. OceanWP,现代多功能主题,适合电商;2. Divi,高级主题带页面构建器,功能丰富;3. Breakthrough Pro,基于Genesis框架的企业主题;4. Neve,时尚主题支持多语言;5. Ultra,商业主题搭配Themify构建器。每款主题均附有预览图,方便选择。更多主题可访问http://ztmao.com。
572 1
|
安全 Java 数据库连接
Java使用MyBatis-Plus的OR
通过MyBatis-Plus的条件构造器,Java开发者可以方便地进行复杂的查询条件组合,包括AND和OR条件的灵活使用。熟练掌握这些技巧,可以显著提升开发效率和代码可读性。
849 20

热门文章

最新文章