Spring系列第52篇:Spring实现数据库读写分离

star2017 1年前 ⋅ 291 阅读

1、背景

大多数系统都是读多写少,为了降低数据库的压力,可以对主库创建多个从库,从库自动从主库同步数据,程序中将写的操作发送到主库,将读的操作发送到从库去执行。

今天的主要目标:通过spring实现读写分离

读写分离需实现下面2个功能:

1、读的方法,由调用者来控制具体是读从库还是主库

2、有事务的方法,内部的所有读写操作都走主库

2、思考3个问题

1、读的方法,由调用者来控制具体是读从库还是主库,如何实现?

可以给所有读的方法添加一个参数,来控制读从库还是主库。

2、数据源如何路由?

spring-jdbc 包中提供了一个抽象类:AbstractRoutingDataSource,实现了javax.sql.DataSource接口,我们用这个类来作为数据源类,重点是这个类可以用来做数据源的路由,可以在其内部配置多个真实的数据源,最终用哪个数据源,由开发者来决定。

AbstractRoutingDataSource中有个map,用来存储多个目标数据源

  1. private Map<Object, DataSource> resolvedDataSources;

比如主从库可以这么存储

  1. resolvedDataSources.put("master",主库数据源);
  2. resolvedDataSources.put("salave",从库数据源);

AbstractRoutingDataSource中还有抽象方法determineCurrentLookupKey,将这个方法的返回值作为key到上面的resolvedDataSources中查找对应的数据源,作为当前操作db的数据源

  1. protected abstract Object determineCurrentLookupKey();

3、读写分离在哪控制?

读写分离属于一个通用的功能,可以通过spring的aop来实现,添加一个拦截器,拦截目标方法的之前,在目标方法执行之前,获取一下当前需要走哪个库,将这个标志存储在ThreadLocal中,将这个标志作为AbstractRoutingDataSource.determineCurrentLookupKey()方法的返回值,拦截器中在目标方法执行完毕之后,将这个标志从ThreadLocal中清除。

3、代码实现

3.1、工程结构图

3.2、DsType

表示数据源类型,有2个值,用来区分是主库还是从库。

  1. package com.javacode2018.readwritesplit.base;
  2. public enum DsType {
  3. MASTER, SLAVE;
  4. }

3.3、DsTypeHolder

内部有个ThreadLocal,用来记录当前走主库还是从库,将这个标志放在dsTypeThreadLocal中

  1. package com.javacode2018.readwritesplit.base;
  2. public class DsTypeHolder {
  3. private static ThreadLocal<DsType> dsTypeThreadLocal = new ThreadLocal<>();
  4. public static void master() {
  5. dsTypeThreadLocal.set(DsType.MASTER);
  6. }
  7. public static void slave() {
  8. dsTypeThreadLocal.set(DsType.SLAVE);
  9. }
  10. public static DsType getDsType() {
  11. return dsTypeThreadLocal.get();
  12. }
  13. public static void clearDsType() {
  14. dsTypeThreadLocal.remove();
  15. }
  16. }

3.4、IService接口

这个接口起到标志的作用,当某个类需要启用读写分离的时候,需要实现这个接口,实现这个接口的类都会被读写分离拦截器拦截。

  1. package com.javacode2018.readwritesplit.base;
  2. //需要实现读写分离的service需要实现该接口
  3. public interface IService {
  4. }

3.5、ReadWriteDataSource

读写分离数据源,继承ReadWriteDataSource,注意其内部的determineCurrentLookupKey方法,从上面的ThreadLocal中获取当前需要走主库还是从库的标志。

  1. package com.javacode2018.readwritesplit.base;
  2. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  3. import org.springframework.lang.Nullable;
  4. public class ReadWriteDataSource extends AbstractRoutingDataSource {
  5. @Nullable
  6. @Override
  7. protected Object determineCurrentLookupKey() {
  8. return DsTypeHolder.getDsType();
  9. }
  10. }

3.6、ReadWriteInterceptor

