1. 程式人生 > 程式設計 >使用springboot aop來實現讀寫分離和事物配置

使用springboot aop來實現讀寫分離和事物配置

什麼事讀寫分離

讀寫分離,基本的原理是讓主資料庫處理事務性增、改、刪操作(INSERT、UPDATE、DELETE),而從資料庫處理SELECT查詢操作。資料庫複製被用來把事務性操作導致的變更同步到叢集中的從資料庫。

為什麼要實現讀寫分離

增加冗餘

增加了機器的處理能力

對於讀操作為主的應用,使用讀寫分離是最好的場景,因為可以確保寫的伺服器壓力更小,而讀又可以接受點時間上的延遲。

實現

本文介紹利用spring aop來動態切換資料來源來實現讀寫分離。

先建一個maven專案,匯入springBoot依賴。

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>
 <!--mybatis-->
 <dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.1</version>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
 <!-- druid -->
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>${druid.version}</version>
 </dependency>
 <!-- mysql connector-->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql-connector.version}</version>
 </dependency>

 <!--test-->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>

</dependencies>

然後在配置檔案application.yml中自定義資料來源配置項

server:
 port: 8080
logging:
 level:
 org.springframework: INFO
 com.qiang: DEBUG
spring:
 output:
 ansi:
  enabled: always
 datasource:
 type: com.alibaba.druid.pool.DruidDataSource
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://localhost:3306/db_area?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
 username: root
 password: root
 db:
 readsize: 2
 read0:
  type: com.alibaba.druid.pool.DruidDataSource
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/db_area?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  username: root
  password: root

 read1:
  type: com.alibaba.druid.pool.DruidDataSource
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/db_area?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  username: root
  password: root
 aop:
 auto: true
 proxy-target-class: true

配置Druid

package com.qiang.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author gengqiang
 * @date 2018/5/3
 */
@Configuration
public class DruidConfig {

 private Logger logger = LoggerFactory.getLogger(DruidConfig.class

 /**
  * 主據源
  * @return
  */
 @Primary
 @Bean(name = "dataSource")
 @ConfigurationProperties(prefix = "spring.datasource")
 public DataSource dataSource() {
  return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();

 }

 /**
  * 從資料來源1
  * @return
  */
 @Bean(name = "readDataSource0")
 @ConfigurationProperties(prefix = "spring.db.read0")
 public DataSource readDataSource0() {
  return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();

 }
 /**
  * 從資料來源2
  * @return
  */
 @Bean(name = "readDataSource1")
 @ConfigurationProperties(prefix = "spring.db.read1")
 public DataSource readDataSource1() {
  return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();

 }

} 

配置Mybaits

package com.qiang.config;

import com.qiang.config.db.DataSourceType;
import com.qiang.config.db.RoutingDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gengqiang
 * @date 2018/5/3
 */
@Configuration
@EnableTransactionManagement(order = 2)
@MapperScan(basePackages = {"com.qiang.demo.mapper"})
public class MybatisConfig implements TransactionManagementConfigurer,ApplicationContextAware {


 private static ApplicationContext context;


 /**
  * 寫庫資料來源
  */
 @Autowired
 private DataSource dataSource;

 /**
  * 讀資料來源數量
  */
 @Value("${spring.db.readsize}")
 private Integer readsize;
 /**
  * 資料來源路由代理
  *
  * @return
  */

 @Bean
 public AbstractRoutingDataSource routingDataSouceProxy() {
  RoutingDataSource proxy = new RoutingDataSource(readsize);
  Map<Object,Object> targetDataSources = new HashMap<>(readsize + 1);
  targetDataSources.put(DataSourceType.WRITE.getType(),dataSource);
  for (int i = 0; i < readsize; i++) {
   DataSource d = context.getBean("readDataSource" + i,DataSource.class);
   targetDataSources.put(i,d);
  }
  proxy.setDefaultTargetDataSource(dataSource);
  proxy.setTargetDataSources(targetDataSources);
  return proxy;
 }

 @Bean
 @ConditionalOnMissingBean
 public SqlSessionFactoryBean sqlSessionFactory() throws IOException {
  SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  bean.setDataSource(routingDataSouceProxy());
  bean.setVfs(SpringBootVFS.class);
  bean.setTypeAliasesPackage("com.qiang");
  Resource configResource = new ClassPathResource("/mybatis-config.xml");
  bean.setConfigLocation(configResource);
  ResourcePatternResolver mapperResource = new PathMatchingResourcePatternResolver();
  bean.setMapperLocations(mapperResource.getResources("classpath*:mapper/**/*.xml"));
  return bean;
 }

 @Override
 public PlatformTransactionManager annotationDrivenTransactionManager() {
  return new DataSourceTransactionManager(routingDataSouceProxy());
 }

 @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  if (context == null) {
   context = applicationContext;
  }
 }
}

其中實現資料來源切換的功能就是自定義一個類擴充套件AbstractRoutingDataSource抽象類,就是程式碼中的定義的RoutingDataSource,其實該相當於資料來源DataSourcer的路由中介,可以實現在專案執行時根據相應key值切換到對應的資料來源DataSource上。

RoutingDataSource.class

