Skip to content

Commit 59c3777

Browse files
committed
feat #IAUS2R sql插件支持指定数据源
1 parent 2ea9e18 commit 59c3777

File tree

30 files changed

+787
-34
lines changed

30 files changed

+787
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.yomahub.liteflow.exception;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
5+
/**
6+
* 缺少 maven 依赖异常
7+
*
8+
* @author tkc
9+
* @since 2.12.5
10+
*/
11+
public class MissMavenDependencyException extends RuntimeException {
12+
13+
private static final long serialVersionUID = 1L;
14+
private static final String TEMPLATE = "miss maven dependency " + "\n" +
15+
"<dependency>\n" +
16+
" <groupId>{groupId}</groupId>\n" +
17+
" <artifactId>{artifactId}</artifactId>\n" +
18+
" <version>${version}</version>\n" +
19+
"</dependency>";
20+
21+
/**
22+
* 异常信息
23+
*/
24+
private String message;
25+
26+
public MissMavenDependencyException(String groupId, String artifactId) {
27+
this.message = StrUtil.format(TEMPLATE, groupId, artifactId);
28+
}
29+
30+
@Override
31+
public String getMessage() {
32+
return message;
33+
}
34+
35+
public void setMessage(String message) {
36+
this.message = message;
37+
}
38+
39+
}

liteflow-core/src/main/java/com/yomahub/liteflow/spi/ContextAware.java

+12
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ public interface ContextAware extends SpiPriority {
3333
*/
3434
<T> Map<String, T> getBeansOfType(Class<T> type);
3535

36+
/**
37+
* 判断是否存在指定名称的bean
38+
*
39+
* @param beanName bean 名称
40+
*/
3641
boolean hasBean(String beanName);
3742

43+
/**
44+
* 判断是否存在指定类型的bean
45+
*
46+
* @param clazz 类型
47+
*/
48+
boolean hasBean(Class<?> clazz);
49+
3850
Object registerDeclWrapBean(String beanName, DeclWarpBean declWarpBean);
3951

4052
}

liteflow-core/src/main/java/com/yomahub/liteflow/spi/local/LocalContextAware.java

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public boolean hasBean(String beanName) {
5454
return false;
5555
}
5656

