Skip to content

Commit 373b269

Browse files
committed
perf: 修改数据源配置方式,由列表改为 map
1 parent 1f33e7f commit 373b269

2 files changed

Lines changed: 13 additions & 21 deletions

File tree

simple-application/src/main/resources/application.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ mybatis:
1919
# 自定义配置
2020
simple:
2121
datasource:
22-
# 数据源列表
2322
dbs:
24-
- name: db1
23+
db1:
2524
# 数据源类型
2625
type: com.zaxxer.hikari.HikariDataSource
2726
# 是否设置为默认数据源
@@ -32,7 +31,7 @@ simple:
3231
driver-class-name: com.mysql.cj.jdbc.Driver
3332
username: root
3433
password: xxxx
35-
- name: db2
34+
db2:
3635
type: com.zaxxer.hikari.HikariDataSource
3736
config:
3837
jdbc-url: jdbc:mysql://localhost:3306/demo_cluster?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

simple-framework/src/main/java/top/cadecode/framework/config/DataSourceConfig.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515

1616
import javax.sql.DataSource;
1717
import java.util.HashMap;
18-
import java.util.List;
1918
import java.util.Map;
2019
import java.util.Set;
21-
import java.util.concurrent.atomic.AtomicInteger;
22-
import java.util.stream.Collectors;
2320

2421
/**
2522
* @author Cade Li
@@ -33,36 +30,33 @@
3330
@ConfigurationProperties("simple.datasource")
3431
public class DataSourceConfig {
3532

36-
private static final String DBS_PREFIX = "simple.datasource.dbs[";
37-
private static final String DBS_SUFFIX = "].config";
33+
private static final String DBS_PREFIX = "simple.datasource.dbs.";
34+
private static final String DBS_SUFFIX = ".config";
3835

3936
private final Environment environment;
4037

4138
// 注入数据库配置
42-
private List<DbConfigObject> dbs;
39+
private Map<String, DbConfigObject> dbs;
4340

4441
@Bean
4542
public DynamicDataSource dynamicDataSource() {
4643
log.info("配置动态数据源");
4744
DynamicDataSource dynamicDS = new DynamicDataSource();
4845
// 获取数据库 key 集合
49-
Set<String> keys = dbs.stream()
50-
.map(DbConfigObject::getName)
51-
.collect(Collectors.toSet());
46+
Set<String> keys = dbs.keySet();
5247
// 存储 keys
5348
DynamicDataSourceHolder.addDataSourceKeys(keys);
5449
// 定义数据源容器
5550
Map<Object, Object> dataSourceMap = new HashMap<>();
5651
// 遍历 dbs
57-
AtomicInteger index = new AtomicInteger();
58-
dbs.forEach(db -> {
59-
log.info("解析数据源配置 " + db.getName());
60-
DataSource dataSource = getDataSource(db.getType(), db.getName(), index.getAndIncrement());
61-
dataSourceMap.put(db.getName(), dataSource);
52+
dbs.forEach((name, db) -> {
53+
log.info("解析数据源配置 " + name);
54+
DataSource dataSource = getDataSource(db.getType(), name);
55+
dataSourceMap.put(name, dataSource);
6256
// 设置默认数据源
6357
if (!dynamicDS.hasDefaultDataSource() && db.isDefaultFlag()) {
6458
dynamicDS.setDefaultTargetDataSource(dataSource);
65-
log.info("设置默认数据源为 " + db.getName());
59+
log.info("设置默认数据源为 " + name);
6660
}
6761
});
6862
// 判断是否设置默认数据源
@@ -83,7 +77,7 @@ public PlatformTransactionManager transactionManager() {
8377
* 获取数据源实例工具方法
8478
*/
8579
@SuppressWarnings("unchecked")
86-
private DataSource getDataSource(String type, String name, int index) {
80+
private DataSource getDataSource(String type, String name) {
8781
Class<DataSource> dataSourceClass;
8882
try {
8983
dataSourceClass = (Class<DataSource>) Class.forName(type);
@@ -93,7 +87,7 @@ private DataSource getDataSource(String type, String name, int index) {
9387
}
9488
// 生成数据源实例
9589
return Binder.get(environment)
96-
.bind(DBS_PREFIX + index + DBS_SUFFIX, dataSourceClass)
90+
.bind(DBS_PREFIX + name + DBS_SUFFIX, dataSourceClass)
9791
.get();
9892
} catch (ClassNotFoundException e) {
9993
throw new IllegalArgumentException("数据源 " + name + " 的类型 " + type + " 不存在");
@@ -107,7 +101,6 @@ private DataSource getDataSource(String type, String name, int index) {
107101
*/
108102
@Data
109103
public static class DbConfigObject {
110-
private String name;
111104
private String type;
112105
private boolean defaultFlag;
113106
}

0 commit comments

Comments
 (0)