歡迎您光臨本站 註冊首頁

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

←手機掃碼閱讀     wooen @ 2020-04-30 , reply:0

什麼事讀寫分離

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

為什麼要實現讀寫分離

增加冗餘

增加了機器的處理能力

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

實現

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

先建一個maven項目,導入springBoot依賴。



org.springframework.bootspring-boot-starter-parent1.5.2.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-aoporg.mybatis.spring.bootmybatis-spring-boot-starter1.3.1org.springframework.bootspring-boot-starter-jdbccom.alibabadruid${druid.version}mysqlmysql-connector-java${mysql-connector.version}org.springframework.bootspring-boot-starter-testtest



然後在配置文件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

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 ThreadLocallocal = new ThreadLocal<>(); public static ThreadLocalgetLocal() { 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); } }

其中第一個測試插入數據,第二個測試查詢。

第一測試結果:

第二個測結果:

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

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

測試:

@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,聲明一個事物。

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

配置事物

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

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



[wooen ] 使用springboot aop來實現讀寫分離和事物配置已經有272次圍觀

http://coctec.com/docs/java/show-post-232228.html