读写分离拦截器,需放在事务拦截器前面执行,通过@1代码我们将此拦截器的顺序设置为Integer.MAX_VALUE - 2,稍后我们将事务拦截器的顺序设置为Integer.MAX_VALUE - 1,事务拦截器的执行顺序是从小到达的,所以,ReadWriteInterceptor会在事务拦截器org.springframework.transaction.interceptor.TransactionInterceptor之前执行。

由于业务方法中存在相互调用的情况,比如service1.m1中调用service2.m2,而service2.m2中调用了service2.m3,我们只需要在m1方法执行之前,获取具体要用哪个数据源就可以了,所以下面代码中会在第一次进入这个拦截器的时候,记录一下走主库还是从库。

下面方法中会获取当前目标方法的最后一个参数,最后一个参数可以是DsType类型的,开发者可以通过这个参数来控制具体走主库还是从库。

  1. package com.javacode2018.readwritesplit.base;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.springframework.core.annotation.Order;
  7. import org.springframework.stereotype.Component;
  8. import java.util.Objects;
  9. @Aspect
  10. @Order(Integer.MAX_VALUE - 2) //@1
  11. @Component
  12. public class ReadWriteInterceptor {
  13. @Pointcut("target(IService)")
  14. public void pointcut() {
  15. }
  16. //获取当前目标方法的最后一个参数
  17. private Object getLastArgs(final ProceedingJoinPoint pjp) {
  18. Object[] args = pjp.getArgs();
  19. if (Objects.nonNull(args) && args.length > 0) {
  20. return args[args.length - 1];
  21. } else {
  22. return null;
  23. }
  24. }
  25. @Around("pointcut()")
  26. public Object around(final ProceedingJoinPoint pjp) throws Throwable {
  27. //判断是否是第一次进来,用于处理事务嵌套
  28. boolean isFirst = false;
  29. try {
  30. if (DsTypeHolder.getDsType() == null) {
  31. isFirst = true;
  32. }
  33. if (isFirst) {
  34. Object lastArgs = getLastArgs(pjp);
  35. if (DsType.SLAVE.equals(lastArgs)) {
  36. DsTypeHolder.slave();
  37. } else {
  38. DsTypeHolder.master();
  39. }
  40. }
  41. return pjp.proceed();
  42. } finally {
  43. //退出的时候,清理
  44. if (isFirst) {
  45. DsTypeHolder.clearDsType();
  46. }
  47. }
  48. }
  49. }

3.7、ReadWriteConfiguration

spring配置类,作用

1、@3:用来将com.javacode2018.readwritesplit.base包中的一些类注册到spring容器中,比如上面的拦截器ReadWriteInterceptor

2、@1:开启spring aop的功能

3、@2:开启spring自动管理事务的功能,@EnableTransactionManagement的order用来指定事务拦截器org.springframework.transaction.interceptor.TransactionInterceptor顺序,在这里我们将order设置为Integer.MAX_VALUE - 1,而上面ReadWriteInterceptor的order是Integer.MAX_VALUE - 2,所以ReadWriteInterceptor会在事务拦截器之前执行。

  1. package com.javacode2018.readwritesplit.base;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  5. import org.springframework.transaction.annotation.EnableTransactionManagement;
  6. @Configuration
  7. @EnableAspectJAutoProxy //@1
  8. @EnableTransactionManagement(proxyTargetClass = true, order = Integer.MAX_VALUE - 1) //@2
  9. @ComponentScan(basePackageClasses = IService.class) //@3
  10. public class ReadWriteConfiguration {
  11. }

3.8、@EnableReadWrite

这个注解用来开启读写分离的功能,@1通过@Import将ReadWriteConfiguration导入到spring容器了,这样就会自动启用读写分离的功能。业务中需要使用读写分离,只需要在spring配置类中加上@EnableReadWrite注解就可以了。

  1. package com.javacode2018.readwritesplit.base;
  2. import org.springframework.context.annotation.Import;
  3. import java.lang.annotation.*;
  4. @Target(ElementType.TYPE)
  5. @Retention(RetentionPolicy.RUNTIME)
  6. @Documented
  7. @Import(ReadWriteConfiguration.class) //@1
  8. public @interface EnableReadWrite {
  9. }

