跟我学Springboot开发后端管理系统5:数据库读写分离
在Matrix-web后台管理系统中,使用到了数据库的读写分离技术。采用的开源的Sharding-JDBC作为数据库读写分离的框架。Matrix-Web后台数据库这一块采用的技术栈如下:
使用Mybatis-Plus作为ORM框架
使用Druid或者HikariCP作为数据库连接池
使用Sharding-JDBC 作为数据库读写分离的框架
本篇文章将讲述在Sharding-JDBC在Matrix-Web中的应用。
Sharding-JDBC简介
Sharding-JDBC是的分布式数据库中间件解决方案。Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(计划中)是3款相互独立的产品,共同 组成了ShardingSphere。Sharding-JDBC定位于轻量级的Java框架,它使用客户端直连数据库,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。
适用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。
基于任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。
支持任意实现JDBC规范的数据库。目前支持MySQL,Oracle,SQLServer和PostgreSQL。
架构图如下:
支持以下的特效:
分库分表
读写分离
柔性事务
分布式主键
分布式治理能力
Spring Boot中使用Sharding-JDBC
在Matrix-Web项目中并没有搭建数据库的主从,所以只能用一个库去模仿数据,关于数据库的读写分离和分库分表,可以参考我的另外几篇文章:
Sharding-JDBC教程:Mysql数据库主从搭建
Sharding-JDBC教程:Spring Boot整合Sharding-JDBC实现读写分离
Sharding-JDBC教程:Spring Boot整合Sharding-JDBC实现数据分表+读写分离
Sharding-JDBC教程:Spring Boot整合Sharding-JDBC实现分库分表+读写分离
在本项目中,采用相同的库区模拟主库和从库:
数据库类型 | 数据库 | ip |
---|---|---|
主 | aries | 127.0.0.1 |
从 | aries | 127.0.0.1 |
Sharding-Jdbc集合Druid和MyBatis-Plus
在本篇文章中使用Spring Boot 2.0.3+Druid+Sharding-JDBC+MyBatis-Plus+MySQL进行读写分离的案件讲解。
在工程的pom文件引入以下的依赖,包括Spring Boot的Web起步依赖spring-boot-starter-web,mybatis的起步依赖mybatis-spring-boot-starter, mysql的连机器,连接池druid的起步依赖druid-spring-boot-starter,sharding-jdbc的起步依赖sharding-jdbc-spring-boot-starter。代码如下:
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version>
</dependency><dependency><groupId>io.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>3.1.0.M1</version>
</dependency>
在spring boot工程配置文件application.yml做以下的配置:
sharding:jdbc:dataSource:names: db-test0,db-test1,db-test2# 配置主库db-test0: #org.apache.tomcat.jdbc.pool.DataSourcetype: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/aries?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456#最大连接数maxPoolSize: 20db-test1: # 配置第一个从库type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/aries?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456maxPoolSize: 20db-test2: # 配置第二个从库type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/aries?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456maxPoolSize: 20config:masterslave: # 配置读写分离load-balance-algorithm-type: round_robin # 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询name: db1s2master-data-source-name: db-test0slave-data-source-names: db-test1,db-test2props:sql: # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!show: falsemybatis-plus:datasource: dataSourcemapper-locations: classpath:/mapper/*Mapper.xml#实体扫描,多个package用逗号或者分号分隔typeAliasesPackage: io.github.forezp.modules.system.entitytypeEnumsPackage: io.github.forezp.modules.system.entity.enumsglobal-config:db-config:id-type: id_workercapital-mode: true#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";id-type: 2#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"field-strategy: 2#驼峰下划线转换db-column-underline: true#刷新mapper 调试神器refresh-mapper: true#数据库大写下划线转换#capital-mode: true#序列接口实现类配置#key-generator: com.baomidou.springboot.xxx#逻辑删除配置logic-delete-value: 0logic-not-delete-value: 1#自定义填充策略接口实现
# meta-object-handler: com.baomidou.springboot.xxx#自定义SQL注入器#sql-injector: com.baomidou.springboot.xxxconfiguration:map-underscore-to-camel-case: truecache-enabled: falselog-impl: org.apache.ibatis.logging.stdout.StdOutImpl
sharding.jdbc.dataSource.names配置的是数据库的名称,就是多个数据源的名称。sharding.jdbc.dataSource配置多个数据源。需要配置数据库名称,和上面配置的对应。以及数据的配置,包括连接池的类型、连接器、数据库地址、 数据库账户密码信息等。sharding.jdbc.config.masterslave.load-balance-algorithm-type查询时的负载均衡算法,目前有2种算法,round_robin(轮询)和random(随机)。sharding.jdbc.config.masterslave.master-data-source-name主数据源名称。sharding.jdbc.config.masterslave.slave-data-source-names从数据源名称,多个用逗号隔开。
Sharing-JDBC结合HikariCP
在pom文件中将Druid的起步依赖换成HikariCP:
<dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>${HikariCP.version}</version>
</dependency>
在配置文件application将sharing.jdbc.datasouce.db-test1.type改成com.zaxxer.hikari.HikariDataSource,具体如下:
sharding:jdbc:dataSource:names: db-test0,db-test1,db-test2# 配置主库db-test0: #org.apache.tomcat.jdbc.pool.DataSource
# type: com.alibaba.druid.pool.DruidDataSourcetype: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.jdbc.DriverjdbcUrl: jdbc:mysql://localhost:3306/aries?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456#最大连接数maxPoolSize: 20db-test1: # 配置第一个从库
# type: com.alibaba.druid.pool.DruidDataSourcetype: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.jdbc.DriverjdbcUrl: jdbc:mysql://localhost:3306/aries?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456maxPoolSize: 20db-test2: # 配置第二个从库
# type: com.alibaba.druid.pool.DruidDataSourcetype: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.jdbc.DriverjdbcUrl: jdbc:mysql://localhost:3306/aries?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMTusername: rootpassword: 123456maxPoolSize: 20config:masterslave: # 配置读写分离load-balance-algorithm-type: round_robin # 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询name: db1s2master-data-source-name: db-test0slave-data-source-names: db-test1,db-test2props:sql: # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!show: true
源码下载
参考资料
https://github.com/apache/incubator-shardingsphere-example/releases/tag/3.1.0.M1
https://shardingsphere.apache.org/document/current/cn/overview/
https://github.com/apache/incubator-shardingsphere
参考资料
https://www.rowkey.me/blog/2018/05/12/java-tech-overview/
http://www.51testing.com/html/74/n-3723674.html
源码下载
https://github.com/forezp/matrix-web
往期文章:
跟我学Springboot开发后端管理系统1:概述
跟我学Springboot开发后端管理系统2:Mybatis-Plus实战
跟我学Springboot开发后端管理系统3:Mybatis-Plus实战2
跟我学Springboot开发后端管理系统4:数据库连接池Druid和HikariCP
跟我学Springboot开发后端管理系统5:数据库读写分离相关推荐
- 跟我学Springboot开发后端管理系统9:AOP+logback+MDC日志输出
MDC介绍 在比较复杂的应用中,一个请求需要走很多个方法的处理,怎么样才能快速查找一个请求的全部日志呢.在分布式系统中,我们可以用链路追踪,比如zipkin.skywalking去快速查找日志,从而定 ...
- 跟我学Springboot开发后端管理系统8:Matrxi-Web权限设计实现
上篇文章讲述了Matrix-web整体实现的权限控制的思路.现在来回顾一下: 首先,用户需要登录,填用户名.密码,后端接收到登录请求,进行用户.密码的校验,校验成功后则根据用户名生成Token,并返回 ...
- 跟我学Springboot开发后端管理系统7:Matrxi-Web权限设计
Matrxi-Web权限设计 对于一个后端系统来说,权限是基础设施,是安全保障.没有权限,系统可能随时面临各种风险,所以权限设计对后端系统来说至关重要.在Javaweb开发中,有很多权限开发的框架,比 ...
- 跟我学Springboot开发后端管理系统6:缓存框架Caffeine
Caffeine是一个基于Java8的高性能缓存框架,号称趋于完美.Caffeine受启发于Guava Cache的API,使用API和Guava是一致的.它借鉴了Guava Cache和Concur ...
- 跟我学Springboot开发后端管理系统4:数据库连接池Druid和HikariCP
上一篇文章主要讲解了如何再Matrix-Web中使用Mybatis-Plus,Mybatis-Plus作为Orm框架,连接数据库需要连接数据库的依赖.WEB 系统高并发环境下,频繁的进行数据库连接操作 ...
- 跟我学Springboot开发后端管理系统3:Mybatis-Plus实战2
在上一篇文章讲述了如何使用Mybatis-plus自动生成代码,生成的代码具有单表操作数据库的能力,节约了开发时间.然后讲述了如何在Spring Boot中整合Mybatis-Plus.这篇文章讲述如 ...
- 跟我学Springboot开发后端管理系统2:Mybatis-Plus实战
在Matrix-Web项目中使用Mybatis-Plus作为操作数据库的ORM框架.在市面上常用的ORM框架有hibernetes.mybatis.JPA等,那么为什么选择Mybatis-Plus呢? ...
- mybatis plus springboot test_跟我学Springboot开发后端管理系统3:Mybatis-Plus实战2
文章来源:https://mp.weixin.qq.com/s/YL0FDjn8BczNfIJJ9DgMRA 原文作者:forezp 来源平台:微信公众号 在上一篇文章讲述了如何使用Mybatis-p ...
- 跟我学Springboot开发后端管理系统8:AOP+logback+MDC日志输出
MDC介绍 在比较复杂的应用中,一个请求需要走很多个方法的处理,怎么样才能快速查找一个请求的全部日志呢.在分布式系统中,我们可以用链路追踪,比如zipkin.skywalking去快速查找日志,从而定 ...
最新文章
- mysql计算本月的天数_Mysql已知年、月,求起始日期,本月天数
- 无盘服务器读写缓存,无盘系统中实现网络磁盘本地写缓存控制的方法
- matlab表达一次函数,[转载]MATLAB数据拟合例子(一次函数、指数函数、双曲线)...
- python time 时间差,python如何计算时间差
- S_ISREG等几个常见的宏(转)
- 物理学基石 —— 波、电磁波、微波
- 【数字信号】基于matlab GUI数字电子琴【含Matlab源码 272期】
- html css js php常用网页代码汇总合集(一)网页设计入门代码知识汇总1
- 一套完整的网络视频监控系统设计方案
- 共轭、转置,共轭转置和逆矩阵的性质
- CSDN文章添加版权声明
- PS2021中使用Nik插件崩溃闪退|Nik Collection不兼容cc2021闪退的解决方法
- 【k8s】kubernetes编写自己的operator(operator-sdk:v1.xxx)
- 关于召回率和hit rate
- Redis从入门到精通
- 处女作,,QT+arduino的物联网小项目
- OpenCV图像高光
- TR069协议向导—— 一个帮助你了解TR069协议的简明教程(二)
- 第一章Web技术概述
- 【微信小程序】微信开发者工具的使用