ShardingSphere实现分库分表+读写分离

有关分库分表前面写了六篇博客:

1、分库分表(1) --- 理论

2、分库分表(2) --- ShardingSphere(理论)

3、分库分表(3) ---SpringBoot + ShardingSphere实现读写分离

4、分库分表(4) ---SpringBoot + ShardingSphere 实现分表

5、分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表

6、分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离

这篇博客通过ShardingSphere实现分库分表 + 读写分离,并在文章最下方附上项目Github地址

一、项目概述

1、技术架构

项目总体技术选型

SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4  + MySQL + lombok(插件)

2、项目说明

场景 在实际开发中,如果数据库压力大我们可以通过 分库分表 的基础上进行 读写分离,来减缓数据库压力。

3、数据库设计

分库 ms单库分库分为 ms0库 和 ms1库。

分表 tab_user单表分为tab_user0表 和 tab_user1表。

读写分离 数据写入ms0库 和 ms1库,数据读取 sl0库 和 sl1库。

如图

ms0 ---主库

ms1 ---主库

sl0 ---从库

sl1 ---从库

说明 初始数据的时候,这边只有 sl0从库 我插入了一条数据。那是因为我们这个项目中Mysql服务器并没有实现主从部署,这四个库都在同一服务器上,所以

做不到主数据库数据自动同步到从数据库。所以这里在从数据库建一条数据。等下验证的时候,我们只需验证数据是否存入ms0ms1,数据读取是否在sl0sl1

具体的创建表SQL也会放到GitHub项目里

二、核心代码

说明 完整的代码会放到GitHub上,这里只放一些核心代码。

1、application.properties

server.port=8082

#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
#打印sql
spring.shardingsphere.props.sql.show=true
#数据源
spring.shardingsphere.datasource.names=master0,slave0,master1,slave1 spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ms0?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=root spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://localhost:3306/sl0?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=root spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ms1?characterEncoding=utf-8
spring.shardingsphere.datasource.master1.username=root
spring.shardingsphere.datasource.master1.password=root spring.shardingsphere.datasource.slave1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.url=jdbc:mysql://localhost:3306/slave1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=root #根据年龄分库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=master$->{age % 2}
#根据id分表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master$->{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} #指定master0为主库,slave0为它的从库
spring.shardingsphere.sharding.master-slave-rules.master0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.master0.slave-data-source-names=slave0
#指定master1为主库,slave1为它的从库
spring.shardingsphere.sharding.master-slave-rules.master1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.master1.slave-data-source-names=slave1

Sharding-JDBC可以通过JavaYAMLSpring命名空间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:8082/save-user

我们可以从商品接口代码中可以看出,它会批量插入5条数据。我们先看控制台输出SQL语句

我们可以从SQL语句可以看出 master0master1 库中都插入了数据。

我们再来看数据库

ms0.tab_user0

ms0.tab_user1

ms1.tab_user0

ms1.tab_user1

完成分库分表插入数据。

2、获取数据

这里获取列表接口的SQL。

  select *  from tab_user

请求接口结果

结论 从接口返回的结果可以很明显的看出,数据存储在主库,而数据库的读取在从库。

注意 ShardingSphere并不支持CASE WHENHAVINGUNION (ALL)有限支持子查询。这个官网有详细说明。

Github地址https://github.com/yudiandemingzi/spring-boot-sharding-sphere

参考

1、ShardingSphere中文文档

2、ShardingSphere官网

3、Shardingsphere Github库

 我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(20)

