mirror of
https://github.com/yin5980280/easy.git
synced 2026-09-03 07:26:58 +08:00
【A】新增读写分离starter
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
|
||||
<spring-boot-admin.version>2.3</spring-boot-admin.version>
|
||||
<spring-boot-admin.version>2.3.0</spring-boot-admin.version>
|
||||
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
|
||||
<druid-spring-boot-starter-version>1.1.24</druid-spring-boot-starter-version>
|
||||
<redisson-spring-boot-starter-version>3.13.5</redisson-spring-boot-starter-version>
|
||||
@@ -31,7 +31,7 @@
|
||||
<swagger.version>3.0.0</swagger.version>
|
||||
<swagger.bootstrap.ui.version>2.0.5</swagger.bootstrap.ui.version>
|
||||
<kaptcha.version>2.3.2</kaptcha.version>
|
||||
<pagehelper.boot.version>1.3</pagehelper.boot.version>
|
||||
<pagehelper.boot.version>1.3.0</pagehelper.boot.version>
|
||||
<fastjson.version>1.2.73</fastjson.version>
|
||||
<xstream.version>1.4.13</xstream.version>
|
||||
<dom4j.version>2.1.3</dom4j.version>
|
||||
@@ -119,6 +119,12 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.org.easysite</groupId>
|
||||
<artifactId>easy-spring-boot-starter-wr-separation</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.org.easysite</groupId>
|
||||
<artifactId>easy-spring-boot-starters</artifactId>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>easy-spring-boot-starters</artifactId>
|
||||
<groupId>cn.org.easysite</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>easy-spring-boot-starter-wr-separation</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.org.easysite</groupId>
|
||||
<artifactId>easy-spring-boot-wr-separation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* @Copyright : Copyright (c) 2020
|
||||
* @Company : EasySite Technology 阿富汗 Co. Ltd.
|
||||
*/
|
||||
package cn.org.easysite.spring.boot.wr.separation.autoconfigure;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import cn.org.easysite.spring.boot.wr.separation.datasource.DynamicRoutingDataSource;
|
||||
import cn.org.easysite.spring.boot.wr.separation.processor.TransactionalBeanPostProcessor;
|
||||
|
||||
/**
|
||||
* @author : 潘多拉
|
||||
* @version : 1.0
|
||||
* @date : 2020/10/9 9:02 上午
|
||||
* @link : cn.org.easysite.spring.boot.wr.separation.autoconfigure.DatasourceSeparationAutoConfiguration
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
@Import(TransactionalBeanPostProcessor.class)
|
||||
public class DatasourceSeparationAutoConfiguration {
|
||||
|
||||
@Bean(name = "transactionManager")
|
||||
@ConditionalOnMissingBean(PlatformTransactionManager.class)
|
||||
public PlatformTransactionManager platformTransactionManager(DataSource dynamicRoutingDataSource) {
|
||||
return new DataSourceTransactionManager(dynamicRoutingDataSource);
|
||||
}
|
||||
|
||||
@Bean(name = "dynamicRoutingDataSource")
|
||||
@Primary
|
||||
public DynamicRoutingDataSource dynamicRoutingDataSource(DataSource writeDataSource, DataSource readDataSource) {
|
||||
DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
|
||||
Map<Object, Object> dataSourceMap = new HashMap<>();
|
||||
dataSourceMap.put("writeDataSource", writeDataSource);
|
||||
dataSourceMap.put("readDataSource", readDataSource);
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
dynamicDataSource.setDefaultTargetDataSource(writeDataSource);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.org.easysite.spring.boot.wr.separation.autoconfigure.DatasourceSeparationAutoConfiguration
|
||||
@@ -17,6 +17,7 @@
|
||||
<module>easy-spring-cloud-starter-feign</module>
|
||||
<module>easy-spring-boot-starter-dlock</module>
|
||||
<module>easy-spring-boot-starter-data-jpa</module>
|
||||
<module>easy-spring-boot-starter-wr-separation</module>
|
||||
</modules>
|
||||
<artifactId>easy-spring-boot-starters</artifactId>
|
||||
|
||||
|
||||
@@ -19,9 +19,9 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
||||
/**
|
||||
* 读或写
|
||||
*/
|
||||
READONLY("slaverDataSource"),
|
||||
READONLY("readDataSource"),
|
||||
|
||||
WRITE("masterDataSource");
|
||||
WRITE("writeDataSource");
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.org.easysite</groupId>
|
||||
<artifactId>easy-swagger-spring-boot-starter</artifactId>
|
||||
<artifactId>easy-spring-boot-starter-swagger</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
|
||||
Reference in New Issue
Block a user