当前位置: 首页 > news >正文

基于smilehappiness-framework-base,快速集成ShardingSphere JDBC

文章目录

  • 1 前言
  • 2 分库分表
    • 2.1 什么是分库分表
    • 2.2 垂直分库
    • 2.3 水平分表
  • 3 如何集成使用分库分表
    • 3.1 添加maven依赖
    • 3.2 添加 shardingSphere 数据源
    • 3.2 添加 sharding jdbc 配置
      • 3.2.1 分表配置示例
      • 3.2.2 分库且分表配置示例

1 前言

为什么使用分库分表?随着业务量的增加,单表的数据量非常庞大,查询性能会变得非常差,速度非常慢,分库分表可以很友好的解决这个问题。

数据库中的数据量不一定是可控的,在未进行分库分表的情况下,随着时间和业务的发展,库中的表会越来越多,表中的数据量也会越来越大,相应地,数据操作,增删改查的开销也会越来越大;另外,由于无法进行分布式式部署,而一台服务器的资源(CPU、磁盘、内存、IO等)是有限的,最终数据库所能承载的数据量、数据处理能力都将遭遇瓶颈。

2 分库分表

分库分表的目的是为了解决单表数据量过大的问题,将数据分散到不同的库的不同的表中,从而提高查询效率。当然啦,分库还可以很好的配合读写分离使用,进一步提高系统的性能。

2.1 什么是分库分表

分库分表,就是把原本存储于一个库的数据分块存储到多个库上,把原本存储于一个表的数据分块存储到多个表上。根据目前社区活跃度,本项目采用
shardingsphere-jdbc 进行分库分表,可以很好的兼容SpringBoot。

使用的组件版本: shardingsphere-jdbc使用的是5.2.1版本,动态数据源使用的是4.2.0版本

  • ShardingSphere
    JDBC(前身称为Sharding-JDBC)并不是包含多个独立组件,而是一个统一的数据库访问层解决方案的组成部分。它是一个轻量级的Java框架,通过提供一套完整的JDBC驱动的方式来透明化分库分表操作,使得用户能够像操作单个数据库一样操作分布式的数据库集群。
  • Sharding-JDBC:最初是指的一个专注于在Java应用中进行数据库分片(sharding)的轻量级框架,通过JDBC驱动扩展的方式实现在应用端的数据库水平扩展能力。

ShardingSphere JDBC:随着项目发展,该项目被纳入到了更广泛的Apache ShardingSphere项目之下,并正式更名为“ShardingSphere
JDBC”,其不仅保留了原有的数据库分片功能,还可能增加了更多如分布式事务、数据治理等企业级特性,成为了Apache
ShardingSphere项目中针对Java应用环境下的一个模块。
所以,现在大家所说的“ShardingSphere JDBC”就是以前“Sharding-JDBC”的延续和发展,具备更强大的功能和更完善的设计。

2.2 垂直分库

垂直分库是指将表中的数据按照一定的规则,将数据分散到不同的库中,通常是按照业务功能划分到不同的数据库中,比如,将用户数据、订单数据、商品数据分别存储到不同的数据库中。

2.3 水平分表

水平分表是指将表中的数据按照一定的规则,将数据分散到表结构相同的,数据量相似的不同的表中。

3 如何集成使用分库分表

3.1 添加maven依赖

在需要使用分库分表的项目中,添加如下依赖:


<parent><groupId>cn.smilehappiness</groupId><artifactId>smilehappiness-framework-base</artifactId><version>3.0.3-RELEASE</version>
</parent>

<dependency><groupId>cn.smilehappiness</groupId><artifactId>smilehappiness-shardingjdbc</artifactId>
</dependency>

3.2 添加 shardingSphere 数据源

为需要使用分库分表的mapper类打上 @DS(“shardingSphere”) 注解添加指定数据源,示例如下:


@DS("shardingSphere")
@Repository
public interface TShardingTestModeMapper extends BaseMapper<TShardingTestMod> {
}

3.2 添加 sharding jdbc 配置

注:查询分表的数据时,需要包含分表的分片键,比如使用biz_id分表,查询数据时,where条件中,需要包含biz_id这个字段,否则将会查询所有的表数据。

3.2.1 分表配置示例

spring:shardingsphere:mode:type: Standaloneprops:sql-show: truedatasource:common:driver-class-name: com.mysql.cj.jdbc.Drivernames: creditcredit:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://ip:port/xxx?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: adminpassword: 666rules:sharding:tables:bpm_process_data_record:actual-data-nodes: credit.bpm_process_data_record_$->{0..19}# 分库策略databaseStrategy:none:# 分表策略table-strategy:standard:sharding-column: biz_idsharding-algorithm-name: bpm-process-data-record-inlinebpm_process_record:actual-data-nodes: credit.bpm_process_record_$->{0..9}table-strategy:standard:sharding-column: biz_idsharding-algorithm-name: bpm-process-record-inlinesharding-algorithms:bpm-process-data-record-inline:type: INLINEprops:algorithm-expression: bpm_process_data_record_$->{Long.parseLong(biz_id) % 20}bpm-process-record-inline:type: INLINEprops:algorithm-expression: bpm_process_record_$->{Long.parseLong(biz_id) % 10}

