分库分表(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 一主多从+分库分 ...
随机推荐
- 计蒜客 ACM训练联盟周赛 第一场 Christina式方格取数 思维
助手Christina发明了一种方格取数的新玩法:在n*m的方格棋盘里,每个格子里写一个数.两个人轮流给格子染色,直到所有格子都染了色.在所有格子染色完后,计算双方的分数.对于任意两个相邻(即有公共边 ...
- git连接远程分支
今天在再用git连接到远程的dev分支的时候出现了下面的情况 $ git checkout -b dev origin/dev fatal: 'origin/dev' is not a commit ...
- 使用Git初始化本地仓库并首次提交代码
本文介绍使用Git初始化本地仓库,并首次提交代码到远程仓库GitLab上面. 首先,登录GitLab,创建一个新项目的私人仓库: 然后,在本地仓库(就是你写代码文件夹),右键,Git Bash Her ...
- CNCF 宣布成立应用交付领域小组,正式开启云原生应用时代
作者|赵钰莹 作为云原生领域的顶级开源社区, Cloud Native Computing Foundation (云原生基金会,以下简称 CNCF)近日宣布成立 Application Delive ...
- TCP/IP协议,TCP与平台通信,通讯协议压力测试(python)
最近的项目来了一个需求,要求测试tcp网关通讯协议: 1.液压井盖通过TCP/IP TCP与平台通信: 2.硬件定期发送心跳包(10S)给平台,是平台与硬件保持长连接: 3.每台硬件有一个12字节的唯 ...
- WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- CSS精灵图合成工具
链接:http://pan.baidu.com/s/1o7OUUVo 提取密码:rmnx
- 垃圾佬的旅游III(Hash + 暴力)
题目链接:http://120.78.128.11/Problem.jsp?pid=3445 最开始的思路就是直接暴力求解,先把所有的数值两两存入结构体,再从小到大枚举.用二分的思路去判断数值以及出现 ...
- zookeeper伪集群
Zookeeper运行需要java环境,需要安装jdk,建议本地下载好需要的安装包然后上传到服务器上面,服务器上面下载速度太慢. 上传 [root@192 ~]# java -version java ...
- 从Hybrid到React-Native: JS在移动端的南征北战史
注:因为不了解Dart,所以本文不对flutter相关内容进行阐述, 实在抱歉 Hybrid Hybird是一种混合开发应用,可以实现JS和Java代码的互通,单纯使用ios/android原生实现, ...