feat:规则引擎(数据源初始化)

master_fei
Yunfei Du 2024-05-15 18:29:54 +08:00
parent df32e7d7eb
commit a48cd106b6
14 changed files with 421 additions and 18 deletions

View File

@ -141,6 +141,11 @@
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.etl</groupId>
<artifactId>etl-common</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>etl-common-data-standard</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.etl</groupId>
<artifactId>etl-common-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,44 @@
package com.etl.conmon.data.standard.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @author YunFei.Du
* @date 14:16 2024/5/15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DataModel {
/**
*
*/
private String key;
/**
*
*/
private Object value;
/**
* -
*/
private String sourceType;
/**
* -
*/
private String processType;
/**
*
*/
private Class<?> processClass;
}

View File

@ -0,0 +1,38 @@
package com.etl.conmon.data.standard.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @author YunFei.Du
* @date 14:17 2024/5/15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DataSetModel {
// [[DataModel,DataModel,DataModel],[DataModel,DataModel,DataModel]]
private DataSetModel[] dataSetModel = null;
private int setLength;
public static DataSetModel build(int dataSetModelLength){
return build ( new DataSetModel[dataSetModelLength] );
}
public static DataSetModel build(DataSetModel[] dataSetModel){
return DataSetModel.builder ()
.dataSetModel(dataSetModel)
.setLength ( dataSetModel.length )
.build ();
}
}

View File

@ -0,0 +1,10 @@
package com.etl.conmon.data.standard.model;
/**
*
* @author YunFei.Du
* @date 14:17 2024/5/15
*/
public interface DataStandard {
}

View File

@ -0,0 +1,78 @@
package com.etl.conmon.data.standard.model;
import com.etl.conmon.data.standard.utils.EtlUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Arrays;
import java.util.function.IntFunction;
/**
*
* @author YunFei.Du
* @date 14:17 2024/5/15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RecordModel {
// [DataModel,DataModel,DataModel]
/**
* key ([] )
*/
private String[] keys;
/**
*
*/
private DataModel[] keyForValue;
/**
*
*/
private DataModel[] dataModelArr = null;
/**
*
*/
private int recodeLength;
/**
* RecordModel
* build(int dataModelLength, String[] keys)便RecordModel
* dataModelLengthkeys
*
* @param dataModelLength RecordModel
* @param keys RecordModel访
* @return RecordModel
*/
public static RecordModel build(int dataModelLength,String[] keys){
// 使用提供的数据模型长度和键数组来构建RecordModel
return build ( new DataModel[dataModelLength] , keys);
}
/**
*
*
* @param dataModelArr
* @param keys
* @return
*/
public static RecordModel build(DataModel[] dataModelArr, String[] keys){
return RecordModel.builder()
.dataModelArr ( dataModelArr )
.recodeLength ( dataModelArr.length )
.keys( keys)
// 筛选数据模型数组中键值匹配给定键数组的元素并将其设置为keyForValue
.keyForValue(
Arrays.stream (dataModelArr).filter ( dataModel -> EtlUtils.valAsArr ( keys , dataModel.getKey () ) )
.toArray ( value -> new DataModel[0] )
)
.build ();
}
}

View File

@ -0,0 +1,21 @@
package com.etl.conmon.data.standard.utils;
/**
* @ClassName EtlUtils
* @Description
* @Author YunFei.Du
* @Date 2024/5/15 14:28
*/
public class EtlUtils {
public static boolean valAsArr(String[] keys,String key ){
for (String s : keys) {
if (s.equals ( key )){
return true;
}
}
return false;
}
}

View File

@ -18,6 +18,7 @@
<module>etl-common-datascope</module>
<module>etl-common-datasource</module>
<module>etl-common-system</module>
<module>etl-common-data-standard</module>
</modules>
<artifactId>etl-common</artifactId>

View File

