读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做。因此,一般来讲,读写分离有两种实现方式。第一种是依靠中间件(比如:MyCat),也就是说应用程序连接到中间件,中间件帮我们做SQL分离;第二种是应用程序自己去做分离。这里我们选择程序自己来做,主要是利用Spring提供的路由数据源,以及AOP。
然而,应用程序层面去做读写分离最大的弱点(不足之处)在于无法动态增加数据库节点,因为数据源配置都是写在配置中的,新增数据库意味着新加一个数据源,必然改配置,并重启应用。当然,好处就是相对简单。
基于特定的key,路由到特定的数据源。它内部维护了一组目标数据源,并且做了路由key与目标数据源之间的映射,提供基于key查找数据源的方法。
环境
把一台虚拟机克隆成两份,ip分别配置为:
克隆步骤参考:CentOS7虚拟机克隆
主从复制配置参考:MySQL系列之主从复制配置【十二】
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.linyf</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>springboot+mysql+mybatis实现读写分离实战</description>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</plugins>
</build>
</project>
spring:
datasource:
master:
type: com.zaxxer.hikari.HikariDataSource
jdbc-url: jdbc:mysql://192.168.67.1:3306/test?characterEncoding=utf-8
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
slave1:
type: com.zaxxer.hikari.HikariDataSource
jdbc-url: jdbc:mysql://192.168.67.11:3306/test?characterEncoding=utf-8
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
slave2:
type: com.zaxxer.hikari.HikariDataSource
jdbc-url: jdbc:mysql://192.168.67.21:3306/test?characterEncoding=utf-8
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
package com.linyf.demo.config;
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();
}
@Bean
public 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个数据源,后续只用路由数据源。
package com.linyf.demo.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;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(myRoutingDataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sqlSessionFactory.getObject();
}
@Bean
public PlatformTransactionManager platformTransactionManager() {
return new DataSourceTransactionManager(myRoutingDataSource);
}
}
由于Spring容器中现在有4个数据源,所以需要为事务管理器和MyBatis手动指定一个明确的数据源。
目标数据源就是前3个,但是使用的时候如果查找数据源?
首先,定义一个枚举来代表这三个数据源:
package com.linyf.demo.config;
public enum DBTypeEnum {
MASTER, SLAVE1, SLAVE2
}
接下来,通过ThreadLocal将数据源设置到每个线程上下文中:
package com.linyf.demo.config;
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.linyf.demo.config;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.lang.Nullable;
public class MyRoutingDataSource extends AbstractRoutingDataSource {
@Nullable
@Override
protected Object determineCurrentLookupKey() {
return DBContextHolder.get();
}
}
设置路由key
默认情况下,所有的查询都走从库,插入/修改/删除走主库。通过方法名来区分操作类型(CRUD):
package com.linyf.demo.config;
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.linyf.demo.anno.Master) " +
"&& (execution(* com.linyf.demo.service..*.select*(..)) " +
"|| execution(* com.linyf.demo.service..*.get*(..)))")
public void readPointcut() {
}
@Pointcut("@annotation(com.linyf.demo.anno.Master) " +
"|| execution(* com.linyf.demo.service..*.insert*(..)) " +
"|| execution(* com.linyf.demo.service..*.add*(..)) " +
"|| execution(* com.linyf.demo..*.update*(..)) " +
"|| execution(* com.linyf.demo.service..*.edit*(..)) " +
"|| execution(* com.linyf.demo.service..*.delete*(..)) " +
"|| execution(* com.linyf.demo..*.remove*(..))")
public void writePointcut() {
}
@Before("readPointcut()")
public void read() {
DBContextHolder.slave();
}
@Before("writePointcut()")
public void write() {
DBContextHolder.master();
}
/**
* 另一种写法:if...else... 判断哪些需要读从数据库,其余的走主数据库
*/
// @Before("execution(* com.linyf.demo.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.linyf.demo.anno;
public @interface Master {
}
在主库,新建名为test的数据库,然后新建一个user表:
然后新建User实体类:
package com.linyf.demo.entity;
import lombok.Data;
@Data
public class User {
/**
* 主键id
*/
private int id;
/**
* 名称
*/
private String name;
/**
* 年龄
*/
private int age;
}
UserService接口:
package com.linyf.demo.service;
import com.linyf.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> selectList();
Boolean insert(User user);
}
UserServiceImpl实现类:
package com.linyf.demo.service.impl;
import com.linyf.demo.dao.UserMapper;
import com.linyf.demo.entity.User;
import com.linyf.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> selectList() {
return userMapper.selectList();
}
@Override
public Boolean insert(User user) {
return userMapper.insert(user);
}
}
UserMapper接口:
package com.linyf.demo.dao;
import com.linyf.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List<User> selectList();
Boolean insert(User user);
}
UserMapper.xml:
<mapper namespace="com.linyf.demo.dao.UserMapper">
<insert id="insert" parameterType="com.linyf.demo.entity.User" >
insert into user (id,`name`,age) values (#{id}, #{name}, #{age})
insert>
<select id="selectList" resultType="com.linyf.demo.entity.User">
select * from user
select>
mapper>
UserController:
package com.linyf.demo.Controller;
import com.linyf.demo.entity.User;
import com.linyf.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/select")
public List<User> select(){
return userService.selectList();
}
@GetMapping("/insert")
public Boolean insert(User user){
return userService.insert(user);
}
}
启动项目,浏览器请求:
http://localhost:8080/insert?id=1&name=zhangsan&age=18
http://localhost:8080/insert?id=2&name=lisi&age=20
http://localhost:8080/select
http://localhost:8080/select
http://localhost:8080/select