mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
Merge remote-tracking branch 'upstream/dev' into instacne-spi
# Conflicts: # liteflow-rule-plugin/liteflow-rule-sql/src/main/java/com/yomahub/liteflow/parser/sql/vo/SQLParserVO.java
This commit is contained in:
@@ -25,5 +25,23 @@
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-crypto</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 苞米豆动态数据源 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring</artifactId>
|
||||
<version>${dynamic-datasource.version}</version>
|
||||
<optional>true</optional>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!--Sharding JDBC -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shardingsphere</groupId>
|
||||
<artifactId>sharding-jdbc-core</artifactId>
|
||||
<version>${sharding-jdbc.version}</version>
|
||||
<optional>true</optional>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -8,6 +8,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.core.FlowInitHook;
|
||||
import com.yomahub.liteflow.parser.constant.ReadType;
|
||||
import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteflowDataSourceConnectFactory;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.read.SqlReadFactory;
|
||||
import com.yomahub.liteflow.parser.sql.util.JDBCHelper;
|
||||
@@ -58,6 +59,9 @@ public class SQLXmlELParser extends ClassXmlFlowELParser {
|
||||
// 初始化 SqlReadFactory
|
||||
SqlReadFactory.registerRead(sqlParserVO);
|
||||
|
||||
// 初始化连接器
|
||||
LiteflowDataSourceConnectFactory.register();
|
||||
|
||||
// 注册轮询任务
|
||||
SqlReadFactory.registerSqlReadPollTask(ReadType.CHAIN);
|
||||
SqlReadFactory.registerSqlReadPollTask(ReadType.SCRIPT);
|
||||
@@ -92,7 +96,7 @@ public class SQLXmlELParser extends ClassXmlFlowELParser {
|
||||
* @param sqlParserVO sqlParserVO
|
||||
*/
|
||||
private void checkParserVO(SQLParserVO sqlParserVO) {
|
||||
if (sqlParserVO.isDefaultDataSource()) {
|
||||
if (sqlParserVO.isAutoFoundDataSource()) {
|
||||
return;
|
||||
}
|
||||
if (StrUtil.isEmpty(sqlParserVO.getUrl())) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource;
|
||||
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* 数据源获取接口
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public interface LiteFlowDataSourceConnect {
|
||||
|
||||
/**
|
||||
* 检查是否支持该数据源
|
||||
*/
|
||||
boolean filter(SQLParserVO config);
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*/
|
||||
Connection getConn(SQLParserVO config) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource;
|
||||
|
||||
import com.yomahub.liteflow.parser.sql.datasource.impl.BaoMiDouDynamicDsConn;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.impl.DefaultLiteFlowJdbcConn;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.impl.LiteFlowAutoLookUpJdbcConn;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.impl.ShardingJdbcDsConn;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.ContextAware;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* 数据源获取接口工厂
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public class LiteflowDataSourceConnectFactory {
|
||||
private static final List<LiteFlowDataSourceConnect> CONNECT_LIST = new CopyOnWriteArrayList<>();
|
||||
private static final Logger LOG = LoggerFactory.getLogger(LiteflowDataSourceConnectFactory.class);
|
||||
|
||||
public static void register() {
|
||||
ContextAware contextAware = ContextAwareHolder.loadContextAware();
|
||||
Map<String, LiteFlowDataSourceConnect> beanMap = contextAware.getBeansOfType(LiteFlowDataSourceConnect.class);
|
||||
Collection<LiteFlowDataSourceConnect> values = beanMap.values();
|
||||
|
||||
// 清空原有的列表
|
||||
CONNECT_LIST.clear();
|
||||
// 将自定义的放在最前面
|
||||
CONNECT_LIST.addAll(values);
|
||||
|
||||
// 内置的几种处理器
|
||||
CONNECT_LIST.add(new DefaultLiteFlowJdbcConn());
|
||||
CONNECT_LIST.add(new BaoMiDouDynamicDsConn());
|
||||
CONNECT_LIST.add(new ShardingJdbcDsConn());
|
||||
|
||||
// 自动查找放在最后,这个用于兜底处理,这个里面如果找不到,会直接抛出异常
|
||||
CONNECT_LIST.add(new LiteFlowAutoLookUpJdbcConn());
|
||||
}
|
||||
|
||||
public static Optional<LiteFlowDataSourceConnect> getConnect(SQLParserVO config) {
|
||||
for (LiteFlowDataSourceConnect dataSourceConnect : CONNECT_LIST) {
|
||||
if (dataSourceConnect.filter(config)) {
|
||||
LOG.debug("use lite-flow-data-source-connect: {}", dataSourceConnect.getClass().getName());
|
||||
return Optional.of(dataSourceConnect);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource.impl;
|
||||
|
||||
import cn.hutool.core.util.ClassLoaderUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.yomahub.liteflow.exception.MissMavenDependencyException;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.ContextAware;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 苞米豆动态数据源
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public class BaoMiDouDynamicDsConn implements LiteFlowDataSourceConnect {
|
||||
@Override
|
||||
public boolean filter(SQLParserVO config) {
|
||||
// 是否配置苞米豆动态数据源配置
|
||||
boolean configFlag = Optional.ofNullable(config.getBaomidou())
|
||||
.map(SQLParserVO.DataSourceConfig::getDataSourceName)
|
||||
.isPresent();
|
||||
if (!configFlag) {
|
||||
return false;
|
||||
}
|
||||
boolean classLoadFlag = ClassLoaderUtil.isPresent(Constant.LOAD_CLASS_NAME);
|
||||
if (!classLoadFlag) {
|
||||
throw new MissMavenDependencyException(Constant.MAVEN_GROUP_ID, Constant.MAVEN_ARTIFACT_ID);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConn(SQLParserVO config) throws Exception {
|
||||
String dataSourceName = config.getBaomidou().getDataSourceName();
|
||||
ContextAware contextAware = ContextAwareHolder.loadContextAware();
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = contextAware.getBean(DynamicRoutingDataSource.class);
|
||||
Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
||||
if (!dataSources.containsKey(dataSourceName)) {
|
||||
throw new ELSQLException(StrUtil.format("can not found {} datasource", dataSourceName));
|
||||
}
|
||||
|
||||
DataSource dataSource = dynamicRoutingDataSource.getDataSource(dataSourceName);
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* 常量类
|
||||
*/
|
||||
public static class Constant {
|
||||
public static final String LOAD_CLASS_NAME = "com.baomidou.dynamic.datasource.DynamicRoutingDataSource";
|
||||
public static final String MAVEN_GROUP_ID = "com.baomidou";
|
||||
public static final String MAVEN_ARTIFACT_ID = "dynamic-datasource-spring-boot-starter";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource.impl;
|
||||
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
/**
|
||||
* lite flow 默认的数据源连接
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public class DefaultLiteFlowJdbcConn implements LiteFlowDataSourceConnect {
|
||||
@Override
|
||||
public boolean filter(SQLParserVO config) {
|
||||
return config.isUseJdbcConn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConn(SQLParserVO config) throws Exception {
|
||||
String url = config.getUrl();
|
||||
String username = config.getUsername();
|
||||
String password = config.getPassword();
|
||||
|
||||
return DriverManager.getConnection(url, username, password);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource.impl;
|
||||
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.util.LiteFlowJdbcUtil;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* lite flow 自动查找连接
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public class LiteFlowAutoLookUpJdbcConn implements LiteFlowDataSourceConnect {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(LiteFlowAutoLookUpJdbcConn.class);
|
||||
|
||||
@Override
|
||||
public boolean filter(SQLParserVO config) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConn(SQLParserVO config) throws Exception {
|
||||
return DataSourceBeanNameHolder.autoLookUpConn(config);
|
||||
}
|
||||
|
||||
public static class DataSourceBeanNameHolder {
|
||||
private static String DATA_SOURCE_NAME = null;
|
||||
|
||||
public static synchronized void init(String dataSourceName) {
|
||||
if (DATA_SOURCE_NAME == null) {
|
||||
DATA_SOURCE_NAME = dataSourceName;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDataSourceName() {
|
||||
return DATA_SOURCE_NAME;
|
||||
}
|
||||
|
||||
public static boolean isNotInit() {
|
||||
return DATA_SOURCE_NAME == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动查找可用数据源
|
||||
*/
|
||||
public static Connection autoLookUpConn(SQLParserVO sqlParserVO) throws SQLException {
|
||||
Connection connection;
|
||||
Map<String, DataSource> dataSourceMap = ContextAwareHolder.loadContextAware().getBeansOfType(DataSource.class);
|
||||
if (LiteFlowJdbcUtil.DataSourceBeanNameHolder.isNotInit()) {
|
||||
synchronized (LiteFlowJdbcUtil.DataSourceBeanNameHolder.class) {
|
||||
if (LiteFlowJdbcUtil.DataSourceBeanNameHolder.isNotInit()) {
|
||||
String executeSql = LiteFlowJdbcUtil.buildCheckSql(sqlParserVO);
|
||||
// 遍历数据源,多数据源场景下,判断哪个数据源有 liteflow 配置
|
||||
for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
|
||||
String dataSourceName = entry.getKey();
|
||||
DataSource dataSource = entry.getValue();
|
||||
|
||||
if (LiteFlowJdbcUtil.checkConnectionCanExecuteSql(dataSource.getConnection(), executeSql)) {
|
||||
// 找到数据源名称后,将其缓存起来,下次使用就不再寻找
|
||||
LiteFlowJdbcUtil.DataSourceBeanNameHolder.init(dataSourceName);
|
||||
|
||||
LOG.info("use dataSourceName[{}],has found liteflow config", dataSourceName);
|
||||
break;
|
||||
} else {
|
||||
LOG.info("check dataSourceName[{}],but not has liteflow config", dataSourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
DataSource dataSource = Optional.ofNullable(LiteFlowJdbcUtil.DataSourceBeanNameHolder.getDataSourceName())
|
||||
.map(dataSourceMap::get)
|
||||
.orElse(null);
|
||||
if (dataSource == null) {
|
||||
throw new ELSQLException("can not found liteflow config in dataSourceName " + dataSourceMap.keySet());
|
||||
}
|
||||
connection = dataSource.getConnection();
|
||||
if (connection == null) {
|
||||
throw new ELSQLException("can not found liteflow config in dataSourceName " + dataSourceMap.keySet());
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.yomahub.liteflow.parser.sql.datasource.impl;
|
||||
|
||||
import cn.hutool.core.util.ClassLoaderUtil;
|
||||
import com.yomahub.liteflow.exception.MissMavenDependencyException;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.ContextAware;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* ShardingJdbc 动态数据源
|
||||
*
|
||||
* @author tkc
|
||||
* @since 2.12.5
|
||||
*/
|
||||
public class ShardingJdbcDsConn implements LiteFlowDataSourceConnect {
|
||||
|
||||
@Override
|
||||
public boolean filter(SQLParserVO config) {
|
||||
// 是否配置 sharding jdbc 动态数据源配置
|
||||
boolean configFlag = Optional.ofNullable(config.getShardingjdbc())
|
||||
.map(SQLParserVO.DataSourceConfig::getDataSourceName)
|
||||
.isPresent();
|
||||
|
||||
if (!configFlag) {
|
||||
return false;
|
||||
}
|
||||
boolean classLoadFlag = ClassLoaderUtil.isPresent(Constant.LOAD_CLASS_NAME);
|
||||
if (!classLoadFlag) {
|
||||
throw new MissMavenDependencyException(Constant.MAVEN_GROUP_ID, Constant.MAVEN_ARTIFACT_ID);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConn(SQLParserVO config) throws Exception {
|
||||
ContextAware contextAware = ContextAwareHolder.loadContextAware();
|
||||
|
||||
return contextAware.getBean(DataSource.class).getConnection();
|
||||
}
|
||||
|
||||
|
||||
public static class Constant {
|
||||
public static final String LOAD_CLASS_NAME = "org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource";
|
||||
public static final String MAVEN_GROUP_ID = "org.apache.shardingsphere";
|
||||
public static final String MAVEN_ARTIFACT_ID = "sharding-jdbc-core";
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.yomahub.liteflow.parser.sql.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteFlowDataSourceConnect;
|
||||
import com.yomahub.liteflow.parser.sql.datasource.LiteflowDataSourceConnectFactory;
|
||||
import com.yomahub.liteflow.parser.sql.exception.ELSQLException;
|
||||
import com.yomahub.liteflow.parser.sql.vo.SQLParserVO;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
@@ -10,6 +12,7 @@ import org.slf4j.LoggerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.*;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LiteFlowJdbcUtil {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(LiteFlowJdbcUtil.class);
|
||||
@@ -17,44 +20,27 @@ public class LiteFlowJdbcUtil {
|
||||
|
||||
/**
|
||||
* 获取链接
|
||||
* 此方法会根据配置,判读使用指定数据源,还是IOC容器中已有的数据源
|
||||
* 此方法会从多个角度查找数据源,优先级从上到下
|
||||
* 1. 自定义连接器获取,实现了 DataBaseConnect 接口
|
||||
* 2. 没有配置数据源连接配置,判断标准参考 SqlParserVO#isDefaultDataSource(),自动寻找IOC容器中已有的数据源
|
||||
* 3. 使用数据源配置,使用 jdbc 创建连接
|
||||
*
|
||||
* @param sqlParserVO sql解析器参数
|
||||
* @return 返回数据库连接
|
||||
*/
|
||||
public static Connection getConn(SQLParserVO sqlParserVO) {
|
||||
Connection connection = null;
|
||||
String url = sqlParserVO.getUrl();
|
||||
String username = sqlParserVO.getUsername();
|
||||
String password = sqlParserVO.getPassword();
|
||||
|
||||
try {
|
||||
// 如果不配置 jdbc 连接相关配置,代表使用项目数据源
|
||||
if (sqlParserVO.isDefaultDataSource()) {
|
||||
String executeSql = buildCheckSql(sqlParserVO);
|
||||
Map<String, DataSource> dataSourceMap = ContextAwareHolder.loadContextAware().getBeansOfType(DataSource.class);
|
||||
// 遍历数据源,多数据源场景下,判断哪个数据源有 liteflow 配置
|
||||
for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
|
||||
String dataSourceName = entry.getKey();
|
||||
DataSource dataSource = entry.getValue();
|
||||
|
||||
if (checkConnectionCanExecuteSql(dataSource.getConnection(), executeSql)) {
|
||||
connection = dataSource.getConnection();
|
||||
LOG.info("use dataSourceName[{}],has found liteflow config", dataSourceName);
|
||||
break;
|
||||
} else {
|
||||
LOG.info("check dataSourceName[{}],but not has liteflow config", dataSourceName);
|
||||
}
|
||||
}
|
||||
if (connection == null) {
|
||||
throw new ELSQLException("can not found liteflow config in dataSourceName " + dataSourceMap.keySet());
|
||||
}
|
||||
// 如果指定连接查找器,就使用连接查找器获取连接
|
||||
Optional<LiteFlowDataSourceConnect> connectOpt = LiteflowDataSourceConnectFactory.getConnect(sqlParserVO);
|
||||
if (connectOpt.isPresent()) {
|
||||
connection = connectOpt.get().getConn(sqlParserVO);
|
||||
} else {
|
||||
// 理论上这里不会走,因为最后一个连接查找器 LiteFlowAutoLookUpJdbcConn 没找到会抛出异常的
|
||||
// 这里是一个兜底,理论上不会走
|
||||
throw new ELSQLException("can not found connect by liteflow config");
|
||||
}
|
||||
// 如果配置 jdbc 连接相关配置,代表使用指定链接信息
|
||||
else {
|
||||
connection = DriverManager.getConnection(url, username, password);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new ELSQLException(e);
|
||||
}
|
||||
@@ -62,6 +48,7 @@ public class LiteFlowJdbcUtil {
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断连接是否可以执行指定 sql
|
||||
*
|
||||
@@ -123,10 +110,69 @@ public class LiteFlowJdbcUtil {
|
||||
* @param sqlParserVO sql解析器参数
|
||||
* @return 返回组合完成的检查sql
|
||||
*/
|
||||
private static String buildCheckSql(SQLParserVO sqlParserVO) {
|
||||
public static String buildCheckSql(SQLParserVO sqlParserVO) {
|
||||
String chainTableName = sqlParserVO.getChainTableName();
|
||||
String elDataField = sqlParserVO.getElDataField();
|
||||
String chainNameField = sqlParserVO.getChainNameField();
|
||||
return StrUtil.format(CHECK_SQL_PATTERN, chainNameField, elDataField, chainTableName);
|
||||
}
|
||||
|
||||
public static class DataSourceBeanNameHolder {
|
||||
private static String DATA_SOURCE_NAME = null;
|
||||
|
||||
public static synchronized void init(String dataSourceName) {
|
||||
if (DATA_SOURCE_NAME == null) {
|
||||
DATA_SOURCE_NAME = dataSourceName;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDataSourceName() {
|
||||
return DATA_SOURCE_NAME;
|
||||
}
|
||||
|
||||
public static boolean isNotInit() {
|
||||
return DATA_SOURCE_NAME == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动查找可用数据源
|
||||
*/
|
||||
public static Connection autoLookUpConn(SQLParserVO sqlParserVO) throws SQLException {
|
||||
Connection connection;
|
||||
Map<String, DataSource> dataSourceMap = ContextAwareHolder.loadContextAware().getBeansOfType(DataSource.class);
|
||||
if (DataSourceBeanNameHolder.isNotInit()) {
|
||||
synchronized (DataSourceBeanNameHolder.class) {
|
||||
if (DataSourceBeanNameHolder.isNotInit()) {
|
||||
String executeSql = buildCheckSql(sqlParserVO);
|
||||
// 遍历数据源,多数据源场景下,判断哪个数据源有 liteflow 配置
|
||||
for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
|
||||
String dataSourceName = entry.getKey();
|
||||
DataSource dataSource = entry.getValue();
|
||||
|
||||
if (checkConnectionCanExecuteSql(dataSource.getConnection(), executeSql)) {
|
||||
// 找到数据源名称后,将其缓存起来,下次使用就不再寻找
|
||||
DataSourceBeanNameHolder.init(dataSourceName);
|
||||
|
||||
LOG.info("use dataSourceName[{}],has found liteflow config", dataSourceName);
|
||||
break;
|
||||
} else {
|
||||
LOG.info("check dataSourceName[{}],but not has liteflow config", dataSourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
DataSource dataSource = Optional.ofNullable(DataSourceBeanNameHolder.getDataSourceName())
|
||||
.map(dataSourceMap::get)
|
||||
.orElse(null);
|
||||
if (dataSource == null) {
|
||||
throw new ELSQLException("can not found liteflow config in dataSourceName " + dataSourceMap.keySet());
|
||||
}
|
||||
connection = dataSource.getConnection();
|
||||
if (connection == null) {
|
||||
throw new ELSQLException("can not found liteflow config in dataSourceName " + dataSourceMap.keySet());
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,6 +166,16 @@ public class SQLParserVO {
|
||||
*/
|
||||
private Boolean sqlLogEnabled = true;
|
||||
|
||||
/**
|
||||
* 苞米豆动态数据源配置
|
||||
*/
|
||||
private DataSourceConfig baomidou;
|
||||
|
||||
/**
|
||||
* sharding jdbc 动态数据源配置
|
||||
*/
|
||||
private DataSourceConfig shardingjdbc;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
@@ -295,10 +305,23 @@ public class SQLParserVO {
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断配资是否使用 IOC 已有数据源
|
||||
* 判断是否自动查找数据源
|
||||
*/
|
||||
public boolean isDefaultDataSource() {
|
||||
return StrUtil.isBlank(url) && StrUtil.isBlank(username) && StrUtil.isBlank(password) && StrUtil.isBlank(driverClassName);
|
||||
public boolean isAutoFoundDataSource() {
|
||||
return StrUtil.isBlank(url) &&
|
||||
StrUtil.isBlank(username) &&
|
||||
StrUtil.isBlank(password) &&
|
||||
StrUtil.isBlank(driverClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否使用jdbc连接
|
||||
*/
|
||||
public boolean isUseJdbcConn(){
|
||||
return StrUtil.isNotBlank(url) &&
|
||||
StrUtil.isNotBlank(username) &&
|
||||
StrUtil.isNotBlank(password) &&
|
||||
StrUtil.isNotBlank(driverClassName);
|
||||
}
|
||||
|
||||
public Boolean getPollingEnabled() {
|
||||
@@ -427,4 +450,32 @@ public class SQLParserVO {
|
||||
public void setGroupKeyInstanceIdField(String groupKeyInstanceIdField) {
|
||||
this.groupKeyInstanceIdField = groupKeyInstanceIdField;
|
||||
}
|
||||
|
||||
public DataSourceConfig getBaomidou() {
|
||||
return baomidou;
|
||||
}
|
||||
|
||||
public void setBaomidou(DataSourceConfig baomidou) {
|
||||
this.baomidou = baomidou;
|
||||
}
|
||||
|
||||
public DataSourceConfig getShardingjdbc() {
|
||||
return shardingjdbc;
|
||||
}
|
||||
|
||||
public void setShardingjdbc(DataSourceConfig shardingjdbc) {
|
||||
this.shardingjdbc = shardingjdbc;
|
||||
}
|
||||
|
||||
public static class DataSourceConfig {
|
||||
private String dataSourceName;
|
||||
|
||||
public String getDataSourceName() {
|
||||
return dataSourceName;
|
||||
}
|
||||
|
||||
public void setDataSourceName(String dataSourceName) {
|
||||
this.dataSourceName = dataSourceName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user