前言

随着业务量的不断增长,数据库的读写压力也越来越大。为了解决这个问题,我们可以采用读写分离的方案来分担数据库的读写负载。本文将介绍如何使用 Spring Boot + MyBatis Plus + MySQL 实现读写分离。

读写分离原理

读写分离是指将数据库的读操作和写操作分别放到不同的数据库实例上,从而达到分担数据库负载的目的。一般情况下,写操作的频率比读操作高,因此可以将写操作放到主库上,将读操作放到从库上。这样可以保证主库的写入性能,同时也可以提高从库的读取性能。

实现步骤

1. 主从复制搭建

首先,我们需要创建两个数据库实例,一个用于主库,一个用于从库。这里我们使用 MySQL 数据库作为示例。

参见搭建mysql主从复制一文。

2.配置pom.xml

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus-boot-starter</artifactId>
  9. </dependency>
  10. <!-- 连接池 -->
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>druid-spring-boot-starter</artifactId>
  14. <version>1.1.9</version>
  15. </dependency>
  16. <!--aop -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-aop</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.aspectj</groupId>
  23. <artifactId>aspectjtools</artifactId>
  24. <version>1.8.13</version>
  25. </dependency>

3. 配置数据源

在 Spring Boot 中,我们可以使用 application.yml 文件来配置数据源。在这里,我们需要配置两个数据源,一个用于主库,一个用于从库。具体配置如下:

  1. mysql:
  2. datasource:
  3. readNum: 1
  4. type: com.alibaba.druid.pool.DruidDataSource
  5. write:
  6. username: root
  7. password: 123456
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. url: jdbc:mysql://10.10.26.212:3306/test?useSSL=false&useUnicode=true
  10. minIdle: 5
  11. maxActive: 100
  12. initialSize: 10
  13. maxWait: 60000
  14. timeBetweenEvictionRunsMillis: 60000
  15. minEvictableIdleTimeMillis: 300000
  16. validationQuery: select 'x'
  17. testWhileIdle: true
  18. testOnBorrow: false
  19. testOnReturn: false
  20. poolPreparedStatements: true
  21. maxPoolPreparedStatementPerConnectionSize: 50
  22. removeAbandoned: true
  23. filters: stat
  24. read1:
  25. username: root
  26. password: 123456
  27. driver-class-name: com.mysql.cj.jdbc.Driver
  28. url: jdbc:mysql://10.10.26.213:3306/test?useSSL=false&useUnicode=true
  29. minIdle: 5
  30. maxActive: 100
  31. initialSize: 10
  32. maxWait: 60000
  33. timeBetweenEvictionRunsMillis: 60000
  34. minEvictableIdleTimeMillis: 300000
  35. validationQuery: select 'x'
  36. testWhileIdle: true
  37. testOnBorrow: false
  38. testOnReturn: false
  39. poolPreparedStatements: true
  40. maxPoolPreparedStatementPerConnectionSize: 50
  41. removeAbandoned: true
  42. filters: stat

4. 配置 MyBatis Plus

