fix:完善规则引擎版本,使用连接池
parent
72ccb2bc0f
commit
40c9516a49
|
@ -28,5 +28,10 @@
|
||||||
<version>3.6.3</version>
|
<version>3.6.3</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>muyu-data-source-common</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.muyu.data.source.client.config;
|
package com.muyu.data.source.client.config;
|
||||||
|
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.data.source.client.jdbcUtils.JDBCConcreteClass;
|
||||||
|
import com.muyu.data.source.config.DataSourceConfig;
|
||||||
import com.muyu.data.source.domain.Children;
|
import com.muyu.data.source.domain.Children;
|
||||||
import com.muyu.data.source.domain.DataSource;
|
import com.muyu.data.source.domain.DataSource;
|
||||||
import com.muyu.data.source.domain.DatabaseType;
|
import com.muyu.data.source.domain.DatabaseType;
|
||||||
|
@ -23,60 +25,66 @@ import org.springframework.context.annotation.Import;
|
||||||
/**
|
/**
|
||||||
* 数据接入客户端配置类
|
* 数据接入客户端配置类
|
||||||
*
|
*
|
||||||
* @author CHX
|
* @author CHX on 2024/5/9 星期四
|
||||||
* on 2024/5/9 星期四
|
|
||||||
*/
|
*/
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
@Import(value = {SourceClientRunner.class})
|
@Import(value = {SourceClientRunner.class})
|
||||||
public class SourceClientConfig {
|
public class SourceClientConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RemoteDataSourceService remoteDataSourceService;
|
private RemoteDataSourceService remoteDataSourceService;
|
||||||
@Autowired
|
|
||||||
private RemoteDataTypeService remoteDataTypeService;
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RemoteChildrenService remoteChildrenService;
|
private RemoteChildrenService remoteChildrenService;
|
||||||
|
|
||||||
|
|
||||||
public List<List<DataModel>> getDataModel(Long id) {
|
public List<List<DataModel>> getDataModel(Long id) {
|
||||||
List<List<DataModel>> list = new ArrayList<>();
|
List<List<DataModel>> list = new ArrayList<>();
|
||||||
|
//根据表id查询表结构对象
|
||||||
Result<Children> childrenResult = remoteChildrenService.selectChildren(id);
|
Result<Children> childrenResult = remoteChildrenService.selectChildren(id);
|
||||||
Children children = childrenResult.getData();
|
Children children = childrenResult.getData();
|
||||||
|
//根据数据源id查询数据源对象
|
||||||
Result<DataSource> dataSourceResult = remoteDataSourceService.getDataSourceById(children.getAssetId());
|
Result<DataSource> dataSourceResult = remoteDataSourceService.getDataSourceById(children.getAssetId());
|
||||||
DataSource dataSource = dataSourceResult.getData();
|
DataSource dataSource = dataSourceResult.getData();
|
||||||
Result<DatabaseType> databaseTypeResult = remoteDataTypeService.getDataType(dataSource.getDataType());
|
//sql
|
||||||
DatabaseType databaseType = databaseTypeResult.getData();
|
|
||||||
String jdbcUrl=databaseType.getUrlPre()+dataSource.getHost()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName()+"?"+dataSource.getConnectionParam();
|
|
||||||
String sql = "";
|
String sql = "";
|
||||||
if ("mysql".equals(dataSource.getDataType())){
|
switch (dataSource.getDataType()) {
|
||||||
|
case "mysql":
|
||||||
sql = "select * from" + children.getName();
|
sql = "select * from" + children.getName();
|
||||||
|
break;
|
||||||
|
case "postgresql":
|
||||||
|
sql="select * from "+dataSource.getDatabaseName()+"."+children.getName();
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Class.forName(databaseType.getDriverManager());
|
//调用连接池,获取连接
|
||||||
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
// 调用连接池 获取连接
|
||||||
PreparedStatement ps = connection.prepareStatement(sql);
|
Connection connection = DataSourceConfig.getConnection(dataSource.getId());
|
||||||
ResultSet rs = ps.executeQuery();
|
JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass();
|
||||||
|
// 调用抽象类 获取jdbc查询结果
|
||||||
|
ResultSet rs = jdbcConcreteClass.getResultSet(connection, sql);
|
||||||
ResultSetMetaData metaData = rs.getMetaData();
|
ResultSetMetaData metaData = rs.getMetaData();
|
||||||
List<DataModel> dataModelList = new ArrayList<>();
|
List<DataModel> dataModelList = new ArrayList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||||
|
// 字段名称
|
||||||
String columnName = metaData.getColumnName(i);
|
String columnName = metaData.getColumnName(i);
|
||||||
|
// 字段值
|
||||||
Object value = rs.getObject(i);
|
Object value = rs.getObject(i);
|
||||||
|
// 字段类型
|
||||||
String columnTypeName = metaData.getColumnTypeName(i);
|
String columnTypeName = metaData.getColumnTypeName(i);
|
||||||
|
// 字段java映射类型
|
||||||
String columnClassName = metaData.getColumnClassName(i);
|
String columnClassName = metaData.getColumnClassName(i);
|
||||||
DataModel dataModel = DataModel.builder()
|
DataModel dataModel = DataModel.builder().key(columnName).value(value).sourceType(columnTypeName)
|
||||||
.key(columnName)
|
.processType(columnClassName).processClass(Class.forName(columnClassName)).build();
|
||||||
.value(value)
|
|
||||||
.sourceType(columnTypeName)
|
|
||||||
.processType(columnClassName)
|
|
||||||
.processClass(Class.forName(columnClassName))
|
|
||||||
.build();
|
|
||||||
dataModelList.add(dataModel);
|
dataModelList.add(dataModel);
|
||||||
}
|
}
|
||||||
list.add(dataModelList);
|
list.add(dataModelList);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// 归还连接
|
||||||
|
DataSourceConfig.returnConn(connection);
|
||||||
} catch (ClassNotFoundException | SQLException e) {
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,25 +43,5 @@ public class SourceClientRunner implements ApplicationRunner {
|
||||||
DatabaseType databaseType = databaseTypeResult.getData();
|
DatabaseType databaseType = databaseTypeResult.getData();
|
||||||
DataSourceConfig.init(dataSource,databaseType);
|
DataSourceConfig.init(dataSource,databaseType);
|
||||||
});
|
});
|
||||||
// Long key = dataSourceList.get(0).getId();
|
|
||||||
// log.info("查看连接池");
|
|
||||||
// DataSourceConfig.size(key);
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// log.info("取出一个连接,查看连接池");
|
|
||||||
// Connection connection = DataSourceConfig.getConnection(key);
|
|
||||||
// DataSourceConfig.size(key);
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// JDBCConcreteClass jdbcConcreteClass = new JDBCConcreteClass();
|
|
||||||
// ResultSet resultSet = jdbcConcreteClass.getResultSet(connection, "select * from engine_maintenance");
|
|
||||||
// ResultSetMetaData metaData = resultSet.getMetaData();
|
|
||||||
// for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
|
||||||
// log.info("字段名称:{}",metaData.getColumnClassName(i));
|
|
||||||
// log.info("数据库类型:{}",metaData.getColumnTypeName(i));
|
|
||||||
// log.info("java类型:{}",metaData.getColumnClassName(i));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// log.info("返回一个连接,查看连接池");
|
|
||||||
// DataSourceConfig.returnConn(connection);
|
|
||||||
// DataSourceConfig.size(key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.springframework.stereotype.Component;
|
||||||
public class DataSourceConfig {
|
public class DataSourceConfig {
|
||||||
private static HashMap<Long, DruidDataSource> dataSourceMap=new HashMap<>();
|
private static HashMap<Long, DruidDataSource> dataSourceMap=new HashMap<>();
|
||||||
|
|
||||||
|
//初始化连接
|
||||||
public static void init(DataSource dataSource, DatabaseType databaseType){
|
public static void init(DataSource dataSource, DatabaseType databaseType){
|
||||||
DruidDataSource druidDataSource = new DruidDataSource();
|
DruidDataSource druidDataSource = new DruidDataSource();
|
||||||
druidDataSource.setUsername(dataSource.getUser());
|
druidDataSource.setUsername(dataSource.getUser());
|
||||||
|
@ -42,6 +43,7 @@ public class DataSourceConfig {
|
||||||
dataSourceMap.put(dataSource.getId(), druidDataSource);
|
dataSourceMap.put(dataSource.getId(), druidDataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//获取连接
|
||||||
public static Connection getConnection(Long key){
|
public static Connection getConnection(Long key){
|
||||||
DruidDataSource druidDataSource=dataSourceMap.get(key);
|
DruidDataSource druidDataSource=dataSourceMap.get(key);
|
||||||
try {
|
try {
|
||||||
|
@ -52,6 +54,7 @@ public class DataSourceConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//归还连接
|
||||||
public static void returnConn(Connection connection){
|
public static void returnConn(Connection connection){
|
||||||
try {
|
try {
|
||||||
connection.close();
|
connection.close();
|
||||||
|
|
|
@ -24,6 +24,11 @@ public class TestController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestService testService;
|
private TestService testService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据实例集合
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@GetMapping("/getDataModelList/{id}")
|
@GetMapping("/getDataModelList/{id}")
|
||||||
public Result<List<List<DataModel>>> getDataModelList(@PathVariable("id")Long id){
|
public Result<List<List<DataModel>>> getDataModelList(@PathVariable("id")Long id){
|
||||||
List<List<DataModel>> dataModelList=testService.getDataModelList(id);
|
List<List<DataModel>> dataModelList=testService.getDataModelList(id);
|
||||||
|
|
Loading…
Reference in New Issue