分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离的更多相关文章

  1. 分库分表(5) ---SpringBoot + ShardingSphere 实现分库分表

    分库分表(5)--- ShardingSphere实现分库分表 有关分库分表前面写了四篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3. ...

  2. 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离

    分库分表(6)--- ShardingSphere实现分表+ 读写分离 有关分库分表前面写了五篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论 ...

  3. 分库分表(4) ---SpringBoot + ShardingSphere 实现分表

    分库分表(4)--- ShardingSphere实现分表 有关分库分表前面写了三篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论) 3.分库 ...

  4. 分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离

    分库分表(3)---ShardingSphere实现读写分离 有关ShardingSphere概念前面写了两篇博客: 1.分库分表(1) --- 理论 2. 分库分表(2) --- ShardingS ...

  5. 分库分布的几件小事(五)MYSQL读写分离

    1.为什么进行读写分离 这个,高并发这个阶段,那肯定是需要做读写分离的,啥意思?因为实际上大部分的互联网公司,一些网站,或者是app,其实都是读多写少.所以针对这个情况,就是写一个主库,但是主库挂多个 ...

  6. 重新学习Mysql数据13:Mysql主从复制,读写分离,分表分库策略与实践

    一.MySQL扩展具体的实现方式 随着业务规模的不断扩大,需要选择合适的方案去应对数据规模的增长,以应对逐渐增长的访问压力和数据量. 关于数据库的扩展主要包括:业务拆分.主从复制.读写分离.数据库分库 ...

  7. 读写分离&分库分表学习笔记

    读写分离 何为读写分离? 见名思意,根据读写分离的名字,我们就可以知道:读写分离主要是为了将对数据库的读写操作分散到不同的数据库节点上. 这样的话,就能够小幅提升写性能,大幅提升读性能. 我简单画了一 ...

  8. mycat+mysql集群:实现读写分离,分库分表

    1.mycat文档:https://github.com/MyCATApache/Mycat-doc       官方网站:http://www.mycat.org.cn/ 2.mycat的优点: 配 ...

  9. MySQL+MyCat分库分表 读写分离配置

    一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件.运行在代码应用和MySQL数据库之间的应用. 前身 : cor ...

随机推荐

  1. 手把手告诉你如何安装多个版本的node,妈妈再也不用担心版本高低引发的一系列后遗症(非常详细,非常实用)

    简介 最近好多人都问到node怎么同时安装多个版本? 如何配置node的环境变量,如何自如的在多个版本中切换node?还有就是自己在做appium自动化的时候,有时候会因为node的版本过高或者是太低 ...

  2. TypeScript + React + Redux 实战简单天气APP全套完整项目

    下载链接:https://www.yinxiangit.com/171.html 目录: 从面向过程的js到面向对象的js,让web前端更加高大尚.让你的前端步步日上,紧跟技术发展的前沿.让你构建更加 ...

  3. Git学习-上传项目到github

    现在流行把项目代码上传到git上,今天试了好久,终于成功上传到git了,特做点笔记. 准备工作 在github上注册一个账号,创建一个仓库. 创建好仓库,得到它的地址: 开始上传 一.新建一个文件夹, ...

  4. 开发利器JRebel部署SpringBoot项目

    不要以为年纪轻轻就跌倒了人生谷底,未来还有更大的下降空间等着你. idea下载和安装JRebel 激活JRebel 访问https://my.jrebel.com/ 使用facebook或twitte ...

  5. opencv边缘检测报错

    cnts = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] if ...

  6. Java相关PDF书籍与教程免费下载

    场景 我的CSDN: https://blog.csdn.net/BADAO_LIUMANG_QIZHI 我的博客园: https://www.cnblogs.com/badaoliumangqizh ...

  7. 关于设置tomcat端口为80的事

    今天有人要求tomcat服务器的访问地址不能带端口访问, 也就是说只能用80端口访问网站. 那么问题来了, Ubuntu系统禁止root用户以外的用户访问1024以下的商品, 因此tomcat 默认为 ...

  8. spring boot 整合mybatis 的xml版本【包括逆向工程以及分页插件】

    逆向工程很方便,可以直接根据数据库和配置文件生成pojo,mapper接口和相应的映射文件. xml版本和全注解版本其实差不多,大部分情况下,都会保留xml文件方便其他人去扩展新的dml方法. 文章旨 ...

  9. C++虚函数表和对象存储

    C++虚函数表和对象存储 C++中的虚函数实现了多态的机制,也就是用父类型指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数,这种技术可以让父类的指针有"多种形态",这 ...

  10. C#深入学习笔记 - 可空类型与构造函数默认参数

    在实际开发中或许可能会遇到某个属性需要提供一个默认参数,如果该参数是引用类型的话,可以通过 使用 null 来表示未知的值,但如果是int或 其他值类型的话就有点不好办了,因为如果需要一个int或fl ...