mybatis+mysql+driver_SpringBoot+MyBatis+MySQL读写分离

1.  引言

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

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

6a85baa85a53df465a878757e2165717.png

2.  AbstractRoutingDataSource

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

c8c1c2569b5752917881796dd728ec67.png

3.  实践

3.1.  maven依赖

4.0.0

com.cjs.example

cjs-datasource-demo

0.0.1-SNAPSHOT

jar

cjs-datasource-demo

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-aop

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

org.apache.commons

commons-lang3

3.8

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

3.2.  数据源配置

application.yml

spring:

datasource:

master:

jdbc-url: jdbc:mysql://192.168.102.31:3306/test

username: root

password: 123456

driver-class-name: com.mysql.jdbc.Driver

slave1:

jdbc-url: jdbc:mysql://192.168.102.56:3306/test

username: pig # 只读账户

password: 123456

driver-class-name: com.mysql.jdbc.Driver

slave2:

jdbc-url: jdbc:mysql://192.168.102.36:3306/test

username: pig # 只读账户

password: 123456

driver-class-name: com.mysql.jdbc.Driver

多数据源配置

packagecom.cjs.example.config;importcom.cjs.example.bean.MyRoutingDataSource;importcom.cjs.example.enums.DBTypeEnum;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.boot.jdbc.DataSourceBuilder;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;importjava.util.HashMap;importjava.util.Map;/*** 关于数据源配置,参考SpringBoot官方文档第79章《Data Access》

* 79. Data Access

* 79.1 Configure a Custom DataSource

* 79.2 Configure Two DataSources*/@Configurationpublic classDataSourceConfig {

@Bean

@ConfigurationProperties("spring.datasource.master")publicDataSource masterDataSource() {returnDataSourceBuilder.create().build();

}

@Bean

@ConfigurationProperties("spring.datasource.slave1")publicDataSource slave1DataSource() {returnDataSourceBuilder.create().build();

}

@Bean

@ConfigurationProperties("spring.datasource.slave2")publicDataSource slave2DataSource() {returnDataSourceBuilder.create().build();

}

@Beanpublic DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,

@Qualifier("slave1DataSource") DataSource slave1DataSource,

@Qualifier("slave2DataSource") DataSource slave2DataSource) {

Map targetDataSources = new HashMap<>();

targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);

targetDataSources.put(DBTypeEnum.SLAVE1, slave1DataSource);

targetDataSources.put(DBTypeEnum.SLAVE2, slave2DataSource);

MyRoutingDataSource myRoutingDataSource= newMyRoutingDataSource();

myRoutingDataSource.setDefaultTargetDataSource(masterDataSource);

myRoutingDataSource.setTargetDataSources(targetDataSources);returnmyRoutingDataSource;

}

}

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

MyBatis配置

packagecom.cjs.example.config;importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.jdbc.datasource.DataSourceTransactionManager;importorg.springframework.transaction.PlatformTransactionManager;importorg.springframework.transaction.annotation.EnableTransactionManagement;importjavax.annotation.Resource;importjavax.sql.DataSource;

@EnableTransactionManagement

@Configurationpublic classMyBatisConfig {

@Resource(name= "myRoutingDataSource")privateDataSource myRoutingDataSource;

@Beanpublic SqlSessionFactory sqlSessionFactory() throwsException {

SqlSessionFactoryBean sqlSessionFactoryBean= newSqlSessionFactoryBean();

sqlSessionFactoryBean.setDataSource(myRoutingDataSource);

sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));returnsqlSessionFactoryBean.getObject();

}

@BeanpublicPlatformTransactionManager platformTransactionManager() {return newDataSourceTransactionManager(myRoutingDataSource);

}

}

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

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

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

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

packagecom.cjs.example.enums;public enumDBTypeEnum {

MASTER, SLAVE1, SLAVE2;

}

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

packagecom.cjs.example.bean;importcom.cjs.example.enums.DBTypeEnum;importjava.util.concurrent.atomic.AtomicInteger;public classDBContextHolder {private static final ThreadLocal contextHolder = new ThreadLocal<>();private static final AtomicInteger counter = new AtomicInteger(-1);public static voidset(DBTypeEnum dbType) {

contextHolder.set(dbType);

}public staticDBTypeEnum get() {returncontextHolder.get();

}public static voidmaster() {

set(DBTypeEnum.MASTER);

System.out.println("切换到master");

}public static voidslave() {//轮询

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

packagecom.cjs.example.bean;importorg.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;importorg.springframework.lang.Nullable;public class MyRoutingDataSource extendsAbstractRoutingDataSource {

@Nullable

@OverrideprotectedObject determineCurrentLookupKey() {returnDBContextHolder.get();

}

}

设置路由key

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

packagecom.cjs.example.aop;importcom.cjs.example.bean.DBContextHolder;importorg.apache.commons.lang3.StringUtils;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;

@Aspect

@Componentpublic classDataSourceAop {

@Pointcut("!@annotation(com.cjs.example.annotation.Master) " +

"&& (execution(* com.cjs.example.service..*.select*(..)) " +

"|| execution(* com.cjs.example.service..*.get*(..)))")public voidreadPointcut() {

}

@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 voidwritePointcut() {

}

@Before("readPointcut()")public voidread() {

DBContextHolder.slave();

}

@Before("writePointcut()")public voidwrite() {

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();//}//}

}

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

packagecom.cjs.example.annotation;public @interfaceMaster {

}

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

packagecom.cjs.example.service.impl;importcom.cjs.example.annotation.Master;importcom.cjs.example.entity.Member;importcom.cjs.example.entity.MemberExample;importcom.cjs.example.mapper.MemberMapper;importcom.cjs.example.service.MemberService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;

@Servicepublic class MemberServiceImpl implementsMemberService {

@AutowiredprivateMemberMapper memberMapper;

@Transactional

@Overridepublic intinsert(Member member) {returnmemberMapper.insert(member);

}

@Master

@Overridepublic intsave(Member member) {returnmemberMapper.insert(member);

}

@Overridepublic ListselectAll() {return memberMapper.selectByExample(newMemberExample());

}

@Master

@OverridepublicString getToken(String appId) {//有些读操作必须读主数据库//比如,获取微信access_token,因为高峰时期主从同步可能延迟//这种情况下就必须强制从主数据读

return null;

}

}

4.  测试

packagecom.cjs.example;importcom.cjs.example.entity.Member;importcom.cjs.example.service.MemberService;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTestpublic classCjsDatasourceDemoApplicationTests {

@AutowiredprivateMemberService memberService;

@Testpublic voidtestWrite() {

Member member= newMember();

member.setName("zhangsan");

memberService.insert(member);

}

@Testpublic voidtestRead() {for (int i = 0; i < 4; i++) {

memberService.selectAll();

}

}

@Testpublic voidtestSave() {

Member member= newMember();

member.setName("wangwu");

memberService.save(member);

}

@Testpublic voidtestReadFromMaster() {

memberService.getToken("1234");

}

}

查看控制台

d12f87b7363430cce800f8ec7d509c3a.png

e5608d7469c84b59e9907f36cc27c907.png

05b6436d667379a8635f981ad5ef5e96.png

91cffae921bc3f6fc4d64049f153f009.png

5.  工程结构

6aeefc8d4c8daf7a1e330a8b13583782.png

6.  参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值