text:(连接池,重构)

master
031026 2024-05-08 22:40:31 +08:00
commit fc5811cccf
22 changed files with 322 additions and 34 deletions

View File

@ -23,5 +23,16 @@
<artifactId>muyu-data-source-remote</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version> <!-- 使用最新的 Druid 版本 -->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
</project>

View File

@ -1,18 +1,24 @@
package com.muyu.source.clinet.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.data.source.domain.DataSource;
import com.muyu.data.source.domain.SysDictionary;
import com.muyu.data.source.domain.req.DataSourceQueryReq;
import com.muyu.data.source.remote.RemoteDataManagerService;
import lombok.extern.java.Log;
import com.muyu.source.clinet.factory.Singleton;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
@ -30,6 +36,40 @@ public class DataSourceClinetRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
Result<TableDataInfo<DataSource>> list = remoteDataManagerService.list(new DataSourceQueryReq());
log.info(list);
TableDataInfo<DataSource> data = list.getData();
List<DataSource> rows = data.getRows();
HashMap<String,DataSource> map = new HashMap<>();
DruidDataSource druidDataSource = new DruidDataSource();
for (DataSource row : rows) {
if (row.getDataAccessTypeId()==3) {
map.put(row.getId()+row.getDatabaseName(),row);
druidDataSource.setUrl("jdbc:mysql://" + row.getHostAddress() + ":" + row.getHostPort() + "/" + row.getDatabaseName());
druidDataSource.setUsername(row.getDatabaseUserName());
druidDataSource.setPassword(row.getDatabaseUserPassword());
// 配置连接池属性(可选)
druidDataSource.setInitialSize(row.getInitialQuantity()); // 初始化连接数
druidDataSource.setMaxActive(row.getMaximumQuantity()); // 最大连接数
druidDataSource.setMinIdle(row.getMaximumFrequency()); // 最小空闲连接数
druidDataSource.setMaxWait(row.getMaximumTime());
}else if (row.getDataAccessTypeId()==5){
druidDataSource.setUrl("jdbc:postgresql://" + row.getHostAddress() + ":" + row.getHostPort() + "/" + row.getDatabaseName());
druidDataSource.setUsername(row.getDatabaseUserName());
druidDataSource.setPassword(row.getDatabaseUserPassword());
// 配置连接池属性(可选)
druidDataSource.setInitialSize(row.getInitialQuantity()); // 初始化连接数
druidDataSource.setMaxActive(row.getMaximumQuantity()); // 最大连接数
druidDataSource.setMinIdle(row.getMaximumFrequency()); // 最小空闲连接数
druidDataSource.setMaxWait(row.getMaximumTime());
map.put(row.getId()+row.getDatabaseName(),row);
}
}
log.info(map);
}
}

View File

@ -0,0 +1,24 @@
package com.muyu.source.clinet.factory;
import com.alibaba.druid.pool.DruidDataSource;
import com.muyu.data.source.domain.DataSource;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.HashMap;
@Data
@AllArgsConstructor
public class Singleton {
private HashMap<String, DataSource> map ;
private static Singleton singleton;
//加入了同步代码,解决线程不安全问题
public static synchronized Singleton getInstance(HashMap<String, DataSource> map) {
if (singleton == null) {
singleton = new Singleton(map);
}
return singleton;
}
}

View File