@ -1,6 +1,8 @@
package com.etl.data.client.config;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.etl.data.client.connPool.service.ConnPoolManagementService;
import com.etl.data.client.jdbcUtils.JDBCConcreteClass;
import com.etl.data.domain.DataSource;
import com.etl.data.domain.dataSource.DataSourceConfig;
@ -15,6 +17,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.util.List;
import java.util.Map;
/**
* 访
@ -32,25 +35,26 @@ public class DataAccessClientRunner implements ApplicationRunner {
public void run(ApplicationArguments args) {
List< DataSource > dataSourceList = remoteDataSourceService.getDataSourceList ( new DataSourceQueryReq ( ) ).getData ( ).getRows ( );
if (!dataSourceList.isEmpty ()){
ConnPoolManagementService.init ( dataSourceList );
try {
dataSourceList.stream ( ).forEach ( dataSource -> {
log.info ( "dataSource:{}", dataSource );
DataSourceConfig.init ( dataSource );
} );
String key = dataSourceList.get ( 0 ).getName ( ) + dataSourceList.get ( 0 ).getId ( );
DataSourceConfig.getNum ( key );
Connection connection = DataSourceConfig.getConnection ( key );
JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass ( );
PreparedStatement preparedStatement = jdbcConcreteClass.getPreparedStatement ( connection, "select * from car" );
ResultSetMetaData rsd = preparedStatement.getMetaData ( );
for (int i = 1; i <= rsd.getColumnCount ( ); i++) {
log.info ( "类型:{}", rsd.getColumnClassName ( i ) );
}
DataSourceConfig.close ( connection );
DataSourceConfig.getNum ( key );
DruidPooledConnection druidPooledConnection = ConnPoolManagementService.get ( dataSourceList.get ( 0 ).getKey ( ) );
// Map< DruidPooledConnection, String > connToKey = ConnPoolManagementService.getConnToKey ( );
log.info ( "connToKey:{}", druidPooledConnection );
// JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass ( );
// PreparedStatement preparedStatement = jdbcConcreteClass.getPreparedStatement ( connection, "select * from car" );
// ResultSetMetaData rsd = preparedStatement.getMetaData ( );
// for (int i = 1; i <= rsd.getColumnCount ( ); i++) {
// log.info ( "类型:{}", rsd.getColumnClassName ( i ) );
// }
ConnPoolManagementService.returnConnection ( druidPooledConnection );
} catch (Exception e) {
log.error ( "数据访问客户端运行程序异常:{}", e.getMessage ( ) );
throw new RuntimeException ( e );
}
}
}
}

View File

@ -0,0 +1,53 @@
package com.etl.data.client.connPool.pool;
import com.alibaba.druid.pool.DruidDataSource;
import com.etl.common.core.exception.ServiceException;
import com.etl.common.core.utils.StringUtils;
import com.etl.common.security.utils.SecurityUtils;
import lombok.extern.log4j.Log4j2;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author YunFei.Du
* @date 15:04 2024/5/15
*/
@Log4j2
public class ConnPoolContext {
/**
*
*/
private ConnPoolContext(){
}
private final static ConcurrentHashMap<String, DruidDataSource> connPoolContext
= new ConcurrentHashMap<>(16);
public static void setConnection(String key,DruidDataSource druidDataSource){
if (connPoolContext.containsKey(key)){
throw new ServiceException (
StringUtils.format ( "连接池key:{} 已存在" )
);
}
connPoolContext.put(key,druidDataSource);
}
/**
* key
* @param key
* @return
*/
public static DruidDataSource getConnection(String key){
return connPoolContext.get ( key );
}
public static void remove(String key){
try (DruidDataSource druidDataSource = connPoolContext.remove ( key )) {
druidDataSource.close ();
}catch (RuntimeException e){
log.warn ( "关闭连接池失败:[{}]-- [{}]", key,e.getMessage (),e );
}
}
}

View File

