分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表
ShardingSphere实现分库分表
有关分库分表前面写了四篇博客:
2、分库分表(2) --- ShardingSphere(理论)
3、分库分表(3) ---SpringBoot + ShardingSphere实现读写分离
4、分库分表(4) ---SpringBoot + ShardingSphere 实现分表
这篇博客通过ShardingSphere实现分库分表
,并在文章最下方附上项目Github地址
。
一、项目概述
1、技术架构
项目总体技术选型
SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4 + MySQL + lombok(插件)
2、项目说明
场景
在实际开发中,如果表的数据过大我们需要把一张表拆分成多张表,也可以垂直切分把一个库拆分成多个库,这里就是通过ShardingSphere实现分库分表
功能。
3、数据库设计
分库
ds一个库分为 ds0库 和 ds1库。
分表
tab_user一张表分为tab_user0表 和 tab_user1表。
如图
ds0库
ds1库
具体的创建表SQL也会放到GitHub项目里
二、核心代码
说明
完整的代码会放到GitHub上,这里只放一些核心代码。
1、application.properties
server.port=8084
#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
#打印sql
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
#根据年龄分库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{age % 2}
#根据id分表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=ds$->{0..1}.tab_user$->{0..1}
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 2}
Sharding-JDBC可以通过Java
,YAML
,Spring命名空间
和Spring Boot Starter
四种方式配置,开发者可根据场景选择适合的配置方式。具体可以看官网。
2、UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 模拟插入数据
*/
List<User> userList = Lists.newArrayList();
/**
* 初始化插入数据
*/
@PostConstruct
private void getData() {
userList.add(new User(1L,"小小", "女", 3));
userList.add(new User(2L,"爸爸", "男", 30));
userList.add(new User(3L,"妈妈", "女", 28));
userList.add(new User(4L,"爷爷", "男", 64));
userList.add(new User(5L,"奶奶", "女", 62));
}
/**
* @Description: 批量保存用户
*/
@PostMapping("save-user")
public Object saveUser() {
return userService.insertForeach(userList);
}
/**
* @Description: 获取用户列表
*/
@GetMapping("list-user")
public Object listUser() {
return userService.list();
}
三、测试验证
1、批量插入数据
请求接口
localhost:8084/save-user
我们可以从商品接口代码中可以看出,它会批量插入5条数据。我们先看控制台输出SQL语句
我们可以从SQL语句可以看出 ds0 和 ds1 库中都插入了数据。
我们再来看数据库
ds0.tab_user0
ds0.tab_user1
ds1.tab_user0
ds1.tab_user1
完成分库分表插入数据。
2、获取数据
这里获取列表接口的SQL,这里对SQL做了order排序操作,具体ShardingSphere分表实现order操作的原理可以看上面一篇博客。
select * from tab_user order by age <!--根据年龄排序-->
请求接口结果
我们可以看出虽然已经分库分表,但依然可以将多表数据聚合在一起并可以支持按age排序。
注意
ShardingSphere并不支持CASE WHEN
、HAVING
、UNION (ALL)
,有限支持子查询
。这个官网有详细说明。
Github地址
:https://github.com/yudiandemingzi/spring-boot-sharding-sphere
参考
我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(20)
分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表的更多相关文章
- 分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离
分库分表(7)--- ShardingSphere实现分库分表+读写分离 有关分库分表前面写了六篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理 ...
- 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离
分库分表(6)--- ShardingSphere实现分表+ 读写分离 有关分库分表前面写了五篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论 ...
- 分库分表(4) ---SpringBoot + ShardingSphere 实现分表
分库分表(4)--- ShardingSphere实现分表 有关分库分表前面写了三篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3.分库 ...
- 分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离
分库分表(3)---ShardingSphere实现读写分离 有关ShardingSphere概念前面写了两篇博客: 1.分库分表(1) --- 理论 2. 分库分表(2) --- ShardingS ...
- SpringBoot使用Sharding-JDBC分库分表
本文介绍SpringBoot使用当当Sharding-JDBC进行分库分表. 1.有关Sharding-JDBC 有关Sharding-JDBC介绍这里就不在多说,之前Sharding-JDBC是当当 ...
- Springboot2.x + ShardingSphere 实现分库分表
之前一篇文章中我们讲了基于Mysql8的读写分离(文末有链接),这次来说说分库分表的实现过程. 概念解析 垂直分片 按照业务拆分的方式称为垂直分片,又称为纵向拆分,它的核心理念是专库专用. 在拆分之前 ...
- springboot+mybatisplus+sharding-jdbc分库分表实例
项目实践 现在Java项目使用mybatis多一些,所以我也做了一个springboot+mybatisplus+sharding-jdbc分库分表项目例子分享给大家. 要是用的springboot+ ...
- 在多数据源中对部分数据表使用shardingsphere进行分库分表
背景 近期在项目中需要使用多数据源,其中有一些表的数据量比较大,需要对其进行分库分表:而其他数据表数据量比较正常,单表就可以. 项目中可能使用其他组的数据源数据,因此需要多数据源支持. 经过调研多数据 ...
- ShardingJdbc-分表;分库;分库分表;读写分离;一主多从+分表;一主多从+分库分表;公共表;数据脱敏;分布式事务
目录 创建项目 分表 导包 表结构 Yml 分库 Yml Java 分库分表 数据库 Yml 读写分离 数据库 Yml 其他 只请求主库 读写分离判断逻辑代码 一主多从+分表 Yml 一主多从+分库分 ...
随机推荐
- 使用Spring Boot和RxJava的构建响应式REST API
我不打算解释什么是响应式编程,也不解释为什么要使用它.我希望你已经在其他地方了解过,如果没有,你可以使用Google去搜索它.在本文中,我将告诉您如何使用专门针对Spring Boot和RxJava的 ...
- 记一次oracle新建用户及分配指定表权限的操作记录
1.登录 2.创建用户create user new用户名 identified by new用户名创建new用户名用户,密码设置为new用户名. 3.授权new用户名用户的连接.资源权限.grant ...
- 记录一次oracle的坑
背景:程序正常运行中,突然技术支持人员反映数据库数据好久没有增加,于是乎各种排查问题,但是一直没有找到原因,由于代码比较久,也不是本人所写,更气的是居然用的是oracle数据库,并且是通过java代码 ...
- oracle 常用脚本以及语句
oracle 常用脚本以及语句 一.oracle 安装10G 单机初始化环境: #!/bin/bash #关闭selinuxsed -i 's\SELINUX=enforcing\SELINUX=di ...
- 52 (OC)* 苹果手机各种尺寸详细表以及iPhoneX、iPhoneXS、iPhoneXR、iPhoneXSMax屏幕适配
iPhone设备 物理分辨率是硬件所支持的,逻辑分辨率是软件可以达到的. 代数 设备 操作系统 逻辑分辨率(point) 物理分辨率(pixel) 屏幕尺寸(对角线长度) 缩放因子 iPhone ...
- JsonConvert 转DateTime类型为json 带T
在调用接口的时候 将Model转换成json Datetime类型多了个T 用的是Newtonsoft.Json.dll 版本v4.5.0.0 代码:paramsjson = JsonConvert ...
- java线上cpu、内存问题排查方法
一.线程 查进程中占用cpu高的线程 ps -mp xxxxx -o THREAD,tid,time | sort -rn 将线程的id从10位转到16位,可以在下面jstack中找到对应线程 输出线 ...
- Spring框架学习笔记(2)——面向切面编程AOP
介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...
- 视频转成在github的readme中展示项目的gif动图
本文中涉及的FastStone Capture和FFmpeg两个软件的百度网盘链接: 链接:https://pan.baidu.com/s/1D5LO9Qmjl-vwJZfnbAloyQ 提取码:56 ...
- mysql备份脚本并保留7天
脚本需求: 每天备份mysql数据库,保留7天的脚本. 存放在/opt/dbbak目录中. 脚本名称为database_xxxx-xx-xx.sql 脚本内容: #!/bin/bash export ...