MyBatis Plus 是一个 MyBatis 的增强工具,它可以简化 MyBatis 的开发流程。在这里,我们需要在 application.yml 文件中配置 MyBatis Plus 的相关参数。具体配置如下:

  1. mybatis-plus:
  2. mapper-locations: classpath:mapper/*.xml
  3. configuration:
  4. map-underscore-to-camel-case: true
  5. cache-enabled: false
  6. typeAliasesPackage: com.sandy.dyds.model

5. 实现读写分离

首先,我们需要创建一个 DbContextHolder 类,用于保存当前线程使用的数据源。具体实现如下:

  1. @Log4j2
  2. public class DbContextHolder {
  3. public static final String WRITE = "write";
  4. public static final String READ = "read";
  5. private static ThreadLocal<String> contextHolder = new ThreadLocal<>();
  6. public static void setDbType(String dbType) {
  7. if(dbType == null) {
  8. throw new NullPointerException();
  9. }
  10. contextHolder.set(dbType);
  11. }
  12. public static String getDbType() {
  13. return contextHolder.get() == null ? WRITE : contextHolder.get();
  14. }
  15. public static void clearDbType() {
  16. contextHolder.remove();
  17. }
  18. }

然后,我们需要创建一个 RoutingDataSource 类,用于动态切换数据源。具体实现如下:

  1. @Log4j2
  2. public class RoutingDataSource extends AbstractRoutingDataSource {
  3. @Value("${mysql.datasource.readNum}")
  4. private int num;
  5. @Override
  6. protected Object determineCurrentLookupKey() {
  7. String typeKey = DbContextHolder.getDbType();
  8. if(typeKey.equals(DbContextHolder.WRITE)) {
  9. return typeKey;
  10. }
  11. //使用随机数决定使用哪个读库
  12. //在1-N之间生成整型随机数
  13. int random = (int) (Math.random() * 1) + num;
  14. return DbContextHolder.READ + random;
  15. }
  16. }

接着,我们需要创建一个 DataSourceConfig 类,用于配置数据源。具体实现如下:

  1. @Configuration
  2. public class DataSourceConfig {
  3. @Value("${mysql.datasource.type}")
  4. private Class<? extends DataSource> dataSourceType;
  5. /**
  6. * 写数据源
  7. * Primary 标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。
  8. * 多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean
  9. */
  10. @Primary
  11. @Bean
  12. @ConfigurationProperties(prefix = "mysql.datasource.write")
  13. public DataSource writeDataSource() {
  14. DataSource ds = DataSourceBuilder.create().type(dataSourceType).build();
  15. return ds;
  16. }
  17. @Bean
  18. @ConfigurationProperties(prefix = "mysql.datasource.read1")
  19. public DataSource readDataSource_1() {
  20. DataSource ds = DataSourceBuilder.create().type(dataSourceType).build();
  21. return ds;
  22. }
  23. /**
  24. * 设置数据源路由,通过该类中的determineCurrentLookupKey决定使用哪个数据源
  25. */
  26. @Bean
  27. public AbstractRoutingDataSource routingDataSource() {
  28. RoutingDataSource proxy = new RoutingDataSource();
  29. Map<Object, Object> targetDataSources = new HashMap<>();
  30. targetDataSources.put(DbContextHolder.WRITE, writeDataSource());
  31. targetDataSources.put(DbContextHolder.READ + "1", readDataSource_1());
  32. proxy.setDefaultTargetDataSource(writeDataSource());
  33. proxy.setTargetDataSources(targetDataSources);
  34. return proxy;
  35. }
  36. /**
  37. * 由于Spring容器中现在有多个数据源,所以我们需要为事务管理器和MyBatis手动指定一个明确的数据源。
  38. */
  39. @Bean
  40. public SqlSessionFactory sqlSessionFactory() throws Exception {
  41. MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
  42. sqlSessionFactory.setDataSource(routingDataSource());
  43. MybatisConfiguration configuration = new MybatisConfiguration();
  44. configuration.setJdbcTypeForNull(JdbcType.NULL);
  45. configuration.setMapUnderscoreToCamelCase(true);
  46. configuration.setCacheEnabled(false);
  47. sqlSessionFactory.setConfiguration(configuration);
  48. return sqlSessionFactory.getObject();
  49. }
  50. @Bean
  51. public DataSourceTransactionManager transactionManager() {
  52. return new DataSourceTransactionManager(routingDataSource());
  53. }
  54. }