package com.qiang.config.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 資料來源路由
 *
 * @author gengqiang
 */
public class RoutingDataSource extends AbstractRoutingDataSource {

 private AtomicInteger count = new AtomicInteger(0);

 private int readsize;

 public RoutingDataSource(int readsize) {
  this.readsize = readsize;
 }

 @Override
 protected Object determineCurrentLookupKey() {
  String typeKey = DataSourceContextHolder.getJdbcType();
  if (typeKey == null) {
   logger.error("無法確定資料來源");
  }
  if (typeKey.equals(DataSourceType.WRITE.getType())) {
   return DataSourceType.WRITE.getType();
  }
  //讀庫進行負載均衡
  int a = count.getAndAdd(1);
  int lookupkey = a % readsize;
  return lookupkey;
 }

}

其中用到了2個輔助類

package com.qiang.config.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 全域性資料來源
 *
 * @author gengqiang
 */
public class DataSourceContextHolder {

 private static Logger logger = LoggerFactory.getLogger(DataSourceContextHolder.class);

 private final static ThreadLocal<String> local = new ThreadLocal<>();

 public static ThreadLocal<String> getLocal() {
  return local;
 }

 public static void read() {
  logger.debug("切換至[讀]資料來源");
  local.set(DataSourceType.READ.getType());
 }

 public static void write() {
  logger.debug("切換至[寫]資料來源");
  local.set(DataSourceType.WRITE.getType());
 }

 public static String getJdbcType() {
  return local.get();
 }

}
package com.qiang.config.db;

/**
 * @author gengqiang
 */
public enum DataSourceType {
 READ("read","讀庫"),WRITE("write","寫庫");
 private String type;
 private String name;

 DataSourceType(String type,String name) {
  this.type = type;
  this.name = name;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

最後通過aop設定切面,攔截讀寫來動態的設定資料來源

package com.qiang.config.aop;

import com.qiang.config.db.DataSourceContextHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 攔截資料庫讀寫
 *
 * @author gengqiang
 */

@Aspect
@Component
@Order(1)
public class DataSourceAspect {

 Logger logger = LoggerFactory.getLogger(getClass());

 @Before("execution(* com.qiang..*.*ServiceImpl.find*(..)) " +
   "|| execution(* com.qiang..*.*ServiceImpl.count*(..))" +
   "|| execution(* com.qiang..*.*ServiceImpl.sel*(..))" +
   "|| execution(* com.qiang..*.*ServiceImpl.get*(..))"
 )
 public void setReadDataSourceType() {
  logger.debug("攔截[read]方法");
  DataSourceContextHolder.read();
 }

 @Before("execution(* com.qiang..*.*ServiceImpl.insert*(..)) " +
   "|| execution(* com.qiang..*.*ServiceImpl.save*(..))" +
   "|| execution(* com.qiang..*.*ServiceImpl.update*(..))" +
   "|| execution(* com.qiang..*.*ServiceImpl.set*(..))" +
   "|| execution(* com.qiang..*.*ServiceImpl.del*(..))")
 public void setWriteDataSourceType() {
  logger.debug("攔截[write]操作");
  DataSourceContextHolder.write();
 }

}

主要的程式碼就寫好了,下面來測試一下是否讀寫分離。

寫一個測試類:

package com.qiang;

import com.qiang.demo.entity.Area;
import com.qiang.demo.service.AreaService;
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;

/**
 * @author gengqiang
 * @date 2018/5/4
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplication {

 @Autowired
 private AreaService areaService;


 @Test
 public void test() {
  Area area = new Area();
  area.setDistrictId("0000");
  area.setName("test");
  area.setParentId(0);
  area.setLevel(1);
  areaService.insert(area);
 }

 @Test
 public void test2() {
  areaService.selectByPrimaryKey(1);
 }
}

其中第一個測試插入資料,第二個測試查詢。

第一測試結果:

使用springboot aop來實現讀寫分離和事物配置

第二個測結果:

使用springboot aop來實現讀寫分離和事物配置

從結果看出來第一個走的寫資料來源,就是主資料來源,第二個的走讀資料來源,就是從資料來源。

然後我們在測試一下事物,看遇到異常是否會滾。

測試:

 @Test
 public void contextLoads() throws Exception {
  try {
   areaService.insertBack();

  } catch (Exception e) {
//   e.printStackTrace();
  }
  System.out.println(areaService.count(new Area()));

 }

其中service:

@Override
@Transactional(rollbackFor = Exception.class)
public void insertBack() {
 Area area = new Area();
 area.setDistrictId("0000");
 area.setName("test");
 area.setParentId(0);
 area.setLevel(1);
 mapper.insert(area);
 throw new RuntimeException();
}

方法上加@Transactional,宣告一個事物。

使用springboot aop來實現讀寫分離和事物配置

看一下執行結果,雖然執行插入的時候,sql是運行了,但最後查詢的時候數量為0,說明會滾了。

配置事物

使用springboot aop來實現讀寫分離和事物配置

第一步需要加一個註解@EnableTransactionManagement,後面的引數是為了區分aop和事物執行的順序。

然後在需要會滾的方法上加一個註解@Transactional。

原始碼地址

以上這篇使用springboot aop來實現讀寫分離和事物配置就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。