Commit 48d14e82 by naan1993

更新多数据源配置,增加一个开关控制是否开启多数据源

parent ff877842
...@@ -9,6 +9,7 @@ import com.stylefeng.guns.config.properties.MutiDataSourceProperties; ...@@ -9,6 +9,7 @@ import com.stylefeng.guns.config.properties.MutiDataSourceProperties;
import com.stylefeng.guns.core.mutidatesource.DynamicDataSource; import com.stylefeng.guns.core.mutidatesource.DynamicDataSource;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
...@@ -33,31 +34,41 @@ public class MybatisPlusConfig { ...@@ -33,31 +34,41 @@ public class MybatisPlusConfig {
@Autowired @Autowired
MutiDataSourceProperties mutiDataSourceProperties; MutiDataSourceProperties mutiDataSourceProperties;
/** /**
* druid数据库连接池 * 另一个数据源
*/ */
private DruidDataSource dataSourceGuns() { private DruidDataSource bizDataSource() {
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource); druidProperties.config(dataSource);
mutiDataSourceProperties.config(dataSource);
return dataSource; return dataSource;
} }
/** /**
* 多数据源配置 * guns的数据源
*/ */
private DruidDataSource bizDataSource() { private DruidDataSource dataSourceGuns(){
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = new DruidDataSource();
druidProperties.config(dataSource); druidProperties.config(dataSource);
mutiDataSourceProperties.config(dataSource);
return dataSource; return dataSource;
} }
/**
* 单数据源连接池配置
*/
@Bean
@ConditionalOnProperty(prefix = "guns", name = "muti-datasource-open", havingValue = "false")
public DruidDataSource singleDatasource() {
return dataSourceGuns();
}
/** /**
* 动态数据源 * 多数据源连接池配置
*/ */
@Bean @Bean
public DynamicDataSource dataSource() { @ConditionalOnProperty(prefix = "guns", name = "muti-datasource-open", havingValue = "true")
public DynamicDataSource mutiDataSource() {
DruidDataSource dataSourceGuns = dataSourceGuns(); DruidDataSource dataSourceGuns = dataSourceGuns();
DruidDataSource bizDataSource = bizDataSource(); DruidDataSource bizDataSource = bizDataSource();
......
...@@ -10,6 +10,7 @@ import org.aspectj.lang.annotation.Around; ...@@ -10,6 +10,7 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -24,6 +25,7 @@ import java.lang.reflect.Method; ...@@ -24,6 +25,7 @@ import java.lang.reflect.Method;
*/ */
@Aspect @Aspect
@Component @Component
@ConditionalOnProperty(prefix = "guns", name = "muti-datasource-open", havingValue = "true")
public class MultiSourceExAop implements Ordered { public class MultiSourceExAop implements Ordered {
private Logger log = Logger.getLogger(this.getClass()); private Logger log = Logger.getLogger(this.getClass());
......
...@@ -10,6 +10,7 @@ guns: ...@@ -10,6 +10,7 @@ guns:
#file-upload-path: d:/tmp #文件上传目录(不配置的话为java.io.tmpdir目录) #file-upload-path: d:/tmp #文件上传目录(不配置的话为java.io.tmpdir目录)
session-invalidate-time: 1800 #session失效时间 单位:秒 session-invalidate-time: 1800 #session失效时间 单位:秒
session-validation-interval: 900 #多久检测一次失效的session 单位:秒 session-validation-interval: 900 #多久检测一次失效的session 单位:秒
muti-datasource-open: false #是否开启多数据源(true/false)
################### 项目启动端口 ################### ################### 项目启动端口 ###################
server: server:
......
...@@ -20,8 +20,8 @@ public class BizTest extends BaseJunit { ...@@ -20,8 +20,8 @@ public class BizTest extends BaseJunit {
public void test() { public void test() {
//testService.testGuns(); //testService.testGuns();
//testService.testBiz(); testService.testBiz();
testService.testAll(); //testService.testAll();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment