点击上方“方志朋”,选择“设为星标”

回复”666“获取新整理的面试资料

来源:http://t.cn/AiKuJEB9

1. 引言

读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做。因此,一般来讲,读写分离有两种实现方式。第一种是依靠中间件(比如:MyCat),也就是说应用程序连接到中间件,中间件帮我们做SQL分离;第二种是应用程序自己去做分离。这里我们选择程序自己来做,主要是利用Spring提供的路由数据源,以及AOP。

然而,应用程序层面去做读写分离最大的弱点(不足之处)在于无法动态增加数据库节点,因为数据源配置都是写在配置中的,新增数据库意味着新加一个数据源,必然改配置,并重启应用。当然,好处就是相对简单。

img

2. AbstractRoutingDataSource

基于特定的查找key路由到特定的数据源。它内部维护了一组目标数据源,并且做了路由key与目标数据源之间的映射,提供基于key查找数据源的方法。

img

3. 实践

关于配置请参考:

https://www.cnblogs.com/cjsblog/p/9706370.html

3.1. maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cjs.example</groupId><artifactId>cjs-datasource-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>cjs-datasource-demo</name><description></description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.8</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><!--<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.5</version><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version></dependency></dependencies><configuration><configurationFile>${basedir}/src/main/resources/myBatisGeneratorConfig.xml</configurationFile><overwrite>true</overwrite></configuration><executions><execution><id>Generate MyBatis Artifacts</id><goals><goal>generate</goal></goals></execution></executions></plugin>--></plugins></build>
</project>

3.2. 数据源配置

application.yml

spring:datasource:master:jdbc-url: jdbc:mysql://192.168.102.31:3306/testusername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverslave1:jdbc-url: jdbc:mysql://192.168.102.56:3306/testusername: pig   # 只读账户password: 123456driver-class-name: com.mysql.jdbc.Driverslave2:jdbc-url: jdbc:mysql://192.168.102.36:3306/testusername: pig   # 只读账户password: 123456driver-class-name: com.mysql.jdbc.Driver

多数据源配置

package com.cjs.example.config;import com.cjs.example.bean.MyRoutingDataSource;
import com.cjs.example.enums.DBTypeEnum;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;/*** 关于数据源配置,参考SpringBoot官方文档第79章《Data Access》* 79. Data Access* 79.1 Configure a Custom DataSource* 79.2 Configure Two DataSources*/@Configuration
public class DataSourceConfig {@Bean@ConfigurationProperties("spring.datasource.master")public DataSource masterDataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties("spring.datasource.slave1")public DataSource slave1DataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties("spring.datasource.slave2")public DataSource slave2DataSource() {return DataSourceBuilder.create().build();}@Beanpublic DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,@Qualifier("slave1DataSource") DataSource slave1DataSource,@Qualifier("slave2DataSource") DataSource slave2DataSource) {Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);targetDataSources.put(DBTypeEnum.SLAVE1, slave1DataSource);targetDataSources.put(DBTypeEnum.SLAVE2, slave2DataSource);MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();myRoutingDataSource.setDefaultTargetDataSource(masterDataSource);myRoutingDataSource.setTargetDataSources(targetDataSources);return myRoutingDataSource;}}

这里,我们配置了4个数据源,1个master,2两个slave,1个路由数据源。前3个数据源都是为了生成第4个数据源,而且后续我们只用这最后一个路由数据源。

MyBatis配置

package com.cjs.example.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.annotation.Resource;
import javax.sql.DataSource;@EnableTransactionManagement
@Configuration
public class MyBatisConfig {@Resource(name = "myRoutingDataSource")private DataSource myRoutingDataSource;@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(myRoutingDataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));return sqlSessionFactoryBean.getObject();}@Beanpublic PlatformTransactionManager platformTransactionManager() {return new DataSourceTransactionManager(myRoutingDataSource);}
}

由于Spring容器中现在有4个数据源,所以我们需要为事务管理器和MyBatis手动指定一个明确的数据源。

3.3. 设置路由key / 查找数据源

目标数据源就是那前3个这个我们是知道的,但是使用的时候是如果查找数据源的呢?

首先,我们定义一个枚举来代表这三个数据源

package com.cjs.example.enums;public enum DBTypeEnum {MASTER, SLAVE1, SLAVE2;}

接下来,通过ThreadLocal将数据源设置到每个线程上下文中

