封存
This commit is contained in:
-1
@@ -65,7 +65,6 @@ public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
* 需要注入数据源
|
||||
*/
|
||||
@Autowired
|
||||
@Qualifier("masterDataSource")
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -61,10 +61,6 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<!--数据库连接池相关依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
|
||||
+4
-4
@@ -13,13 +13,13 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
* @version 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SalveDataSourceCondition implements Condition {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SalveDataSourceCondition.class);
|
||||
public class SlaveDataSourceCondition implements Condition {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SlaveDataSourceCondition.class);
|
||||
@Override
|
||||
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
|
||||
Environment environment = conditionContext.getEnvironment();
|
||||
//判断环境中是否有从数据库的配置
|
||||
logger.info("是否包含从数据库: {}",environment.containsProperty("druid.two.username"));
|
||||
return environment.containsProperty("druid.two.username");
|
||||
logger.info("是否包含从数据库: {}",environment.containsProperty("spring.datasource.druid.slave.username"));
|
||||
return environment.containsProperty("spring.datasource.druid.slave.username");
|
||||
}
|
||||
}
|
||||
+12
-7
@@ -1,6 +1,7 @@
|
||||
package com.micro.fast.common.config;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import com.micro.fast.common.condition.SlaveDataSourceCondition;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
@@ -9,19 +10,23 @@ import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author lsy
|
||||
*/
|
||||
@Configuration
|
||||
public class DruidDataSourceConfig {
|
||||
|
||||
@Primary
|
||||
@Bean(name = "masterDataSource")
|
||||
@ConfigurationProperties("druid.one")
|
||||
public DataSource dataSourceOne(){
|
||||
@Bean(name = DynamicDataSourceConfig.MASTER_DATA_SOURCE)
|
||||
@ConfigurationProperties("spring.datasource.druid.master")
|
||||
public DataSource masterDataSource(){
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean(name = "slaveDataSource")
|
||||
@ConfigurationProperties("druid.two")
|
||||
@Conditional(com.micro.fast.common.condition.SalveDataSourceCondition.class)
|
||||
public DataSource dataSourceTwo(){
|
||||
@Bean(name = DynamicDataSourceConfig.SLAVE_DATA_SOURCE)
|
||||
@ConfigurationProperties("spring.datasource.druid.slave")
|
||||
@Conditional(SlaveDataSourceCondition.class)
|
||||
public DataSource slaveDataSource(){
|
||||
return DruidDataSourceBuilder.create().build();
|
||||
}
|
||||
}
|
||||
|
||||
+26
-7
@@ -1,5 +1,6 @@
|
||||
package com.micro.fast.common.config;
|
||||
|
||||
import com.micro.fast.common.dao.DataSourceContextHolder;
|
||||
import com.micro.fast.common.dao.DynamicDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -22,20 +23,31 @@ import java.util.Map;
|
||||
@Configuration
|
||||
@AutoConfigureBefore({SqlSessionFactoryConfig.class})
|
||||
public class DynamicDataSourceConfig {
|
||||
public static final String SLAVE = "slave";
|
||||
public static final String MASTER = "master";
|
||||
public static final String SLAVE_DATA_SOURCE = "slaveDataSource";
|
||||
public static final String MASTER_DATA_SOURCE = "masterDataSource";
|
||||
|
||||
/**
|
||||
* 注入主数据源
|
||||
* 当ataSource存在时
|
||||
*/
|
||||
@Autowired
|
||||
@Qualifier("masterDataSource")
|
||||
@Qualifier(MASTER_DATA_SOURCE)
|
||||
private DataSource masterDataSource;
|
||||
|
||||
/**
|
||||
* 从数据源
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
@Qualifier("slaveDataSource")
|
||||
@Qualifier(SLAVE_DATA_SOURCE)
|
||||
private DataSource slaveDataSource;
|
||||
|
||||
/**
|
||||
* 收集所有的datasource
|
||||
*/
|
||||
@Autowired
|
||||
private Map<String,DataSource> dataSourceMap;
|
||||
|
||||
@Bean(name = "dynamicDataSource")
|
||||
public DataSource dynamicDataSource(){
|
||||
//动态数据源对象
|
||||
@@ -44,12 +56,19 @@ public class DynamicDataSourceConfig {
|
||||
dynamicDataSource.setDefaultTargetDataSource(masterDataSource);
|
||||
//配置多数据源
|
||||
Map<Object,Object> dataSourceMap = new HashMap<>();
|
||||
//填入主数据源
|
||||
dataSourceMap.put("master",masterDataSource);
|
||||
//如果从数据源存在,填入从数据源
|
||||
//根据数据源存在情况调整动态数据源配置
|
||||
dataSourceMap.put(MASTER,masterDataSource);
|
||||
if (slaveDataSource!=null){
|
||||
dataSourceMap.put("slave",slaveDataSource);
|
||||
dataSourceMap.put(SLAVE,slaveDataSource);
|
||||
}
|
||||
this.dataSourceMap.forEach((key,value) -> {
|
||||
if (!SLAVE_DATA_SOURCE.equals(key)
|
||||
&& !MASTER_DATA_SOURCE.equals(key)
|
||||
&& !MASTER.equals(key)
|
||||
&& !SLAVE.equals(key)){
|
||||
dataSourceMap.put(key,value);
|
||||
}
|
||||
});
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
|
||||
-1
@@ -38,7 +38,6 @@ public class SqlSessionFactoryConfig implements TransactionManagementConfigurer{
|
||||
|
||||
/**
|
||||
* 数据源对象
|
||||
* @see com.micro.fast.common.config.DataSourceMasterConfig
|
||||
*/
|
||||
@Autowired
|
||||
@Qualifier("dynamicDataSource")
|
||||
|
||||
+5
-1
@@ -1,5 +1,6 @@
|
||||
package com.micro.fast.common.dao;
|
||||
|
||||
import com.micro.fast.common.config.DynamicDataSourceConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -25,7 +26,7 @@ public class DataSourceContextHolder {
|
||||
/**
|
||||
* 默认的数据源的名称
|
||||
*/
|
||||
public static final String DEFAULT_DATDASOURCE_NAME = "master";
|
||||
public static final String DEFAULT_DATDASOURCE_NAME = DynamicDataSourceConfig.MASTER;
|
||||
|
||||
/**
|
||||
* 设置数据源名
|
||||
@@ -41,6 +42,9 @@ public class DataSourceContextHolder {
|
||||
* @return
|
||||
*/
|
||||
public static String getDatdasourceName(){
|
||||
if (contextHolder.get()==null){
|
||||
return DEFAULT_DATDASOURCE_NAME;
|
||||
}
|
||||
return contextHolder.get();
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -14,11 +14,11 @@ public class DynamicDataSource extends AbstractRoutingDataSource{
|
||||
/**
|
||||
* 记录日志
|
||||
*/
|
||||
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(DynamicDataSource.class);
|
||||
|
||||
@Override
|
||||
public Object determineCurrentLookupKey() {
|
||||
logger.debug("数据源{}",DataSourceContextHolder.getDatdasourceName());
|
||||
log.debug("数据源{}",DataSourceContextHolder.getDatdasourceName());
|
||||
return DataSourceContextHolder.getDatdasourceName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
name: cms
|
||||
discovery:
|
||||
enabled: true
|
||||
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
name: gateway
|
||||
discovery:
|
||||
enabled: true
|
||||
|
||||
@@ -4,7 +4,7 @@ spring:
|
||||
config:
|
||||
uri: http://config:server@www.config.com:10000/
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
name: eureka-server
|
||||
eureka:
|
||||
client:
|
||||
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
config:
|
||||
name: eureka-server2
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
uri: http://config:server@www.config.com:10000/
|
||||
eureka:
|
||||
client:
|
||||
|
||||
@@ -2,10 +2,6 @@ package com.micro.fast.ucenter;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.cloud.client.SpringCloudApplication;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
name: ucenter
|
||||
discovery:
|
||||
service-id: config-server
|
||||
|
||||
@@ -5,7 +5,7 @@ spring:
|
||||
cloud:
|
||||
config:
|
||||
profile: dev
|
||||
label: master
|
||||
label: druid-springboot
|
||||
name: upms
|
||||
discovery:
|
||||
service-id: config-server
|
||||
|
||||
Reference in New Issue
Block a user