SpringBoot+MyBatisPlus实现读写分离
前言
随着业务量的不断增长,数据库的读写压力也越来越大。为了解决这个问题,我们可以采用读写分离的方案来分担数据库的读写负载。本文将介绍如何使用 Spring Boot + MyBatis Plus + MySQL 实现读写分离。
读写分离原理
读写分离是指将数据库的读操作和写操作分别放到不同的数据库实例上,从而达到分担数据库负载的目的。一般情况下,写操作的频率比读操作高,因此可以将写操作放到主库上,将读操作放到从库上。这样可以保证主库的写入性能,同时也可以提高从库的读取性能。
实现步骤
1. 主从复制搭建
首先,我们需要创建两个数据库实例,一个用于主库,一个用于从库。这里我们使用 MySQL 数据库作为示例。
参见搭建mysql主从复制一文。
2.配置pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.13</version>
</dependency>
3. 配置数据源
在 Spring Boot 中,我们可以使用 application.yml
文件来配置数据源。在这里,我们需要配置两个数据源,一个用于主库,一个用于从库。具体配置如下:
mysql:
datasource:
readNum: 1
type: com.alibaba.druid.pool.DruidDataSource
write:
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.10.26.212:3306/test?useSSL=false&useUnicode=true
minIdle: 5
maxActive: 100
initialSize: 10
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 50
removeAbandoned: true
filters: stat
read1:
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.10.26.213:3306/test?useSSL=false&useUnicode=true
minIdle: 5
maxActive: 100
initialSize: 10
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 50
removeAbandoned: true
filters: stat
4. 配置 MyBatis Plus
MyBatis Plus 是一个 MyBatis 的增强工具,它可以简化 MyBatis 的开发流程。在这里,我们需要在 application.yml
文件中配置 MyBatis Plus 的相关参数。具体配置如下:
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
typeAliasesPackage: com.sandy.dyds.model
5. 实现读写分离
首先,我们需要创建一个 DbContextHolder 类,用于保存当前线程使用的数据源。具体实现如下:
@Log4j2
public class DbContextHolder {
public static final String WRITE = "write";
public static final String READ = "read";
private static ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDbType(String dbType) {
if(dbType == null) {
throw new NullPointerException();
}
contextHolder.set(dbType);
}
public static String getDbType() {
return contextHolder.get() == null ? WRITE : contextHolder.get();
}
public static void clearDbType() {
contextHolder.remove();
}
}
然后,我们需要创建一个 RoutingDataSource 类,用于动态切换数据源。具体实现如下:
@Log4j2
public class RoutingDataSource extends AbstractRoutingDataSource {
@Value("${mysql.datasource.readNum}")
private int num;
@Override
protected Object determineCurrentLookupKey() {
String typeKey = DbContextHolder.getDbType();
if(typeKey.equals(DbContextHolder.WRITE)) {
return typeKey;
}
//使用随机数决定使用哪个读库
//在1-N之间生成整型随机数
int random = (int) (Math.random() * 1) + num;
return DbContextHolder.READ + random;
}
}
接着,我们需要创建一个 DataSourceConfig 类,用于配置数据源。具体实现如下:
@Configuration
public class DataSourceConfig {
@Value("${mysql.datasource.type}")
private Class<? extends DataSource> dataSourceType;
/**
* 写数据源
* Primary 标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。
* 多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean
*/
@Primary
@Bean
@ConfigurationProperties(prefix = "mysql.datasource.write")
public DataSource writeDataSource() {
DataSource ds = DataSourceBuilder.create().type(dataSourceType).build();
return ds;
}
@Bean
@ConfigurationProperties(prefix = "mysql.datasource.read1")
public DataSource readDataSource_1() {
DataSource ds = DataSourceBuilder.create().type(dataSourceType).build();
return ds;
}
/**
* 设置数据源路由,通过该类中的determineCurrentLookupKey决定使用哪个数据源
*/
@Bean
public AbstractRoutingDataSource routingDataSource() {
RoutingDataSource proxy = new RoutingDataSource();
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DbContextHolder.WRITE, writeDataSource());
targetDataSources.put(DbContextHolder.READ + "1", readDataSource_1());
proxy.setDefaultTargetDataSource(writeDataSource());
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
/**
* 由于Spring容器中现在有多个数据源,所以我们需要为事务管理器和MyBatis手动指定一个明确的数据源。
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(routingDataSource());
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
sqlSessionFactory.setConfiguration(configuration);
return sqlSessionFactory.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(routingDataSource());
}
}
最后,我们需要使用AOP以注解方式切换只读数据库。具体实现如下:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnly {
}
@Aspect
@Component
@Log4j2
public class DataSourceAop implements Ordered {
@Around("@annotation(readOnly)")
public Object setRead(ProceedingJoinPoint joinPoint, ReadOnly readOnly) throws Throwable{
try {
DbContextHolder.setDbType(DbContextHolder.READ);
return joinPoint.proceed();
} finally {
//清除DbType一方面为了避免内存泄漏,更重要的是避免对后续在本线程上执行的操作产生影响
DbContextHolder.clearDbType();
log.info("清除threadLocal");
}
}
@Override
public int getOrder() {
return 0;
}
}
总结
通过本文的介绍,我们了解了如何使用 Spring Boot + MyBatis Plus + MySQL 实现读写分离。读写分离可以有效地分担数据库的读写负载,提高数据库的性能和可用性。希望本文能对读写分离的实现有所帮助。
SpringBoot+MyBatisPlus实现读写分离的更多相关文章
- SpringBoot使用Sharding-JDBC读写分离
本文介绍SpringBoot使用当当Sharding-JDBC进行读写分离. 1.有关Sharding-JDBC 本文还是基于当当网Sharding-Jdbc的依赖,与上一篇使用Sharding-Jd ...
- SpringBoot+MyBatis+MySQL读写分离
1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是依 ...
- SpringBoot+MyBatis+MySQL读写分离(实例)
1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是 ...
- Springboot + Mysql8实现读写分离
在实际的生产环境中,为了确保数据库的稳定性,我们一般会给数据库配置双机热备机制,这样在master数据库崩溃后,slave数据库可以立即切换成主数据库,通过主从复制的方式将数据从主库同步至从库,在业务 ...
- SpringBoot + MyBatis + MySQL 读写分离实战
1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是依靠 ...
- SpringBoot 玩转读写分离
环境概览 前言介绍 Sharding-JDBC是当当网的一个开源项目,只需引入jar即可轻松实现读写分离与分库分表.与MyCat不同的是,Sharding-JDBC致力于提供轻量级的服务框架,无需额外 ...
- 分库分表(3) ---SpringBoot + ShardingSphere 实现读写分离
分库分表(3)---ShardingSphere实现读写分离 有关ShardingSphere概念前面写了两篇博客: 1.分库分表(1) --- 理论 2. 分库分表(2) --- ShardingS ...
- 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离
分库分表(6)--- ShardingSphere实现分表+ 读写分离 有关分库分表前面写了五篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论 ...
- 分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离
分库分表(7)--- ShardingSphere实现分库分表+读写分离 有关分库分表前面写了六篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理 ...
- Sharding-JDBC基本使用,整合Springboot实现分库分表,读写分离
结合上一篇docker部署的mysql主从, 本篇主要讲解SpringBoot项目结合Sharding-JDBC如何实现分库分表.读写分离. 一.Sharding-JDBC介绍 1.这里引用官网上的介 ...
随机推荐
- 微软博客上几篇 Semantic-kernel (SK)文章
自从最近微软开源Semantic-kernel 来帮助开发人员在其应用程序中使用AI大型语言模型(LLM)以来,Microsoft一直在忙于改进它,发布了有关如何使用它的新指南并发布了5篇文章介绍他 ...
- 在ArcGIS Pro中对Revit的bim数据进行地理配准(平移、旋转等)
在ArcGIS Pro中,打开Revit的rvt格式数据,默认是没有坐标系,且位置会放置在原点位置(0,0),在实际使用过程中,需要对rvt数据进行地理配准,包括平移.旋转等操作将bim数据放置在正确 ...
- 【过滤器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
简介 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern),是一种结构型模式.这种模式允许使用不同的标准条件来过滤一组对象,并通过逻辑运算的方式把各条件连接起来,它 ...
- kubernetes(k8s)安装命令行自动补全功能
Ubuntu下安装命令 root@master1:~# apt install -y bash-completion Reading package lists... Done Building de ...
- day3 函数的定义和调用,练习编写简单的程序(记录1)
一.函数的定义 可以分为以下两种: 1.函数声明和函数定义分离 这种方法将函数声明和函数定义分开,通常在头文件中先声明函数原型,然后在源文件中实现函数定义. 例如,头文件 example.h 中声明了 ...
- arc076f F - Exhausted?
ARC076 F - Exhausted? [题目大意] \(有m个座位,分别位于坐标为1,2,3,...,m的地方:n个客人,第i位客人只坐位于[0,li]∪[ri,m]的座位.每个座位只能坐一个人 ...
- 程序猿要chatpgpt干掉了?
如何拥抱被chatpgpt拉开的人工智能大时代 昨天 chatgpt-4 发布了.我看到好多技术圈的人都惶恐着,以后咱们都要失业了/(ㄒoㄒ)/~~ 和之前差不多的是毫无意外地又引动了一大波舆论.虽然 ...
- [Pytorch框架] 5.1 kaggle介绍
文章目录 5.1 kaggle介绍 5.1.1 Kaggle 平台简介 比赛介绍 5.1.2 Kaggle板块介绍 Data Rules Team Kernels Discussion Leaderb ...
- 【解决方法】windows连接域时报错:An Active Directory Domain Controller(AD DC) for the domain“chinaskills.com“....
目录-快速跳转 问题描述 原因分析: 解决方案: 附言: 问题描述 操作环境与场景: 在 VM 内 windos 2019 在连接到域时,提示报错: An Active Directory Domai ...
- 文盘Rust -- rust连接oss
作者:京东科技 贾世闻 对象存储是云的基础组件之一,各大云厂商都有相关产品.这里跟大家介绍一下rust与对象存储交到的基本套路和其中的一些技巧. 基本连接 我们以 aws 对象存储的sdk为例来说说基 ...