3.2.2 分库且分表配置示例

spring:shardingsphere:mode:type: Standaloneprops:sql-show: truedatasource:common:driver-class-name: com.mysql.cj.jdbc.Drivernames: db0,db1,db2db0:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db0?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: javapassword: AtOd5SYDRapIOU2O!NyjL!cMK90db1:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db1?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: javapassword: AtOd5SYDRapIOU2O!NyjL!cMK90db2:type: com.zaxxer.hikari.HikariDataSourcejdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db2?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: javapassword: AtOd5SYDRapIOU2O!NyjL!cMK90rules:sharding:tables:t_sharding_test_mod:actual-data-nodes: db$->{0..1}.t_sharding_test_mod_$->{0..2}database-strategy:standard:sharding-column: idsharding-algorithm-name: database-inlinetable-strategy:standard:sharding-column: user_idsharding-algorithm-name: table-inlinet_sharding_time_range:#如果多种时间区间组合,可以使用如下配置方式#actual-data-nodes: db2.t_sharding_time_range_$->{2023}_$->{12},db2.t_sharding_time_range_$->{2024..2025}_$->{(1..12).collect{t ->t.toString().padLeft(2,'0')}}actual-data-nodes: db2.t_sharding_time_range_$->{2024..2025}_$->{(1..12).collect{t ->t.toString().padLeft(2,'0')}}table-strategy:standard:sharding-column: created_timesharding-algorithm-name: time-inlinesharding-algorithms:database-inline:type: INLINEprops:algorithm-expression: db$->{id % 2}table-inline:type: INLINEprops:algorithm-expression: t_sharding_test_mod_$->{user_id % 3}time-inline:type: INTERVALprops:datetime-pattern: "yyyy-MM-dd HH:mm:ss"sharding-suffix-pattern: "yyyy_MM"datetime-lower: "2024-01-01 00:00:00"datetime-upper: "2099-12-30 00:00:00"datetime-interval-amount: 1datetime-interval-unit: MONTHS

相关文章:

  • 请求包的大小会影响Redis每秒处理请求数量
  • linux部署nginx
  • 【JavaEE】_tomcat的安装与使用
  • Android Gradle 开发与应用 (一) : Gradle基础
  • spring Boot快速入门
  • Java MP3转PCM
  • 一文读懂什么是HTTPS检查
  • linux centos7.9改dns和ip
  • 【rust】vscode下rust-analyzer和Rust Test Lens的Lens
  • windows11本地深度学习环境搭建Anacond,keras,tensorflow,pytorch, jupyter notebook
  • apachectl: line 79: 20233 Segmentation fault (core dumped) $HTTPD “$@“
  • 【卡码网】完全背包问题 52. 携带研究材料——代码随想录算法训练营Day44
  • 10 Redis之SB整合Redis+ 高并发问题 + 分布式锁
  • 探索无限:Sora与AI视频模型的技术革命 - 开创未来视觉艺术的新篇章
  • PHP中的飞碟运算符、取反运算符、对比非ASCII字符串、对比浮点数操作
  • Google 是如何开发 Web 框架的
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • docker-consul
  • golang中接口赋值与方法集
  • Java 多线程编程之:notify 和 wait 用法
  • Java基本数据类型之Number
  • node和express搭建代理服务器(源码)
  • Otto开发初探——微服务依赖管理新利器
  • Redis字符串类型内部编码剖析
  • Vue 2.3、2.4 知识点小结
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 关于字符编码你应该知道的事情
  • 汉诺塔算法
  • 容器服务kubernetes弹性伸缩高级用法
  • 小李飞刀:SQL题目刷起来!
  • 新手搭建网站的主要流程
  • FaaS 的简单实践
  • 关于Android全面屏虚拟导航栏的适配总结
  • # .NET Framework中使用命名管道进行进程间通信
  • # Java NIO(一)FileChannel
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • (13):Silverlight 2 数据与通信之WebRequest
  • (C语言)fgets与fputs函数详解
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (四)Controller接口控制器详解(三)
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转)平衡树
  • (转)详解PHP处理密码的几种方式
  • .cfg\.dat\.mak(持续补充)
  • .Net Redis的秒杀Dome和异步执行
  • .NET 使用配置文件
  • .net 托管代码与非托管代码
  • .NET 中的轻量级线程安全
  • /usr/bin/env: node: No such file or directory
  • @data注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504