最后,我们需要使用AOP以注解方式切换只读数据库。具体实现如下:

  1. @Target({ElementType.METHOD, ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface ReadOnly {
  4. }
  1. @Aspect
  2. @Component
  3. @Log4j2
  4. public class DataSourceAop implements Ordered {
  5. @Around("@annotation(readOnly)")
  6. public Object setRead(ProceedingJoinPoint joinPoint, ReadOnly readOnly) throws Throwable{
  7. try {
  8. DbContextHolder.setDbType(DbContextHolder.READ);
  9. return joinPoint.proceed();
  10. } finally {
  11. //清除DbType一方面为了避免内存泄漏,更重要的是避免对后续在本线程上执行的操作产生影响
  12. DbContextHolder.clearDbType();
  13. log.info("清除threadLocal");
  14. }
  15. }
  16. @Override
  17. public int getOrder() {
  18. return 0;
  19. }
  20. }

总结

通过本文的介绍,我们了解了如何使用 Spring Boot + MyBatis Plus + MySQL 实现读写分离。读写分离可以有效地分担数据库的读写负载,提高数据库的性能和可用性。希望本文能对读写分离的实现有所帮助。

SpringBoot+MyBatisPlus实现读写分离的更多相关文章

  1. SpringBoot使用Sharding-JDBC读写分离

    本文介绍SpringBoot使用当当Sharding-JDBC进行读写分离. 1.有关Sharding-JDBC 本文还是基于当当网Sharding-Jdbc的依赖,与上一篇使用Sharding-Jd ...

  2. SpringBoot+MyBatis+MySQL读写分离

    1.  引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是依 ...

  3. SpringBoot+MyBatis+MySQL读写分离(实例)

    ​ 1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是 ...

  4. Springboot + Mysql8实现读写分离

    在实际的生产环境中,为了确保数据库的稳定性,我们一般会给数据库配置双机热备机制,这样在master数据库崩溃后,slave数据库可以立即切换成主数据库,通过主从复制的方式将数据从主库同步至从库,在业务 ...

  5. SpringBoot + MyBatis + MySQL 读写分离实战

    1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是依靠 ...

  6. SpringBoot 玩转读写分离

    环境概览 前言介绍 Sharding-JDBC是当当网的一个开源项目,只需引入jar即可轻松实现读写分离与分库分表.与MyCat不同的是,Sharding-JDBC致力于提供轻量级的服务框架,无需额外 ...

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

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

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

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

  9. 分库分表(7)--- SpringBoot+ShardingSphere实现分库分表 + 读写分离

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

  10. Sharding-JDBC基本使用,整合Springboot实现分库分表,读写分离

    结合上一篇docker部署的mysql主从, 本篇主要讲解SpringBoot项目结合Sharding-JDBC如何实现分库分表.读写分离. 一.Sharding-JDBC介绍 1.这里引用官网上的介 ...

随机推荐

  1. 微软博客上几篇 Semantic-kernel (SK)文章

    自从最近微软开源Semantic-kernel  来帮助开发人员在其应用程序中使用AI大型语言模型(LLM)以来,Microsoft一直在忙于改进它,发布了有关如何使用它的新指南并发布了5篇文章介绍他 ...

  2. 在ArcGIS Pro中对Revit的bim数据进行地理配准(平移、旋转等)

    在ArcGIS Pro中,打开Revit的rvt格式数据,默认是没有坐标系,且位置会放置在原点位置(0,0),在实际使用过程中,需要对rvt数据进行地理配准,包括平移.旋转等操作将bim数据放置在正确 ...

  3. 【过滤器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现

    简介 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern),是一种结构型模式.这种模式允许使用不同的标准条件来过滤一组对象,并通过逻辑运算的方式把各条件连接起来,它 ...

  4. kubernetes(k8s)安装命令行自动补全功能

    Ubuntu下安装命令 root@master1:~# apt install -y bash-completion Reading package lists... Done Building de ...

  5. day3 函数的定义和调用,练习编写简单的程序(记录1)

    一.函数的定义 可以分为以下两种: 1.函数声明和函数定义分离 这种方法将函数声明和函数定义分开,通常在头文件中先声明函数原型,然后在源文件中实现函数定义. 例如,头文件 example.h 中声明了 ...

  6. arc076f F - Exhausted?

    ARC076 F - Exhausted? [题目大意] \(有m个座位,分别位于坐标为1,2,3,...,m的地方:n个客人,第i位客人只坐位于[0,li]∪[ri,m]的座位.每个座位只能坐一个人 ...

  7. 程序猿要chatpgpt干掉了?

    如何拥抱被chatpgpt拉开的人工智能大时代 昨天 chatgpt-4 发布了.我看到好多技术圈的人都惶恐着,以后咱们都要失业了/(ㄒoㄒ)/~~ 和之前差不多的是毫无意外地又引动了一大波舆论.虽然 ...

  8. [Pytorch框架] 5.1 kaggle介绍

    文章目录 5.1 kaggle介绍 5.1.1 Kaggle 平台简介 比赛介绍 5.1.2 Kaggle板块介绍 Data Rules Team Kernels Discussion Leaderb ...

  9. 【解决方法】windows连接域时报错:An Active Directory Domain Controller(AD DC) for the domain“chinaskills.com“....

    目录-快速跳转 问题描述 原因分析: 解决方案: 附言: 问题描述 操作环境与场景: 在 VM 内 windos 2019 在连接到域时,提示报错: An Active Directory Domai ...

  10. 文盘Rust -- rust连接oss

    作者:京东科技 贾世闻 对象存储是云的基础组件之一,各大云厂商都有相关产品.这里跟大家介绍一下rust与对象存储交到的基本套路和其中的一些技巧. 基本连接 我们以 aws 对象存储的sdk为例来说说基 ...