4、案例

读写分离的关键代码写完了,下面我们来上案例验证一下效果。

4.1、执行sql脚本

下面准备2个数据库:javacode2018_master(主库)、javacode2018_slave(从库)

2个库中都创建一个t_user表,分别插入了一条数据,稍后用这个数据来验证走的是主库还是从库。

  1. DROP DATABASE IF EXISTS javacode2018_master;
  2. CREATE DATABASE IF NOT EXISTS javacode2018_master;
  3. USE javacode2018_master;
  4. DROP TABLE IF EXISTS t_user;
  5. CREATE TABLE t_user (
  6. id INT PRIMARY KEY AUTO_INCREMENT,
  7. name VARCHAR(256) NOT NULL DEFAULT ''
  8. COMMENT '姓名'
  9. );
  10. INSERT INTO t_user (name) VALUE ('master库');
  11. DROP DATABASE IF EXISTS javacode2018_slave;
  12. CREATE DATABASE IF NOT EXISTS javacode2018_slave;
  13. USE javacode2018_slave;
  14. DROP TABLE IF EXISTS t_user;
  15. CREATE TABLE t_user (
  16. id INT PRIMARY KEY AUTO_INCREMENT,
  17. name VARCHAR(256) NOT NULL DEFAULT ''
  18. COMMENT '姓名'
  19. );
  20. INSERT INTO t_user (name) VALUE ('slave库');

4.2、spring配置类

@1:启用读写分离

masterDs()方法:定义主库数据源

slaveDs()方法:定义从库数据源

dataSource():定义读写分离路由数据源

后面还有2个方法用来定义JdbcTemplate和事务管理器,方法中都通过@Qualifier(“dataSource”)限定了注入的bean名称为dataSource:即注入了上面dataSource()返回的读写分离路由数据源。

  1. package com.javacode2018.readwritesplit.demo1;
  2. import com.javacode2018.readwritesplit.base.DsType;
  3. import com.javacode2018.readwritesplit.base.EnableReadWrite;
  4. import com.javacode2018.readwritesplit.base.ReadWriteDataSource;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  11. import org.springframework.transaction.PlatformTransactionManager;
  12. import javax.sql.DataSource;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @EnableReadWrite //@1
  16. @Configuration
  17. @ComponentScan
  18. public class MainConfig {
  19. //主库数据源
  20. @Bean
  21. public DataSource masterDs() {
  22. org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
  23. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  24. dataSource.setUrl("jdbc:mysql://localhost:3306/javacode2018_master?characterEncoding=UTF-8");
  25. dataSource.setUsername("root");
  26. dataSource.setPassword("root123");
  27. dataSource.setInitialSize(5);
  28. return dataSource;
  29. }
  30. //从库数据源
  31. @Bean
  32. public DataSource slaveDs() {
  33. org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
  34. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  35. dataSource.setUrl("jdbc:mysql://localhost:3306/javacode2018_slave?characterEncoding=UTF-8");
  36. dataSource.setUsername("root");
  37. dataSource.setPassword("root123");
  38. dataSource.setInitialSize(5);
  39. return dataSource;
  40. }
  41. //读写分离路由数据源
  42. @Bean
  43. public ReadWriteDataSource dataSource() {
  44. ReadWriteDataSource dataSource = new ReadWriteDataSource();
  45. //设置主库为默认的库,当路由的时候没有在datasource那个map中找到对应的数据源的时候,会使用这个默认的数据源
  46. dataSource.setDefaultTargetDataSource(this.masterDs());
  47. //设置多个目标库
  48. Map<Object, Object> targetDataSources = new HashMap<>();
  49. targetDataSources.put(DsType.MASTER, this.masterDs());
  50. targetDataSources.put(DsType.SLAVE, this.slaveDs());
  51. dataSource.setTargetDataSources(targetDataSources);
  52. return dataSource;
  53. }
  54. //JdbcTemplate,dataSource为上面定义的注入读写分离的数据源
  55. @Bean
  56. public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
  57. return new JdbcTemplate(dataSource);
  58. }
  59. //定义事务管理器,dataSource为上面定义的注入读写分离的数据源
  60. @Bean
  61. public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
  62. return new DataSourceTransactionManager(dataSource);
  63. }
  64. }

