From d6a4cbf6585603470b1e48e9db026306af2b8ac2 Mon Sep 17 00:00:00 2001 From: duLingLing Date: Fri, 9 Aug 2019 11:34:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Upd:=E4=BF=AE=E5=A4=8D=E5=A4=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=E5=88=87=E6=8D=A2=E6=97=A0=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../datasource/DataSourceConfiguration.java | 23 ++++++- .../pos/datasource/MybatisConfiguration.java | 63 ------------------- 3 files changed, 21 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java diff --git a/pom.xml b/pom.xml index a0f340742..1acb62ae8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 manage - 1.0.40 + 1.1.0 UTF-8 diff --git a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java index f50c714a1..d5abe9fb9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java +++ b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java @@ -6,9 +6,12 @@ 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 org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; /** * 多数据源配置类 @@ -21,15 +24,29 @@ public class DataSourceConfiguration { private Class dataSourceType; @Bean(name = "masterDataSource") - @Primary @ConfigurationProperties(prefix = "spring.datasource.master") - public DataSource masterDataSource(){ + public DataSource masterDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") - public DataSource slaveDataSource1(){ + public DataSource slaveDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); } + + + @Bean("routingDataSource") + @Primary + public AbstractRoutingDataSource roundRobinDataSouceProxy() { + ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource(); + Map targetDataResources = new HashMap<>(); + DataSource masterDataSource = masterDataSource(); + targetDataResources.put(DbContextHolder.DbType.MASTER, masterDataSource); + DataSource slaveDataSource = slaveDataSource(); + targetDataResources.put(DbContextHolder.DbType.SLAVE, slaveDataSource); + proxy.setDefaultTargetDataSource(masterDataSource);//默认源 + proxy.setTargetDataSources(targetDataResources); + return proxy; + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java deleted file mode 100644 index b329a44b4..000000000 --- a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/MybatisConfiguration.java +++ /dev/null @@ -1,63 +0,0 @@ -package au.com.royalpay.payment.manage.pos.datasource; - - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.ibatis.mapping.DatabaseIdProvider; -import org.apache.ibatis.plugin.Interceptor; -import org.apache.ibatis.session.SqlSessionFactory; -import org.aspectj.apache.bcel.util.ClassLoaderRepository; -import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer; -import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; -import org.mybatis.spring.boot.autoconfigure.MybatisProperties; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ResourceLoader; -import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; - -import javax.annotation.Resource; -import javax.sql.DataSource; -import java.util.List; -import java.util.Map; - -/** - * Created by Dulingling on 2019/8/7 - */ -@Configuration -@AutoConfigureAfter({DataSourceConfiguration.class}) -public class MybatisConfiguration extends MybatisAutoConfiguration { - - - private static Log logger = LogFactory.getLog(MybatisConfiguration.class); - - @Resource(name = "masterDataSource") - private DataSource masterDataSource; - @Resource(name = "slaveDataSource") - private DataSource slaveDataSource; - - public MybatisConfiguration(MybatisProperties properties, ObjectProvider interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider databaseIdProvider, ObjectProvider> configurationCustomizersProvider) { - super(properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider); - } - - @Bean - public SqlSessionFactory sqlSessionFactory() throws Exception { - return super.sqlSessionFactory(roundRobinDataSouceProxy()); - } - - public AbstractRoutingDataSource roundRobinDataSouceProxy(){ - ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource(); - Map targetDataResources = new ClassLoaderRepository.SoftHashMap(); - targetDataResources.put(DbContextHolder.DbType.MASTER,masterDataSource); - targetDataResources.put(DbContextHolder.DbType.SLAVE,slaveDataSource); - proxy.setDefaultTargetDataSource(masterDataSource);//默认源 - proxy.setTargetDataSources(targetDataResources); - return proxy; - } - - public void test(){ - - } -} - From 9911ef697fe0097b88996784186fd6eb9adb5c96 Mon Sep 17 00:00:00 2001 From: "taylor.dang" Date: Fri, 9 Aug 2019 14:44:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[Y]=20=E4=BF=AE=E6=94=B9=20datasource=20?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../manage/pos/datasource/DataSourceConfiguration.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 1acb62ae8..9c6390846 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 manage - 1.1.0 + 1.1.1 UTF-8 diff --git a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java index d5abe9fb9..5a0e59d46 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java +++ b/src/main/java/au/com/royalpay/payment/manage/pos/datasource/DataSourceConfiguration.java @@ -20,17 +20,17 @@ import java.util.Map; @Configuration @EnableTransactionManagement public class DataSourceConfiguration { - @Value("${spring.datasource.type}") + @Value("${app.datasource.type:'com.alibaba.druid.pool.DruidDataSource'}") private Class dataSourceType; @Bean(name = "masterDataSource") - @ConfigurationProperties(prefix = "spring.datasource.master") + @ConfigurationProperties(prefix = "app.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); } @Bean(name = "slaveDataSource") - @ConfigurationProperties(prefix = "spring.datasource.slave") + @ConfigurationProperties(prefix = "app.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().type(dataSourceType).build(); }