package com.cjs.example.bean;import com.cjs.example.enums.DBTypeEnum;import java.util.concurrent.atomic.AtomicInteger;public class DBContextHolder {private static final ThreadLocal<DBTypeEnum> contextHolder = new ThreadLocal<>();private static final AtomicInteger counter = new AtomicInteger(-1);public static void set(DBTypeEnum dbType) {contextHolder.set(dbType);}public static DBTypeEnum get() {return contextHolder.get();}public static void master() {set(DBTypeEnum.MASTER);System.out.println("切换到master");}public static void slave() {//  轮询int index = counter.getAndIncrement() % 2;if (counter.get() > 9999) {counter.set(-1);}if (index == 0) {set(DBTypeEnum.SLAVE1);System.out.println("切换到slave1");}else {set(DBTypeEnum.SLAVE2);System.out.println("切换到slave2");}}}

获取路由key

package com.cjs.example.bean;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.lang.Nullable;public class MyRoutingDataSource extends AbstractRoutingDataSource {@Nullable@Overrideprotected Object determineCurrentLookupKey() {return DBContextHolder.get();}}

设置路由key

默认情况下,所有的查询都走从库,插入/修改/删除走主库。我们通过方法名来区分操作类型(CRUD)

package com.cjs.example.aop;import com.cjs.example.bean.DBContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class DataSourceAop {@Pointcut("!@annotation(com.cjs.example.annotation.Master) " +"&& (execution(* com.cjs.example.service..*.select*(..)) " +"|| execution(* com.cjs.example.service..*.get*(..)))")public void readPointcut() {}@Pointcut("@annotation(com.cjs.example.annotation.Master) " +"|| execution(* com.cjs.example.service..*.insert*(..)) " +"|| execution(* com.cjs.example.service..*.add*(..)) " +"|| execution(* com.cjs.example.service..*.update*(..)) " +"|| execution(* com.cjs.example.service..*.edit*(..)) " +"|| execution(* com.cjs.example.service..*.delete*(..)) " +"|| execution(* com.cjs.example.service..*.remove*(..))")public void writePointcut() {}@Before("readPointcut()")public void read() {DBContextHolder.slave();}@Before("writePointcut()")public void write() {DBContextHolder.master();}/*** 另一种写法:if...else...  判断哪些需要读从数据库,其余的走主数据库*/
//    @Before("execution(* com.cjs.example.service.impl.*.*(..))")
//    public void before(JoinPoint jp) {
//        String methodName = jp.getSignature().getName();
//
//        if (StringUtils.startsWithAny(methodName, "get", "select", "find")) {
//            DBContextHolder.slave();
//        }else {
//            DBContextHolder.master();
//        }
//    }
}

有一般情况就有特殊情况,特殊情况是某些情况下我们需要强制读主库,针对这种情况,我们定义一个主键,用该注解标注的就读主库

package com.cjs.example.annotation;public @interface Master {
}

例如,假设我们有一张表member

package com.cjs.example.service.impl;import com.cjs.example.annotation.Master;
import com.cjs.example.entity.Member;
import com.cjs.example.entity.MemberExample;
import com.cjs.example.mapper.MemberMapper;
import com.cjs.example.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
public class MemberServiceImpl implements MemberService {@Autowiredprivate MemberMapper memberMapper;@Transactional@Overridepublic int insert(Member member) {return memberMapper.insert(member);}@Master@Overridepublic int save(Member member) {return memberMapper.insert(member);}@Overridepublic List<Member> selectAll() {return memberMapper.selectByExample(new MemberExample());}@Master@Overridepublic String getToken(String appId) {//  有些读操作必须读主数据库//  比如,获取微信access_token,因为高峰时期主从同步可能延迟//  这种情况下就必须强制从主数据读return null;}
}

4. 测试

package com.cjs.example;import com.cjs.example.entity.Member;
import com.cjs.example.service.MemberService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class CjsDatasourceDemoApplicationTests {@Autowiredprivate MemberService memberService;@Testpublic void testWrite() {Member member = new Member();member.setName("zhangsan");memberService.insert(member);}@Testpublic void testRead() {for (int i = 0; i < 4; i++) {memberService.selectAll();}}@Testpublic void testSave() {Member member = new Member();member.setName("wangwu");memberService.save(member);}@Testpublic void testReadFromMaster() {memberService.getToken("1234");}}

查看控制台

img

img

5. 工程结构


热门内容:   

  

  • 阿里巴巴的技术专家,是如何画好架构图的?

  • 史上最烂的项目:苦撑 12 年,600 多万行代码

  • 一次 Jar 包升级引发的血案 & 解决

  • 如何优雅的导出 Excel

  • JDK 13 新特性一览

  • 某小公司RESTful、共用接口、前后端分离、接口约定的实践

  • 请停止学习框架

  • IntelliJ IDEA 2019.3这回真的要飞起来了,新特性抢先看!

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

SpringBoot + MyBatis + MySQL 读写分离实战相关推荐

  1. Springboot Mybatis MySQL读写分离及事物配置

    为什么需要读写分离 当项目越来越大和并发越来大的情况下,单个数据库服务器的压力肯定也是越来越大,最终演变成数据库成为性能的瓶颈,而且当数据越来越多时,查询也更加耗费时间,当然数据库数据过大时,可以采用 ...

  2. MySQL的主从配置+SpringBoot的MySQL读写分离配置

    MySQL的主从复制 点击前往查看MySQL的安装 1.主库操作 vim /etc/my.cnf 添加如下配置 log-bin=mysql-bin #[必须]启用二进制日志 server-id=128 ...

  3. mysql读写分离实战

    一个完整的MySQL读写分离环境包括以下几个部分: 应用程序client database proxy database集群 在本次实战中,应用程序client基于c3p0连接后端的database ...

  4. springboot+mybatis+mybatis +mysql读写分离(AOP方式)

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

  5. Spring Boot + MyBatis + MySQL读写分离

    今日推荐 借助Redis锁,完美解决高并发秒杀问题还在直接用JWT做鉴权?JJWT真香Spring Boot 操作 Redis 的各种实现Fluent Mybatis 牛逼!Nginx 常用配置清单这 ...

  6. boot lib分离 spring_SpringBoot+MyBatis+MySQL读写分离(实例)A

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

  7. mysql读写分离实战准备一

    一目的 首先准备一个web网站,这里模拟一个网站的页面,主要对开源网页实现动态存取,之后将使用本页面进一步实现对mysql的读写分离功能 二架构 前台是开源网页的页面,具体例子可从如下网站下载 htt ...

  8. mysql读写分离实例_SpringBoot+MyBatis+MySQL读写分离(实例)

    https://mp.weixin.qq.com/s/1vTbllmZkHRnuk7_xR2DIg 1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿 ...

  9. mysql读写分离的完整配置

    参考文章: 文章一[仅供参考]: 构建高性能web之路------mysql读写分离实战[按照里面配置主从mysql同步失败,并且按照他的my.cnf配置,给我的虚拟机搞坏了,重新弄了一个] http ...

最新文章

  1. SegmentFault 助力 Uber Hackathon
  2. echarts中toolbox位置_基于QGIS中的LSMS(大规模均值漂移)分割算法
  3. 驾驶卡丁车 模拟,迷宫(女赛)
  4. Linux之移动复制和删除
  5. uva 12222——Mountain Road
  6. 一文搞懂注册中心 zookeeper 和 eureka 中的CP和 AP
  7. 单片机3x3矩阵键盘c语言,C51单片机的3*3矩阵键盘程序
  8. MSET key value [key value ...]
  9. ElasticSearch: 使用Java Api 操作 ES
  10. GUI学习笔记——04更改背景板颜色,鼠标进入事件
  11. 基于vue前端聊天插件_基于Vue聊天的实现
  12. android#boardcast#广播实现强制下线功能
  13. [网页设计]如何在Photoshop里画虚线?
  14. win系统加入方舟服务器秒退,win10方舟生存进化进服务器闪退解决方法
  15. 传统的招投标or在线招投标
  16. 分析肖特基二极管的优势与结构应用
  17. JavaWeb和JavaScript的学习
  18. 吐血推荐!5款好用又骚气的神站!真的有毒!(附网址)
  19. 二叉树练习:找树根和hz
  20. javaScript正则表达式截取字符串【截取中间、截取前面、截取后面】

热门文章

  1. 从网站上扒网页,保存为file文件格式
  2. 透明代理Transparent Proxy
  3. mysql插入大量数据
  4. 一个几何不等式的最佳常数
  5. 有兴趣的执行一下这段代码
  6. 刻意练习:LeetCode实战 -- Task22. 二叉树的中序遍历
  7. 图像掩码操作的两种实现
  8. 【数据结构】顺序表的应用(2)(C语言)
  9. 25个好用到爆的一行 Python 代码,建议收藏
  10. 神经网络学习到的是什么?(Python)