读写分离是为了确保数据库产品的稳定性,很多数据库拥有双机热备功能。

原理:让主数据库(master)处理事务性增、改、删操作,而从数据库(slave)处理select查询操作。

注意:

  • Sharding-JDBC 通过 sql 语句语义分析,实现读写分离过程,不会做数据同步。
  • 数据同步需要数据库搭建主从集群架构,来做数据之间的主从同步。

关于数据库的主从集群架构自行搭建,下面从 Sharding-JDBC 的应用层来做数据的读写分离。

一、读写分离

1、application.properties 配置文件

在 application.properties 配置文件中进行

# 配置真实数据源
spring.shardingsphere.datasource.names=db1,db2
# 配置第1个数据源
spring.shardingsphere.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.db1.url=jdbc:mysql://localhost:3306/sharding_db1?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=123456
# 配置第2个数据源
spring.shardingsphere.datasource.db2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.db2.url=jdbc:mysql://localhost:3306/sharding_db2?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.shardingsphere.datasource.db2.username=root
spring.shardingsphere.datasource.db2.password=123456##配置主从逻辑数据源,读写分离规则, db1主库,db2从库
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=db1
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names[0]=db2
#基于读写分离的表分片
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds0.course_$->{1..2}# 指定表的主键生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#表策略
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{id%2+1}# 打开shardingsphere的sql日志输出。
spring.shardingsphere.props.sql.show=true# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

2、测试

1)保存

   @Testpublic void testSave() throws InterruptedException {for (int i = 0; i < 4; i++) {Course course = new Course();course.setGmtCreate(new Date());course.setName("java");course.setUserId(1001L + i);course.setStatus("1");TimeUnit.MILLISECONDS.sleep(50);int count = courseMapper.insert(course);System.out.println("id ->" + course.getId());}}

从日志中看到,保存操作的数据库都是主库(db1)。

2)查询

    @Testpublic void testIN() {QueryWrapper<Course> wrapper = new QueryWrapper<Course>();wrapper.in("id",1515577381737865217L, 1515492164109004802L, 1515576508269936641L);List<Course> courses = courseMapper.selectList(wrapper);System.out.println(courses.size());courses.forEach(course -> System.out.println(course));}

从日志中看到,查询操作的数据库都是从库(db2)。

因为 id=1515577381737865217记录在主库(db1)中。数据还没有同步到从库(db2),所以查不到。

– 求知若饥,虚心若愚。

ShardingSphere读写分离相关推荐

  1. shardingsphere读写分离+分表【笔记】

    shardingsphere读写分离+分表 用到的框架:mybatis-plus.druid.shardingsphere-jdbc-spring-boot-starter 依赖(gradle配置,m ...

  2. Springboot2.x +JPA 集成 Apache ShardingSphere 读写分离

    分库分表背景: 数据库性能瓶颈:主要分为按照业务来划分或者按照数据量来划分. 拆分方式: 水平拆分(每个表的结构都一样):订单表数据量大,我们可以水平拆分 ,分成order表1.order表2.ord ...

  3. 读写分离怎么实现_项目中如何实现读写分离?怎么配置?

    上篇文章中,在两个 windows 系统的电脑上安装了最新版 8.0.21 MySQL 数据库,并且配置了主从.MySQL如何配置读写分离? 主从复制的原理思想也很简单,就是从库不断地同步主库的改动, ...

  4. 【Spring Boot 实战】数据库千万级分库分表和读写分离实战

    一. 前言 前几天时间写了如何使用Sharding-JDBC进行分库分表和读写分离的例子,相信能够感受到Sharding-JDBC的强大了,而且使用配置都非常干净.官方支持的功能还很多功能分布式主键. ...

  5. 6、ShardingSphere 之 读写分离

    文章目录 1 读写分离原理 2 Mysql 配置主从复制 2.1 创建2个Mysql 数据库服务,并启动两个Mysql服务 2.2 配置Master库的/etc/my.cnf 2.3 配置Slave库 ...

  6. ShardingSphere(七) 读写分离配置,实现分库读写操作

    概述:本章通过介绍使用ShardingSphere实现数据库的读写分离操作.在实现读写分离之前,数据库的主从同步需要提前配置完成,主从同步实现不由Sharding提供.主从同步可参考上一章节<S ...

  7. ShardingSphere JDBC 分库分表 读写分离 数据加密

    简介 在上篇文章中,在本地搭建了运行环境,本地来体验下ShardingSphere JDBC的三个功能:分库分表.读写分离.数据加密 示例运行 首先把概念先捋一捋,参考下面的文档: 数据分片 读写分离 ...

  8. shardingsphere之sharding-proxy读写分离学习笔记

    shardingsphere之sharding-proxy读写分离学习笔记 引言 重要提示 演示环境 版本信息 代码示例和参考 读写分离简介 主从复制 主从复制的原理和流程 sharding-prox ...

  9. Shardingsphere的分库分表+读写分离+分页条件查询

    Shardingsphere的分库分表与读写分离 导入依赖 <dependencies><dependency><groupId>org.springframewo ...

最新文章

  1. mysql filter_MySQL 过滤复制+复制映射 配置方法
  2. 微信小程序弹出用户授权弹窗,微信小程序引导用户授权,获取位置经纬度
  3. python画圆简单代码-Python 用turtle实现用正方形画圆的例子
  4. 云原生产业联盟成立 蚂蚁金服当选为理事单位
  5. 乔治华盛顿大学计算机科学专业,乔治华盛顿大学计算机专业排名如何
  6. 【机器学习基础】数学推导+纯Python实现机器学习算法17:XGBoost
  7. KEIL 默认 char 是无符号的
  8. 办公技巧:Excel日常高频使用技巧,赶快收藏吧!
  9. NHibernet能带来什么呢?
  10. excel趋势线公式导出_除了类 Excel, SpreadJS 表格控件还能为系统开发带来什么价值?...
  11. JQuery 1.8.3对IE9兼容问题getAttribute
  12. 数据库系统概论第五版 (第 1 章 绪论 ) 笔记
  13. 微信小程序上传图片到云储存中
  14. gis差值分析_GIS空间插值分析图解
  15. iconv 判断字符编码_iconv函数文字编码格式转换
  16. AM335x开发环境搭建 基于MYD-AM335x开发板 超详细
  17. 清理浏览器html缓存图片吗,浏览器怎么清除缓存
  18. input正则邮箱_JS正则表达式验证email邮箱是否正确
  19. “IP地址/24”是什么意思
  20. gos-log高性能大日志检索中台

热门文章

  1. mysql的sql_quote_show_create与SHOW CREATE TABLE命令介绍
  2. 鸿蒙应用开发 | 选项卡(TabList / Tab)的功能和用法
  3. jq获取字符串中的数字 jq如何获取id里的数字
  4. C#专题 | TcpIp通信
  5. VR、AR、MR、XR
  6. WINDOW 注册表添加启动项
  7. halcon 返回XLD轮廓全局属性值 get_contour_global_attrib_xld
  8. 移动端web开发——像素的适配
  9. 人工智能深入油气领域,百度智能云与石化盈科共建合同智能化应用平台
  10. 构建安装ARM Ubuntu系统