@ -79,22 +79,22 @@ public class DataSource extends BaseEntity {
/** 初始连接数量 */
@Excel(name = "初始连接数量")
@ApiModelProperty(name = "初始连接数量", value = "初始连接数量")
private String initialQuantity;
private Integer initialQuantity;
/** 最大连接数量 */
@Excel(name = "最大连接数量")
@ApiModelProperty(name = "最大连接数量", value = "最大连接数量")
private String maximumQuantity;
private Integer maximumQuantity;
/** 最大等待时间 */
@Excel(name = "最大等待时间")
@ApiModelProperty(name = "最大等待时间", value = "最大等待时间")
private Long maximumTime;
private Integer maximumTime;
/** 最大等待次数 */
@Excel(name = "最大等待次数")
@ApiModelProperty(name = "最大等待次数", value = "最大等待次数")
private String maximumFrequency;
private Integer maximumFrequency;
/** 数据库用户名 */
@Excel(name = "数据库用户名")

View File

@ -35,12 +35,6 @@ public class DatabaseTableInformation {
/**数据库表条数*/
private Integer dataTotal;
/**接入源名称*/
private String accessSourceName;
/**数据来源名称*/
private String dataSourceSystemName;
/**数据库名称*/
private String databaseName;

View File

@ -0,0 +1,30 @@
package com.muyu.data.source.domain.red;
import com.muyu.data.source.domain.AssetStructure;
import com.muyu.data.source.domain.DataSource;
import com.muyu.data.source.domain.DatabaseTableInformation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.List;
/**
*
*
* @ClassName DataSourceTableRed
* @Author AnNan.Wang
* @Date 2024/5/9 14:11
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class DataSourceTableRed {
/** 数据系统 */
private DataSource dataSource;
/** 表集合 */
private List<DatabaseTableInformation> databaseTableInformationList;
}

View File

@ -55,19 +55,19 @@ public class DataSourceEditReq extends BaseEntity {
/** 初始连接数量 */
@ApiModelProperty(name = "初始连接数量", value = "初始连接数量")
private String initialQuantity;
private Integer initialQuantity;
/** 最大连接数量 */
@ApiModelProperty(name = "最大连接数量", value = "最大连接数量")
private String maximumQuantity;
private Integer maximumQuantity;
/** 最大等待时间 */
@ApiModelProperty(name = "最大等待时间", value = "最大等待时间")
private Long maximumTime;
private Integer maximumTime;
/** 最大等待次数 */
@ApiModelProperty(name = "最大等待次数", value = "最大等待次数")
private String maximumFrequency;
private Integer maximumFrequency;
/** 数据库用户名 */
@ApiModelProperty(name = "数据库用户名", value = "数据库用户名")

View File

@ -55,18 +55,18 @@ public class DataSourceQueryReq extends BaseEntity {
/** 初始连接数量 */
@ApiModelProperty(name = "初始连接数量", value = "初始连接数量")
private String initialQuantity;
private Integer initialQuantity;
/** 最大连接数量 */
@ApiModelProperty(name = "最大连接数量", value = "最大连接数量")
private String maximumQuantity;
private Integer maximumQuantity;
/** 最大等待时间 */
@ApiModelProperty(name = "最大等待时间", value = "最大等待时间")
private Long maximumTime;
private Integer maximumTime;
/** 最大等待次数 */
@ApiModelProperty(name = "最大等待次数", value = "最大等待次数")
private String maximumFrequency;
private Integer maximumFrequency;
}

View File

@ -69,22 +69,22 @@ public class DataSourceSaveReq extends BaseEntity {
/** 初始连接数量 */
@ApiModelProperty(name = "初始连接数量", value = "初始连接数量")
private String initialQuantity;
private Integer initialQuantity;
/** 最大连接数量 */
@ApiModelProperty(name = "最大连接数量", value = "最大连接数量")
private String maximumQuantity;
private Integer maximumQuantity;
/** 最大等待时间 */
@ApiModelProperty(name = "最大等待时间", value = "最大等待时间")
private Long maximumTime;
private Integer maximumTime;
/** 最大等待次数 */
@ApiModelProperty(name = "最大等待次数", value = "最大等待次数")
private String maximumFrequency;
private Integer maximumFrequency;
/** 数据库用户名 */
@ApiModelProperty(name = "数据库用户名", value = "数据库用户名")

View File

@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse;
import com.muyu.data.source.domain.*;
import com.muyu.data.source.domain.model.DatabaseTableModel;
import com.muyu.data.source.domain.red.DataSourceTableRed;
import com.muyu.data.source.domain.req.DatabaseConnect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -163,4 +164,8 @@ public class DataSourceController extends BaseController {
}
@GetMapping("/information")
public Result<List<DataSourceTableRed>> information(){
return dataSourceService.information();
}
}

View File

@ -0,0 +1,21 @@
package com.muyu.data.source.controller;
import com.muyu.data.source.service.DatabaseTableInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* DatabaseTableInformation Controller
*
* @ClassName DatabaseTableInformationController
* @Author AnNan.Wang
* @Date 2024/5/9 14:17
*/
@RestController
@RequestMapping("/controller")
public class DatabaseTableInformationController {
@Autowired
private DatabaseTableInformationService dbTableInformationService;;
}

View File

@ -0,0 +1,15 @@
package com.muyu.data.source.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.data.source.domain.DatabaseTableInformation;
/**
* DatabaseTableInformation Mapper
*
* @author AnNan.Wang
* @ClassName: DatabaseTableInformationMapper
* @createTime: 2024/5/9 14:21
*/
public interface DatabaseTableInformationMapper extends BaseMapper<DatabaseTableInformation> {
}

View File

@ -6,6 +6,7 @@ import com.muyu.common.core.domain.Result;
import com.muyu.data.source.domain.*;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.data.source.domain.model.DatabaseTableModel;
import com.muyu.data.source.domain.red.DataSourceTableRed;
import com.muyu.data.source.domain.req.DataSourceSaveReq;
import com.muyu.data.source.domain.req.DatabaseConnect;
@ -45,4 +46,6 @@ public interface DataSourceService extends IService<DataSource> {
Result<List<DatabaseTableInformation>> quantity();
Result<List<DataSourceTableRed>> information();
}

View File

@ -0,0 +1,14 @@
package com.muyu.data.source.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.data.source.domain.DatabaseTableInformation;
/**
* DatabaseTableInformation Service
*
* @author AnNan.Wang
* @ClassName: DatabaseTableInformationService
* @createTime: 2024/5/9 14:19
*/
public interface DatabaseTableInformationService extends IService<DatabaseTableInformation> {
}

View File

@ -8,8 +8,10 @@ import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.data.source.domain.*;
import com.muyu.data.source.domain.model.DatabaseTableModel;
import com.muyu.data.source.domain.red.DataSourceTableRed;
import com.muyu.data.source.domain.req.DataSourceSaveReq;
import com.muyu.data.source.domain.req.DatabaseConnect;
import com.muyu.data.source.mapper.DatabaseTableInformationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -281,4 +283,28 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
public Result<List<DatabaseTableInformation>> quantity() {
return Result.success(dataSourceMapper.quantity());
}
@Autowired
private DatabaseTableInformationMapper databaseTableInformationMapper;
@Override
public Result<List<DataSourceTableRed>> information() {
List<DataSourceTableRed> arrayList = new ArrayList<>();
List<DataSource> dataSources = dataSourceMapper.selectList(null);
for (DataSource dataSource : dataSources) {
List<DatabaseTableInformation> table = dataSourceMapper.table(dataSource.getDatabaseName());
arrayList.add(
DataSourceTableRed.builder()
.dataSource(dataSource)
.databaseTableInformationList(table)
.build()
);
}
return Result.success(
arrayList
);
}
}

View File

@ -0,0 +1,20 @@
package com.muyu.data.source.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.data.source.domain.DatabaseTableInformation;
import com.muyu.data.source.mapper.DatabaseTableInformationMapper;
import com.muyu.data.source.service.DatabaseTableInformationService;
import org.springframework.stereotype.Service;
/**
* DatabaseTableInformation Impl
*
* @ClassName DatabaseTableInformationServiceImpl
* @Author AnNan.Wang
* @Date 2024/5/9 14:20
*/
@Service
public class DatabaseTableInformationServiceImpl extends ServiceImpl<DatabaseTableInformationMapper, DatabaseTableInformation>
implements DatabaseTableInformationService{
}

View File

@ -70,6 +70,11 @@
<artifactId>muyu-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version> <!-- 使用最新的 Druid 版本 -->
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,78 @@
package com.muyu.data.unlt.test;
import com.alibaba.druid.pool.DruidDataSource;
import com.muyu.data.source.domain.DataSource;
import com.muyu.source.clinet.factory.Singleton;
import lombok.extern.log4j.Log4j2;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
/**
*
*
* @ClassName Test
* @Author AnNan.Wang
* @Date 2024/5/9 21:37
*/
@Log4j2
public class Test {
public static void main(String[] args) {
Integer id=10;
String name = "source";
HashMap<String, DataSource> map = new HashMap<String,DataSource>();
Singleton instance = Singleton.getInstance(map);
DataSource druidDataSource = map.get(id + name);
log.info(instance);
// // 创建 Druid 数据源对象
// DruidDataSource dataSource = new DruidDataSource();
// dataSource.setUrl("jdbc:mysql://localhost:3306/demo");
// dataSource.setUsername("root");
// dataSource.setPassword("root");
//
// Connection connection = null;
// Statement statement = null;
// ResultSet resultSet = null;
//
// try {
// // 从数据源中获取数据库连接
// connection = dataSource.getConnection();
//
// // 创建 Statement 对象
// statement = connection.createStatement();
//
// // 执行查询语句
// resultSet = statement.executeQuery("SELECT * FROM t_user");
//
// // 处理结果集
// while (resultSet.next()) {
// // 通过列名获取数据
// String columnName = resultSet.getString("user_id");
//
// // 处理数据
// System.out.println(columnName);
// }
// } catch (SQLException e) {
// e.printStackTrace();
// } finally {
// // 关闭资源
// try {
// if (resultSet != null) {
// resultSet.close();
// }
// if (statement != null) {
// statement.close();
// }
// if (connection != null) {
// connection.close();
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
}
}

View File

@ -12,5 +12,6 @@ public class DataModelContext {
public DataModelContext (DataSetContext dataSetContext) {
this.dataSetContext = dataSetContext;
}
}

View File

@ -40,9 +40,9 @@ public class EngineMaintenanceController extends BaseController
return engineMaintenanceService.compiler(ruleContentReq);
}
@GetMapping("/loader")
public Result loader(@RequestParam Long ruleId){
return engineMaintenanceService.loader(ruleId);
@PostMapping("/loader")
public Result loader(@RequestBody RuleContentReq ruleContentReq){
return engineMaintenanceService.loader(ruleContentReq);
}
@GetMapping("/getRuleContent")

View File

@ -21,7 +21,7 @@ public interface IEngineMaintenanceService extends IService<Ruleengine> {
Result compiler(RuleContentReq ruleContentReq);
Result loader(Long ruleId);
Result loader(RuleContentReq ruleContentReq);
Result<RuleContentModel> getRuleContent(Long ruleId);

View File

@ -88,7 +88,7 @@ public class EngineMaintenanceServiceImpl extends ServiceImpl<EngineMaintenanceM
String className = ruleContentReq.getClassName();
String content = ruleContentReq.getRuleContent();
// 指定输出目录
String targetDirectory = "D:\\work\\etl\\cloud-server\\muyu-modules\\muyu-rule\\muyu-rule-server\\src\\main\\java\\com\\muyu\\engine";
String targetDirectory = "D:\\work\\etl\\cloud-server\\muyu-modules\\muyu-rule\\muyu-rule-server\\src\\main\\java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
@ -115,20 +115,21 @@ public class EngineMaintenanceServiceImpl extends ServiceImpl<EngineMaintenanceM
}
@Override
public Result loader(Long ruleId) {
public Result loader(RuleContentReq ruleContentReq) {
String className = ruleContentReq.getClassName();
try {
// 假设我们有一个名为com.example.MyClass的类文件
// 注意:这里应该是.class文件的路径
String classPath = "D:\\work\\etl\\cloud-server\\muyu-modules\\muyu-rule\\muyu-rule-server\\src\\main\\java\\com\\muyu\\rule\\controller\\Test"+ruleId+".class";
String classPath = "D:\\work\\etl\\cloud-server\\muyu-modules\\muyu-rule\\muyu-rule-server\\src\\main\\java\\com\\muyu\\engine\\custom\\"+className+".class";
// 读取类文件
byte[] classData = Files.readAllBytes(Paths.get(classPath));
// 提取类名(不包括.class和路径
String className = classPath.substring(classPath.lastIndexOf('\\') + 1).replace(".class", "");
String code = classPath.substring(classPath.lastIndexOf('\\') + 1).replace(".class", "");
// 使用自定义类加载器加载类
MyClassLoader classLoader = new MyClassLoader();
Class<?> clazz = classLoader.defineClassFromBytes(className, classData);
Class<?> clazz = classLoader.defineClassFromBytes(code, classData);
// 使用反射创建实例并调用方法
Object instance = clazz.getDeclaredConstructor().newInstance();