57+
@Override
58+
public boolean hasBean(Class<?> clazz) {
59+
return false;
60+
}
61+
5762
@Override
5863
public Object registerDeclWrapBean(String beanName, DeclWarpBean declWarpBean) {
5964
return null;

liteflow-rule-plugin/liteflow-rule-sql/pom.xml

+18
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,23 @@
2525
<groupId>cn.hutool</groupId>
2626
<artifactId>hutool-crypto</artifactId>
2727
</dependency>
28+
29+
<!-- 苞米豆动态数据源 -->
30+
<dependency>
31+
<groupId>com.baomidou</groupId>
32+
<artifactId>dynamic-datasource-spring</artifactId>
33+
<version>${dynamic-datasource.version}</version>
34+
<optional>true</optional>
35+
<scope>provided</scope>
36+
</dependency>
37+
38+
<!--Sharding JDBC -->
39+
<dependency>
40+
<groupId>org.apache.shardingsphere</groupId>
41+
<artifactId>sharding-jdbc-core</artifactId>
42+
<version>${sharding-jdbc.version}</version>
43+
<optional>true</optional>
44+
<scope>provided</scope>
45+
</dependency>
2846
</dependencies>
2947
</project>

liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/SQLXmlELParser.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.yomahub.liteflow.core.FlowInitHook;
99
import com.yomahub.liteflow.parser.constant.ReadType;
1010
import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
11+
import com.yomahub.liteflow.parser.sql.datasource.LiteflowDataSourceConnectFactory;
1112
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
1213
import com.yomahub.liteflow.parser.sql.read.SqlReadFactory;
1314
import com.yomahub.liteflow.parser.sql.util.JDBCHelper;
@@ -58,6 +59,9 @@ public SQLXmlELParser() {
5859
// 初始化 SqlReadFactory
5960
SqlReadFactory.registerRead(sqlParserVO);
6061

62+
// 初始化连接器
63+
LiteflowDataSourceConnectFactory.register();
64+
6165
// 注册轮询任务
6266
SqlReadFactory.registerSqlReadPollTask(ReadType.CHAIN);
6367
SqlReadFactory.registerSqlReadPollTask(ReadType.SCRIPT);
@@ -92,7 +96,7 @@ public String parseCustom() {
9296
* @param sqlParserVO sqlParserVO
9397
*/
9498
private void checkParserVO(SQLParserVO sqlParserVO) {
95-
if (sqlParserVO.isDefaultDataSource()) {
99+
if (sqlParserVO.isAutoFoundDataSource()) {
96100
return;
97101
}
98102
if (StrUtil.isEmpty(sqlParserVO.getUrl())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.yomahub.liteflow.parser.sql.datasource;
2+
3+
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
4+
5+
import java.sql.Connection;
6+
7+
/**
8+
* 数据源获取接口
9+
*
10+
* @author tkc
11+
* @since 2.12.5
12+
*/
13+
public interface LiteFlowDataSourceConnect {
14+
15+
/**
16+
* 检查是否支持该数据源
17+
*/
18+
boolean filter(SQLParserVO config);
19+
20+
/**
21+
* 获取连接
22+
*/
23+
Connection getConn(SQLParserVO config) throws Exception;
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.yomahub.liteflow.parser.sql.datasource;
2+
3+
import cn.hutool.core.collection.CollUtil;
4+
import com.yomahub.liteflow.parser.sql.datasource.impl.BaoMiDouDynamicDsConn;
5+
import com.yomahub.liteflow.parser.sql.datasource.impl.ShardingJdbcDsConn;
6+
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
7+
import com.yomahub.liteflow.spi.ContextAware;
8+
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
9+
10+
import java.util.Collection;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Optional;
14+
15+
/**
16+
* 数据源获取接口工厂
17+
*
18+
* @author tkc
19+
* @since 2.12.5
20+
*/
21+
public class LiteflowDataSourceConnectFactory {
22+
private static List<LiteFlowDataSourceConnect> CONNECT_LIST = CollUtil.newArrayList(
23+
new BaoMiDouDynamicDsConn(),
24+
new ShardingJdbcDsConn()
25+
);
26+
27+
public static void register() {
28+
ContextAware contextAware = ContextAwareHolder.loadContextAware();
29+
Map<String, LiteFlowDataSourceConnect> beanMap = contextAware.getBeansOfType(LiteFlowDataSourceConnect.class);
30+
Collection<LiteFlowDataSourceConnect> values = beanMap.values();
31+
CONNECT_LIST.addAll(values);
32+
33+
// 根据类名去重
34+
CONNECT_LIST = CollUtil.distinct(CONNECT_LIST, t -> t.getClass().getName(), true);
35+
}
36+
37+
public static Optional<LiteFlowDataSourceConnect> getConnect(SQLParserVO sqlParserVO) {
38+
return CONNECT_LIST.stream().filter(connect -> connect.filter(sqlParserVO)).findFirst();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.yomahub.liteflow.parser.sql.datasource.impl;
2+
3+
import cn.hutool.core.util.ClassLoaderUtil;
4+
import cn.hutool.core.util.StrUtil;
5+
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
6+
import com.yomahub.liteflow.exception.MissMavenDependencyException;
7+
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
8+
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
9+
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
10+
import com.yomahub.liteflow.spi.ContextAware;
11+
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
12+
13+
import javax.sql.DataSource;
14+
import java.sql.Connection;
15+
import java.util.Map;
16+
import java.util.Optional;
17+
18+
/**
19+
* 苞米豆动态数据源
20+
*
21+
* @author tkc
22+
* @since 2.12.5
23+
*/
24+
public class BaoMiDouDynamicDsConn implements LiteFlowDataSourceConnect {
25+
@Override
26+
public boolean filter(SQLParserVO config) {
27+
// 是否配置苞米豆动态数据源配置
28+
boolean configFlag = Optional.ofNullable(config.getBaomidou())
29+
.map(SQLParserVO.DataSourceConfig::getDataSourceName)
30+
.isPresent();
31+
if (!configFlag) {
32+
return false;
33+
}
34+
boolean classLoadFlag = ClassLoaderUtil.isPresent(Constant.LOAD_CLASS_NAME);
35+
if (!classLoadFlag) {
36+
throw new MissMavenDependencyException(Constant.MAVEN_GROUP_ID, Constant.MAVEN_ARTIFACT_ID);
37+
}
38+
return true;
39+
}
40+
41+
@Override
42+
public Connection getConn(SQLParserVO config) throws Exception {
43+
String dataSourceName = config.getBaomidou().getDataSourceName();
44+
ContextAware contextAware = ContextAwareHolder.loadContextAware();
45+
DynamicRoutingDataSource dynamicRoutingDataSource = contextAware.getBean(DynamicRoutingDataSource.class);
46+
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
47+
if (!dataSources.containsKey(dataSourceName)) {
48+
throw new ELSQLException(StrUtil.format("can not found {} datasource", dataSourceName));
49+
}
50+
51+
DataSource dataSource = dynamicRoutingDataSource.getDataSource(dataSourceName);
52+
return dataSource.getConnection();
53+
}
54+
55+
/**
56+
* 常量类
57+
*/
58+
public static class Constant {
59+
public static final String LOAD_CLASS_NAME = "com.baomidou.dynamic.datasource.DynamicRoutingDataSource";
60+
public static final String MAVEN_GROUP_ID = "com.baomidou";
61+
public static final String MAVEN_ARTIFACT_ID = "dynamic-datasource-spring-boot-starter";
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.yomahub.liteflow.parser.sql.datasource.impl;
2+
3+
import cn.hutool.core.util.ClassLoaderUtil;
4+
import com.yomahub.liteflow.exception.MissMavenDependencyException;
5+
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
6+
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
7+
import com.yomahub.liteflow.spi.ContextAware;
8+
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
9+
10+
import javax.sql.DataSource;
11+
import java.sql.Connection;
12+
import java.util.Optional;
13+
14+
/**
15+
* ShardingJdbc 动态数据源
16+
*
17+
* @author tkc
18+
* @since 2.12.5
19+
*/
20+
public class ShardingJdbcDsConn implements LiteFlowDataSourceConnect {
21+
22+
@Override
23+
public boolean filter(SQLParserVO config) {
24+
// 是否配置 sharding jdbc 动态数据源配置
25+
boolean configFlag = Optional.ofNullable(config.getShardingjdbc())
26+
.map(SQLParserVO.DataSourceConfig::getDataSourceName)
27+
.isPresent();
28+
29+
if (!configFlag) {
30+
return false;
31+
}
32+
boolean classLoadFlag = ClassLoaderUtil.isPresent(Constant.LOAD_CLASS_NAME);
33+
if (!classLoadFlag) {
34+
throw new MissMavenDependencyException(Constant.MAVEN_GROUP_ID, BaoMiDouDynamicDsConn.Constant.MAVEN_ARTIFACT_ID);
35+
}
36+
return true;
37+
}
38+
39+
@Override
40+
public Connection getConn(SQLParserVO config) throws Exception {
41+
ContextAware contextAware = ContextAwareHolder.loadContextAware();
42+
43+
return contextAware.getBean(DataSource.class).getConnection();
44+
}
45+
46+
47+
public static class Constant {
48+
public static final String LOAD_CLASS_NAME = "org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource";
49+
public static final String MAVEN_GROUP_ID = "org.apache.shardingsphere";
50+
public static final String MAVEN_ARTIFACT_ID = "sharding-jdbc-core";
51+
}
52+
}

0 commit comments

Comments
 (0)