项目中因为分库分表需要,需同时保留新旧数据源,所以需要引入多数据源的组件。 下面简单说明引入的过程,并对代码做简要分析。
引入 jar
gradle
compile("com.baomidou:dynamic-datasource-spring-boot-starter:3.1.1")
maven
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
配置文件
spring:
datasource:
dynamic:
datasource:
single:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:6033/account?serverTimezone=Asia/Shanghai
username: root
password: mRVHePdfa4Z3X0ewfctefpZuqDrSbtINR4VRslgA2s
type: com.zaxxer.hikari.HikariDataSource
primary: single
源码分析多数据源接入的过程
DynamicDataSourceAutoConfiguration.java
如果我们不写自己的配置文件,会默认加载该文件的配置
@Bean
@ConditionalOnMissingBean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource(); // 获取配置信息
return new YmlDynamicDataSourceProvider(datasourceMap); // 实例化 YmlDynamicDataSourceProvider
}
@Bean
@ConditionalOnMissingBean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setProvider(dynamicDataSourceProvider); // 将上面的 dynamicDataSourceProvider 到 dataSource
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
上面实例化 DynamicRoutingDataSource 类之后,并给属性设置了值,那在什么时候连接数据库资源呢,可以跳转到
DynamicRoutingDataSource
文件看下, 里面有一段很关键的代码
@Override
public void afterPropertiesSet() throws Exception {
Map<String, DataSource> dataSources = provider.loadDataSources(); // 看这里,这一步 provider.loadDataSources() 完成了对数据源的连接, 后面的的代码则进行分组
// 添加并分组数据源
for (Map.Entry<String, DataSource> dsItem : dataSources.entrySet()) {
addDataSource(dsItem.getKey(), dsItem.getValue());
}
// 检测默认数据源设置
if (groupDataSources.containsKey(primary)) {
log.info("dynamic-datasource initial loaded [{}] datasource,primary group datasource named [{}]", dataSources.size(), primary);
} else if (dataSourceMap.containsKey(primary)) {
log.info("dynamic-datasource initial loaded [{}] datasource,primary datasource named [{}]", dataSources.size(), primary);
} else {
throw new RuntimeException("dynamic-datasource Please check the setting of primary");
}
}
这时候我们可以来看下 provider 的 接口定义及实现
public interface DynamicDataSourceProvider {
/**
* 加载所有数据源
*
* @return 所有数据源,key为数据源名称
*/
Map<String, DataSource> loadDataSources();
}
抽象类 AbstractDataSourceProvider 实现了 DynamicDataSourceProvider
, 该方式定义了 createDataSourceMap 方法
@Slf4j
public abstract class AbstractDataSourceProvider implements DynamicDataSourceProvider {
@Autowired
private DataSourceCreator dataSourceCreator;
protected Map<String, DataSource> createDataSourceMap(
Map<String, DataSourceProperty> dataSourcePropertiesMap) {
Map<String, DataSource> dataSourceMap = new HashMap<>(dataSourcePropertiesMap.size() * 2);
for (Map.Entry<String, DataSourceProperty> item : dataSourcePropertiesMap.entrySet()) {
DataSourceProperty dataSourceProperty = item.getValue();
String pollName = dataSourceProperty.getPoolName();
if (pollName == null || "".equals(pollName)) {
pollName = item.getKey();
}
dataSourceProperty.setPoolName(pollName);
dataSourceMap.put(pollName, dataSourceCreator.createDataSource(dataSourceProperty)); // 这一步连接了数据源, DataSourceCreator 主要就调用更底层的类,完成对【连接数据源】的操作
}
return dataSourceMap;
}
}
而我们上面实例的 YmlDynamicDataSourceProvider 则继承了 AbstractDataSourceProvider
类,并实现了 loadDataSources
方法
@Override
public Map<String, DataSource> loadDataSources() {
return createDataSourceMap(dataSourcePropertiesMap);
}
分库分表组件的引入
前面提到了分库分表,自然要引入 shardingsphere 的 jar ,那如果将 shardingsphere 的数据源加入到数据源呢,这个时候就需要我们自己写个配置类,在默认的配置类之前完成配置
jar 包引入
gradle
compile("org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.1.0")
配置
spring:
shardingsphere:
datasource:
names: sharding
sharding:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:6033/account?serverTimezone=Asia/Shanghai
username: root
password: mRVHePdfa4Z3X0ewfctefpZuqDrSbtINR4VRslgA2s
type: com.zaxxer.hikari.HikariDataSource
rules:
sharding:
key-generators:
snowflake:
type: SNOWFLAKE
sharding-algorithms:
idhash:
props:
sharding-count: 3
type: MOD
tables:
user:
actual-data-nodes: sharding.user_$->{0..2}
key-generate-strategy:
column: id
key-generator-name: snowflake
table-strategy:
standard:
sharding-algorithm-name: idhash
sharding-column: id
配置类
package cn.codemao.service.platform.accounthub.hub.config;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.apache.shardingsphere.driver.jdbc.adapter.AbstractDataSourceAdapter;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class, SpringBootConfiguration.class})
public class DataSourceConfiguration {
final String SHARDING_DATA_RESOURCE_NAME = "sharding";
@Resource
private DynamicDataSourceProperties properties;
/**
* shardingSphereDataSource
*/
@Lazy
@Resource(name = "shardingSphereDataSource")
AbstractDataSourceAdapter shardingSphereDataSource;
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
return new AbstractDataSourceProvider() {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
// 将 shardingSphere 的数据源存储到 dataSourceMap,这里存储在 `sharding`, 取连接的时候要注意一样的标识,要不然会出现空指针的情况
dataSourceMap.put(SHARDING_DATA_RESOURCE_NAME, shardingSphereDataSource);
return dataSourceMap;
}
};
}
/**
* 将动态数据源设置为首选的
* 当spring存在多个数据源时, 自动注入的是首选的对象
* 这里可以简单解释下为什么要加 @Primary 注解
* MybatisAutoConfiguration 依赖到 DataSource,但是在我们引入多数据源和分表组件后,有两个文件
shardingSphereDataSource,DynamicDataSourceAutoConfiguration的方法都加上了 @Bean 注解,IOC 容器不确定哪个 DataSource 对象需要注入到容器,这个时候就要加上 @Primary 注解,标明当前这个Bean主要的,可以注入
*/
@Primary
@Bean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setProvider(dynamicDataSourceProvider);
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
总结
单纯搬网上的配置类,总感觉一直半解,分析源码后,就会对整个过程比较清晰。对应后续有可能出现的问题,也更容易解决。接下来会出一篇结合实际项目的分库分表文章 《shardingSphere 项目实战》
网友评论