读写分离原理的什么这里就不讲了,直接来看看使用 sharding-jdbc 来实现读写分离

快速入门分库分表中间件 Sharding-JDBC

对于 Sharding-JDBC 还不了解的可以参考我以前的文章 https://andyoung.blog.csdn.net/article/details/116229612

实现读写分离,先看配置

# 配置真实数据源
spring.shardingsphere.datasource.names=ds0,ds1,s0
# 配置第 1 个数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
# 配置第 2 个数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# 配置第 ds0 slave 数据源
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://localhost:3306/ds0-slave?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=123456
# 配置第 ds0 slave rules 主从规则
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s0
  • 配置三个数据源

  • 最后配置主从规则

    # 配置第 ds0 slave rules 主从规则
    spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
    spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s0
    

测试

 @Testpublic void masterSlaveTest() {TOrder tOrder = orderMapper.selectById(1387233602040721410L);System.out.println(tOrder);TOrder order = new TOrder();order.setOrderId(1387233602040721410L);order.setCreateName("订单 主库master update");order.setPrice(new BigDecimal("2.8"));orderMapper.updateById(order);tOrder = orderMapper.selectById(1387233602040721410L);System.out.println(tOrder);}

具体代码案列 https://github.com/andanyoung/springboot/tree/master/sharding-jdbc

Spring boot 实战 利用 Sharding-jdbc实现数据库的读写分离相关推荐

  1. 徐无忌MySQL笔记:案例实战:如何实现MySQL数据库的读写分离?

    徐无忌MySQL笔记:案例实战:如何实现MySQL数据库的读写分离? 完成:第一遍 1.理想的主从架构实现的效果是怎样的? 主库负责所有读写操作,从库只实现对主库备份功能,这样的主从架构性价比是很低的 ...

  2. Spring boot项目集成Sharding Jdbc

    环境 jdk:1.8 framework: spring boot, sharding jdbc database: MySQL 搭建步骤 在pom 中加入sharding 依赖 <depend ...

  3. 【MySQL】Spring Boot项目基于Sharding-JDBC和MySQL主从复制实现读写分离(8千字详细教程)

    目录 前言 一. 介绍 二. 主从复制 1. 原理 2. 克隆从机 3. 克隆从机大坑 4. 远程登陆 5. 主机配置 6. 从机配置 7. 主机:建立账户并授权 8. 从机:配置需要复制的主机 9. ...

  4. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  5. 最新Spring Boot实战项目(权限后台管理系统)详解

    Spring Boot实战项目 - 权限后台管理系统 简介 这是一套基于spring boot 2.16.shiro.jwt.redis.swagger2.mybatis .thymeleaf.lay ...

  6. spring boot 实战 / 可执行war启动参数详解

    概述   上一篇文章<spring boot 实战 / mvn spring-boot:run 参数详解>主要讲解了spring boot 项目基于maven插件启动过程中借助profil ...

  7. Spring Boot实战系列《六》:人事管理系统的登录设计

    Spring Boot实战系列<六>:人事管理系统的登录设计 Spring Boot实战系列<六>:人事管理系统的登录设计 1.前言 在上一篇中教大家在IEDA或者eclips ...

  8. Spring Boot 实战系列课程终于齐啦~

    Spring Boot 如今已成为 Java 开发必学技术,其可以大大简化 Spring 应用的初始搭建以及开发过程. Spring Boot 不仅支持直接嵌入 Tomcat,Jetty 或 Unde ...

  9. Vue + Spring Boot 学习笔记02:引入数据库实现用户登录功能

    Vue + Spring Boot 学习笔记02:引入数据库实现用户登录功能 在学习笔记01里,我们利用跨域打通了前端的Vue与后端的Spring Boot,实现了用户登录功能,但是后台的登录控制器在 ...

最新文章

  1. bzoj 4566 找相同字符 —— 广义后缀自动机
  2. 刷题刷题 ——网易CPP
  3. GoogleTest测试C++代码
  4. python写math函数_pythonmathcot函数_Matplotlib 编写数学表达式
  5. vim python一键执行、高亮等一键安装
  6. C++ windows网络编程系列1—各种通信模型对比
  7. 跑马灯的一些使用心得
  8. linux pt 客户端,下载工具系列——rTorrent (轻量级优秀BT/PT客户端)
  9. Linaro ABE(高级构建环境)构建GNU交叉工具链
  10. 圣诞节,教你用Python给微信头像添加一个圣诞帽
  11. SpringBoot_01_初窥门径
  12. MFC中Combo控件的使用
  13. 实验室安全与危化品信息管理平台建设
  14. SCRM:SpringBoot + RabbitMQ + 企微 实现发送消息到企业微信
  15. linux进程管理杂记(1)
  16. webpack css loader
  17. 社交网站需要多大的服务器空间,社交app选多大云服务器
  18. 小飞升值记——(21)
  19. leetcode刷题记录--数据结构;深度优先搜索算法;二叉树;平衡树;1020. 飞地的数量;1669. 合并两个链表;108. 将有序数组转换为二叉搜索树
  20. mysql安全实验测验答案_实验三_数据库安全性实验报告.doc

热门文章

  1. “中药资源创新院士团队”诚聘结构、合成和计算生物学博士后5-10名
  2. 高级转录组分析和R数据可视化第11期(报名线上课还可免费参加线下课2020.6)
  3. Cell:绝对异养型生物改造成完全自养型生物
  4. IDBA-UD:组装非均匀覆盖度的宏基因组和单细胞数据
  5. 第54期《SCI论文插图排版与绘制》课程欢迎你!
  6. Nature Genetics:每年造成7亿人感染的酿脓链球菌的致病机制
  7. R语言ggplot2可视化在散点图中的每个点上绘制两个错误条:常见的是垂直错误条,它对应于Y值点上的错误(error bar),添加与X轴(水平)相关的错误条(error bar)
  8. R构建KNN分类器实战
  9. Google团队在DNN的实际应用方式的整理
  10. 数字图像处理3:取样和量化