@ -0,0 +1,114 @@
package com.etl.data.client.connPool.service;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.etl.data.client.connPool.pool.ConnPoolContext;
import com.etl.data.domain.DataSource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import static com.etl.data.client.connPool.pool.ConnPoolContext.getConnection;
/**
* @ClassName ConnPollManagementService
* @Description
* @Author YunFei.Du
* @Date 2024/5/15 15:14
*/
@Component
@Log4j2
public class ConnPoolManagementService {
private final static ThreadLocal< Map<DruidPooledConnection,String> > connToKey
= new ThreadLocal<> ( );
public static Map<DruidPooledConnection,String> getConnToKey() {
Map<DruidPooledConnection,String> dataMap=connToKey.get ();
return dataMap;
}
/**
*
*/
public static void init(List< DataSource> dataSourceList) {
for (DataSource dataSource : dataSourceList) {
createPool ( dataSource );
}
}
/**
*
*/
public static void createPool(DataSource dataSource){
DruidDataSource druidDataSource = new DruidDataSource ( );
druidDataSource.setUsername ( dataSource.getUsername ( ) );
druidDataSource.setPassword ( dataSource.getPassword ( ) );
druidDataSource.setUrl ( "jdbc:mysql://" + dataSource.getHost ( ) + ":" + dataSource.getPort ( ) + "/" + dataSource.getDatabaseName ( ) );
druidDataSource.setDriverClassName ( dataSource.getJdbcDriver ( ) );
// 设置初始化连接数和最小空闲连接数
druidDataSource.setInitialSize ( Integer.valueOf ( dataSource.getInitNum ( ) ) );
druidDataSource.setMinIdle ( Integer.valueOf ( dataSource.getInitNum ( ) ) );
// 设置最大活动连接数
druidDataSource.setMaxActive ( Integer.valueOf ( dataSource.getMaxNum ( ) ) );
ConnPoolContext.setConnection ( dataSource.getKey (), druidDataSource );
try {
druidDataSource.init ();
} catch (SQLException e) {
log.error ( "新增数据源失败" );
throw new RuntimeException ( e );
}
;
}
/**
*
*/
public static DruidPooledConnection get(DataSource dataSource){
return get ( dataSource.getKey () );
}
/**
*
*/
public static DruidPooledConnection get(String key){
DruidPooledConnection connection=null;
DruidDataSource druidDataSource = getConnection (key );
try {
connection = druidDataSource.getConnection ( );
return connection;
} catch (SQLException e) {
log.warn ( "获取连接异常:[{}] - [{}]",e.getMessage (),e );
throw new RuntimeException ( e );
}finally {
getConnToKey ().put ( connection,key );
}
}
/**
*
* @param connection
*/
public static void returnConnection(DruidPooledConnection connection) {
try {
connection.close ();
} catch (SQLException e) {
log.warn ( "获取连接异常:[{}] - [{}]",getConnToKey().get ( connection ),e.getMessage () );
throw new RuntimeException ( e );
}finally {
getConnToKey ().remove ( connection );
}
}
public static void remove(DataSource dataSource) {
}
}

View File

@ -95,4 +95,9 @@ public class DataSource extends BaseEntity {
*
*/
private String modeName;
public String getKey(){
return this.name+"_"+this.systemName+"_"+this.id;
}
}

View File

@ -25,15 +25,15 @@ public class DataSourceController extends BaseController {
@Autowired
private DataSourceService dataSourceService;
public static ThreadLocal< String > local = new ThreadLocal<> ( );
/**
*
*/
@PostMapping("/list")
public Result< TableDataInfo<DataSource> > getDataSourceList (@RequestBody DataSourceQueryReq req) {
startPage();
ThreadLocal< String > local = new ThreadLocal<> ( );
String s = local.get ( );
System.out.println ("asd"+s );
local.set ( "asdf" );
List<DataSource> list = dataSourceService.selectDataSourceList(req);
return getDataAsset (list);
}

View File

@ -8,6 +8,7 @@ import com.etl.common.core.utils.StringUtils;
import com.etl.common.security.utils.SecurityUtils;
import com.etl.common.system.domain.SysRole;
import com.etl.data.controller.DataSourceController;
import com.etl.data.domain.*;
import com.etl.data.domain.Dictionary;
import com.etl.data.domain.custom.Statistics;
@ -65,6 +66,8 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
public List< DataSource > selectDataSourceList(DataSourceQueryReq req) {
List< DataSource > dataSources = this.list ( );
String s = DataSourceController.local.get ( );
// List<DataSource> dataSourceList = new ArrayList<DataSource>();
// List<SysRole> roles = SecurityUtils.getLoginUser().getSysUser().getRoles();
// //判断登录人是否为管理员,不是则需要过滤掉未授权的信息