需要引入的包:
<properties>
<sharding-sphere.version>4.1.0</sharding-sphere.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
</dependencies>
導入包之後,開始配置 yml 文件
spring:
shardingsphere:
dataSource:
names: db-master,db-slave1,db-slave2
# 配置主庫
db-master: #org.apache.tomcat.jdbc.pool.DataSource
name: master-${spring.application.name}
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${seowen.database.master.host}/${seowen.database.master.db}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&failOverReadOnly=false
username: ${seowen.database.master.username}
password: ${seowen.database.master.password}
# 使用druid數據源
filters: stat,wall,config,log4j
#最大連接池數量
maxActive: 10
#最小連接池數量
minIdle: 6
#初始化時建立物理連接的個數。初始化發生在顯示調用init方法,或者第一次getConnection時
initialSize: 6
#獲取連接時最大等待時間,單位毫秒。配置了maxWait之後,缺省啓用公平鎖,併發效率會有所下降,如果需要可以通過配置useUnfairLock屬性爲true使用非公平鎖。
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
timeBetweenEvictionRunsMillis: 300000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 900000
#用來檢測連接是否有效的sql
validationQuery: select 'x'
# 申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。
testOnBorrow: false
#歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
testOnReturn: false
#建議配置爲true,不影響性能,並且保證安全性。申請連接的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
#testWhileIdle: true
#KeepAlive之後的效果
#初始化連接池時會填充到minIdle數量。
#連接池中的minIdle數量以內的連接,更長的時間超過minEvictableIdleTimeMillis,可以執行keepAlive操作。
#當網絡軸向等原因產生的由ExceptionSorter檢測出來的死連接被清除後,自動補充連接到minIdle數量。
keepAlive: true
#是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。在mysql下建議關閉。
poolPreparedStatements: false
#要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改爲true。在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,可以把這個數值配置大一些,比如說100
maxOpenPreparedStatements: -1
maxPoolPreparedStatementPerConnectionSize: 10
useGlobalDataSourceStat: true
#timeBetweenLogStatsMillis: 1000
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
db-slave1: # 配置第一個從庫
name: slave1-${spring.application.name}
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${seowen.database.slave1.host}/${seowen.database.slave1.db}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&failOverReadOnly=false
username: ${seowen.database.slave1.username}
password: ${seowen.database.slave1.password}
filters: stat,wall,config,log4j
#最大連接池數量
maxActive: 10
#最小連接池數量
minIdle: 6
#初始化時建立物理連接的個數。初始化發生在顯示調用init方法,或者第一次getConnection時
initialSize: 6
#獲取連接時最大等待時間,單位毫秒。配置了maxWait之後,缺省啓用公平鎖,併發效率會有所下降,如果需要可以通過配置useUnfairLock屬性爲true使用非公平鎖。
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
timeBetweenEvictionRunsMillis: 300000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 900000
#用來檢測連接是否有效的sql
validationQuery: select 'x'
# 申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。
testOnBorrow: false
#歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
testOnReturn: false
#建議配置爲true,不影響性能,並且保證安全性。申請連接的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
#testWhileIdle: true
#KeepAlive之後的效果
#初始化連接池時會填充到minIdle數量。
#連接池中的minIdle數量以內的連接,更長的時間超過minEvictableIdleTimeMillis,可以執行keepAlive操作。
#當網絡軸向等原因產生的由ExceptionSorter檢測出來的死連接被清除後,自動補充連接到minIdle數量。
keepAlive: true
#是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。在mysql下建議關閉。
poolPreparedStatements: false
#要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改爲true。在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,可以把這個數值配置大一些,比如說100
maxOpenPreparedStatements: -1
maxPoolPreparedStatementPerConnectionSize: 10
useGlobalDataSourceStat: true
#timeBetweenLogStatsMillis: 1000
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
db-slave2: # 配置第二個從庫
name: slave2-${spring.application.name}
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${seowen.database.slave2.host}/${seowen.database.slave2.db}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&failOverReadOnly=false
username: ${seowen.database.slave2.username}
password: ${seowen.database.slave2.password}
filters: stat,wall,config,log4j
#最大連接池數量
maxActive: 10
#最小連接池數量
minIdle: 6
#初始化時建立物理連接的個數。初始化發生在顯示調用init方法,或者第一次getConnection時
initialSize: 6
#獲取連接時最大等待時間,單位毫秒。配置了maxWait之後,缺省啓用公平鎖,併發效率會有所下降,如果需要可以通過配置useUnfairLock屬性爲true使用非公平鎖。
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
timeBetweenEvictionRunsMillis: 300000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 900000
#用來檢測連接是否有效的sql
validationQuery: select 'x'
# 申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。
testOnBorrow: false
#歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
testOnReturn: false
#建議配置爲true,不影響性能,並且保證安全性。申請連接的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
#testWhileIdle: true
#KeepAlive之後的效果
#初始化連接池時會填充到minIdle數量。
#連接池中的minIdle數量以內的連接,更長的時間超過minEvictableIdleTimeMillis,可以執行keepAlive操作。
#當網絡軸向等原因產生的由ExceptionSorter檢測出來的死連接被清除後,自動補充連接到minIdle數量。
keepAlive: true
#是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。在mysql下建議關閉。
poolPreparedStatements: false
#要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改爲true。在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,可以把這個數值配置大一些,比如說100
maxOpenPreparedStatements: -1
maxPoolPreparedStatementPerConnectionSize: 10
useGlobalDataSourceStat: true
#timeBetweenLogStatsMillis: 1000
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
db-slave3: # 另外一個獨立的數據源
name: slave3-keda-sys-rabbitmq
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${seowen.database.slave3.host}/${seowen.database.slave3.db}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&failOverReadOnly=false
username: ${seowen.database.slave3.username}
password: ${seowen.database.slave3.password}
filters: stat,wall,config,log4j
#最大連接池數量
maxActive: 10
#最小連接池數量
minIdle: 6
#初始化時建立物理連接的個數。初始化發生在顯示調用init方法,或者第一次getConnection時
initialSize: 6
#獲取連接時最大等待時間,單位毫秒。配置了maxWait之後,缺省啓用公平鎖,併發效率會有所下降,如果需要可以通過配置useUnfairLock屬性爲true使用非公平鎖。
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
timeBetweenEvictionRunsMillis: 300000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 900000
#用來檢測連接是否有效的sql
validationQuery: select 'x'
# 申請連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能。
testOnBorrow: false
#歸還連接時執行validationQuery檢測連接是否有效,做了這個配置會降低性能
testOnReturn: false
#建議配置爲true,不影響性能,並且保證安全性。申請連接的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
#testWhileIdle: true
#KeepAlive之後的效果
#初始化連接池時會填充到minIdle數量。
#連接池中的minIdle數量以內的連接,更長的時間超過minEvictableIdleTimeMillis,可以執行keepAlive操作。
#當網絡軸向等原因產生的由ExceptionSorter檢測出來的死連接被清除後,自動補充連接到minIdle數量。
keepAlive: true
#是否緩存preparedStatement,也就是PSCache。PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。在mysql下建議關閉。
poolPreparedStatements: false
#要啓用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改爲true。在Druid中,不會存在Oracle下PSCache佔用內存過多的問題,可以把這個數值配置大一些,比如說100
maxOpenPreparedStatements: -1
maxPoolPreparedStatementPerConnectionSize: 10
useGlobalDataSourceStat: true
#timeBetweenLogStatsMillis: 1000
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
masterslave: # 配置讀寫分離
load-balance-algorithm-type: round_robin # 配置從庫選擇策略,提供輪詢與隨機,這裏選擇用輪詢//random 隨機 //round_robin 輪詢
name: db-keda
master-data-source-name: db-master
slave-data-source-names: db-slave1,db-slave2
props:
sql.show: true # 開啓SQL顯示,默認值: false,注意:僅配置讀寫分離時不會打印日誌!!!
executor.size: 4 #工作線程數量最大,默認值: 無限制
acceptor.size: 4 # accept連接的線程數量,默認爲cpu核數2倍
seowen:
database:
master:
host: 192.168.1.1:3307
db: seowen_information
username: root
password: 123456
slave1:
host: 192.168.1.2:3307
db: seowen_information
username: root
password: 123456
slave2:
host: 192.168.1.3:3307
db: seowen_information
username: root
password: 123456
slave3:
host: 192.168.1.4:3308
db: seowen_rabbitmq
username: root
password: 123456
參數說明:
names: db-master,db-slave1,db-slave2 聲明sharding 的主從數據源key, 一主二從。 可以注意到,我實際上是有配置第4個數據源--slave3, 但是並沒有聲明在此。
原因,因爲 slave3是一個獨立的數據源,跟 其他三個數據源的業務上無任何關係,僅僅是用來 記錄 rabbitmq的收發記錄的。
此外,每個數據源的連接池都用了 druld
然後啓動項目,報如下錯誤:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.]--
[keda-information-main]--[192.168.1.158]--[2020-05-16 10:14:12.052]--[ERROR]--[ERROR [,,,]]--[PID:: 23044]--[T:: main]--[CL:: org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter]--[L:: 40]--[MD:: report]--
[MSG::
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
]--
Disconnected from the target VM, address: '127.0.0.1:53512', transport: 'socket'
Process finished with exit code 1
在出現這個錯誤的地方,往上翻看, 會看如下內容:
因爲我使用的是 druid,從圈出來的地方,可以看到在系統啓動的時候,druid 的 DruidDataSourceAutoConfigure會啓動自動裝配,並調用 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties的數據源屬性配置, 但是我上面的配置,並沒有配置 spring原生的 spring.dataSource 屬性, 所以 自然就無法找到 url 的相關配置,那druid就會創建失敗。
解決方式:禁用 Druid的數據源自動配置 DruidDataSourceAutoConfigure.class,
同時官方建議,禁止 JtaAutoConfiguration.class
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class,JtaAutoConfiguration.class})
重新啓動,出現如下,代表成功
同時,打開druid的 監控平臺:
"
可以看到,只有三個數據源(master、slave1、slave2),並沒有 slave3. 查看源碼如下:
package org.apache.shardingsphere.shardingjdbc.spring.boot;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.ShardingRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.impl.ShadowRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShadowDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.spring.boot.common.SpringBootPropertiesConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.EncryptRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.SpringBootEncryptRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.masterslave.MasterSlaveRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.masterslave.SpringBootMasterSlaveRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.ShadowRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.SpringBootShadowRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.ShardingRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties;
import org.apache.shardingsphere.spring.boot.datasource.DataSourcePropertiesSetterHolder;
import org.apache.shardingsphere.spring.boot.util.DataSourceUtil;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import org.apache.shardingsphere.transaction.spring.ShardingTransactionTypeScanner;
import org.apache.shardingsphere.underlying.common.config.inline.InlineExpressionParser;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiObjectFactoryBean;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Spring boot starter configuration.
*/
@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties({
SpringBootShardingRuleConfigurationProperties.class,
SpringBootMasterSlaveRuleConfigurationProperties.class, SpringBootEncryptRuleConfigurationProperties.class,
SpringBootPropertiesConfigurationProperties.class, SpringBootShadowRuleConfigurationProperties.class})
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@RequiredArgsConstructor
public class SpringBootConfiguration implements EnvironmentAware {
private final SpringBootShardingRuleConfigurationProperties shardingRule;
private final SpringBootMasterSlaveRuleConfigurationProperties masterSlaveRule;
private final SpringBootEncryptRuleConfigurationProperties encryptRule;
private final SpringBootShadowRuleConfigurationProperties shadowRule;
private final SpringBootPropertiesConfigurationProperties props;
private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();
private final String jndiName = "jndi-name";
/**
* Get sharding data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(ShardingRuleCondition.class)
public DataSource shardingDataSource() throws SQLException {
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new ShardingRuleConfigurationYamlSwapper().swap(shardingRule), props.getProps());
}
/**
* Get master-slave data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(MasterSlaveRuleCondition.class)
public DataSource masterSlaveDataSource() throws SQLException {
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRule), props.getProps());
}
/**
* Get encrypt data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(EncryptRuleCondition.class)
public DataSource encryptDataSource() throws SQLException {
return EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), new EncryptRuleConfigurationYamlSwapper().swap(encryptRule), props.getProps());
}
/**
* Get shadow data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(ShadowRuleCondition.class)
public DataSource shadowDataSource() throws SQLException {
return ShadowDataSourceFactory.createDataSource(dataSourceMap, new ShadowRuleConfigurationYamlSwapper().swap(shadowRule), props.getProps());
}
/**
* Create sharding transaction type scanner.
*
* @return sharding transaction type scanner
*/
@Bean
public ShardingTransactionTypeScanner shardingTransactionTypeScanner() {
return new ShardingTransactionTypeScanner();
}
@Override
public final void setEnvironment(final Environment environment) {
String prefix = "spring.shardingsphere.datasource.";
for (String each : getDataSourceNames(environment, prefix)) {
try {
dataSourceMap.put(each, getDataSource(environment, prefix, each));
} catch (final ReflectiveOperationException ex) {
throw new ShardingSphereException("Can't find datasource type!", ex);
} catch (final NamingException namingEx) {
throw new ShardingSphereException("Can't find JNDI datasource!", namingEx);
}
}
}
private List<String> getDataSourceNames(final Environment environment, final String prefix) {
StandardEnvironment standardEnv = (StandardEnvironment) environment;
standardEnv.setIgnoreUnresolvableNestedPlaceholders(true);
return null == standardEnv.getProperty(prefix + "name")
? new InlineExpressionParser(standardEnv.getProperty(prefix + "names")).splitAndEvaluate() : Collections.singletonList(standardEnv.getProperty(prefix + "name"));
}
@SuppressWarnings("unchecked")
private DataSource getDataSource(final Environment environment, final String prefix, final String dataSourceName) throws ReflectiveOperationException, NamingException {
Map<String, Object> dataSourceProps = PropertyUtil.handle(environment, prefix + dataSourceName.trim(), Map.class);
Preconditions.checkState(!dataSourceProps.isEmpty(), "Wrong datasource properties!");
if (dataSourceProps.containsKey(jndiName)) {
return getJndiDataSource(dataSourceProps.get(jndiName).toString());
}
DataSource result = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
DataSourcePropertiesSetterHolder.getDataSourcePropertiesSetterByType(dataSourceProps.get("type").toString()).ifPresent(
dataSourcePropertiesSetter -> dataSourcePropertiesSetter.propertiesSet(environment, prefix, dataSourceName, result));
return result;
}
private DataSource getJndiDataSource(final String jndiName) throws NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setResourceRef(true);
bean.setJndiName(jndiName);
bean.setProxyInterface(DataSource.class);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
}
其中的主要方法如下:
從以上源碼可以發現,該方法是先獲取names 屬性,分割成 List<String>,然後循環List,以 元素爲key ,來創建 DataSource,並存入 dataSourceMap 的Map中。
由此,可以明白。 shardingsphere 是通過 聲明的 names 的屬性 ,來創建相應的數據源。 而我的 slave3 沒有聲明,所以就不會被創建。 也就不會被 sharding所管轄。 可以以此,實現多數據源的目的。
重要:
既然,sharding沒有創建 slave3數據源, 那我可以自己創建,並設置相關配置。代碼如下:
@Configuration
//@PropertySource(value = "classpath:datasource.properties",
// ignoreResourceNotFound = true,encoding = "UTF-8")
@MapperScan(basePackages = "com.kd.mq.mapper", sqlSessionFactoryRef = "mqSessionFactoryBean")
public class MqConfig {
private final Logger LOGGER = LoggerFactory.getLogger(MqConfig.class);
/**
* 獲取 mq 數據源配置信息,並創建 DruidDataSource
* @return
*/
@Bean(name = "mqDataSource")
@ConfigurationProperties(prefix = "spring.shardingsphere.datasource.db-slave3")
public DataSource druidDataSource(){
return DruidDataSourceBuilder.create().build();
}
/**
* 創建 mq的 MybatisSqlSessionFactoryBean, 並將上面創建的數據源,注入進去
* @param mqDataSource
* @return
* @throws Exception
*/
@Bean("mqSessionFactoryBean")
public MybatisSqlSessionFactoryBean mqSqlSessionFactoryBean(
@Qualifier("mqDataSource")DataSource mqDataSource)throws Exception {
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(mqDataSource);
mybatisSqlSessionFactoryBean.setTypeHandlersPackage("com.kd.mq.handle");
return mybatisSqlSessionFactoryBean;
}
/**
* 獲取配置文件的 mybatis-plus 配置,並設置爲 Primary。不然會報錯
* @return
*/
@ConfigurationProperties(prefix = "mybatis-plus")
@Bean("defaultMybatisPlus")
@Primary
public MybatisPlusProperties defaultMybatisPlus(){
return new MybatisPlusProperties();
}
/**
* 創建 主從數據源 的MybatisSqlSessionFactoryBean,並注入 sharding創建的數據源,
* 和 mybatis-plus 配置 以及 自定義的Interceptor攔截器數組,這樣才能使 mybatis-plus的相關配置起效
*
* @param dataSource
* @param mybatisPlusProperties
* @param interceptor
* @return
* @throws Exception
*/
@Bean("defaultSqlSessionFactoryBean")
public MybatisSqlSessionFactoryBean defaultSqlSessionFactoryBean(@Qualifier("masterSlaveDataSource") DataSource dataSource
, @Qualifier("defaultMybatisPlus") MybatisPlusProperties mybatisPlusProperties
, Interceptor[] interceptor)throws Exception {
// 這裏用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否則 MyBatisPlus 不會生效
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSource);
mybatisSqlSessionFactoryBean.setConfiguration(mybatisPlusProperties.getConfiguration());
mybatisSqlSessionFactoryBean.setConfigurationProperties(mybatisPlusProperties.getConfigurationProperties());
mybatisSqlSessionFactoryBean.setGlobalConfig(mybatisPlusProperties.getGlobalConfig());
mybatisSqlSessionFactoryBean.setTypeAliasesPackage(mybatisPlusProperties.getTypeAliasesPackage());
mybatisSqlSessionFactoryBean.setTypeAliasesSuperType(mybatisPlusProperties.getTypeAliasesSuperType());
mybatisSqlSessionFactoryBean.setTypeEnumsPackage(mybatisPlusProperties.getTypeEnumsPackage());
mybatisSqlSessionFactoryBean.setTypeHandlersPackage(mybatisPlusProperties.getTypeHandlersPackage());
//獲取 mybatis 配置的 mapper文件
mybatisSqlSessionFactoryBean.setMapperLocations(this.getResources(mybatisPlusProperties.getMapperLocations()));
mybatisSqlSessionFactoryBean.setPlugins(interceptor);
return mybatisSqlSessionFactoryBean;
}
/**
* 配置事務管理器,注入 主從數據源。 不然事務無法起作用
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(@Qualifier("masterSlaveDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
/**
* 加載 mybatis 配置的 mapper 文件
* @param strings
* @return
* @throws IOException
*/
private Resource[] getResources(String [] strings) throws IOException {
if (ArrayUtils.isNotEmpty(strings)) {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
List<Resource> list = new ArrayList<>(strings.length);
for (String str :strings){
list.addAll(Arrays.asList(resolver.getResources(str)));
}
return list.toArray(new Resource[list.size()]);
}
return null;
}
}