4.3、UserService

这个类就相当于我们平时写的service,我是为了方法,直接在里面使用了JdbcTemplate来操作数据库,真实的项目操作db会放在dao里面。

getUserNameById方法:通过id查询name。

insert方法:插入数据,这个内部的所有操作都会走主库,为了验证是不是查询也会走主库,插入数据之后,我们会调用this.userService.getUserNameById(id, DsType.SLAVE)方法去执行查询操作,第二个参数故意使用SLAVE,如果查询有结果,说明走的是主库,否则走的是从库,这里为什么需要通过this.userService来调用getUserNameById?

this.userService最终是个代理对象,通过代理对象访问其内部的方法,才会被读写分离的拦截器拦截。

  1. package com.javacode2018.readwritesplit.demo1;
  2. import com.javacode2018.readwritesplit.base.DsType;
  3. import com.javacode2018.readwritesplit.base.IService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.transaction.annotation.Propagation;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import java.util.List;
  10. @Component
  11. public class UserService implements IService {
  12. @Autowired
  13. private JdbcTemplate jdbcTemplate;
  14. @Autowired
  15. private UserService userService;
  16. @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
  17. public String getUserNameById(long id, DsType dsType) {
  18. String sql = "select name from t_user where id=?";
  19. List<String> list = this.jdbcTemplate.queryForList(sql, String.class, id);
  20. return (list != null && list.size() > 0) ? list.get(0) : null;
  21. }
  22. //这个insert方法会走主库,内部的所有操作都会走主库
  23. @Transactional
  24. public void insert(long id, String name) {
  25. System.out.println(String.format("插入数据{id:%s, name:%s}", id, name));
  26. this.jdbcTemplate.update("insert into t_user (id,name) values (?,?)", id, name);
  27. String userName = this.userService.getUserNameById(id, DsType.SLAVE);
  28. System.out.println("查询结果:" + userName);
  29. }
  30. }

4.4、测试用例

  1. package com.javacode2018.readwritesplit.demo1;
  2. import com.javacode2018.readwritesplit.base.DsType;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. public class Demo1Test {
  7. UserService userService;
  8. @Before
  9. public void before() {
  10. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  11. context.register(MainConfig.class);
  12. context.refresh();
  13. this.userService = context.getBean(UserService.class);
  14. }
  15. @Test
  16. public void test1() {
  17. System.out.println(this.userService.getUserNameById(1, DsType.MASTER));
  18. System.out.println(this.userService.getUserNameById(1, DsType.SLAVE));
  19. }
  20. @Test
  21. public void test2() {
  22. long id = System.currentTimeMillis();
  23. System.out.println(id);
  24. this.userService.insert(id, "张三");
  25. }
  26. }

test1方法执行2次查询,分别查询主库和从库,输出:

  1. master
  2. slave

是不是很爽,由开发者自己控制具体走主库还是从库。

test2执行结果如下,可以看出查询到了刚刚插入的数据,说明insert中所有操作都走的是主库。

  1. 1604905117467
  2. 插入数据{id:1604905117467, name:张三}
  3. 查询结果:张三

5、案例源码

  1. git地址:
  2. https://gitee.com/javacode2018/spring-series
  3. 本文案例对应源码:
  4. spring-series\lesson-004-readwritesplit

本博客所有系列案例代码以后都会放到这个上面,大家watch一下,可以持续关注动态。

最新资料

更多内容请访问:IT源点

全部评论: 0

    我有话说: