refactor(): 重构测试连接

dev
chao 2024-04-25 15:50:22 +08:00
parent 1e1797f443
commit 543a97c3ac
2 changed files with 104 additions and 50 deletions

View File

@ -50,6 +50,9 @@ public class AssetStructureTable extends BaseEntity {
@Excel(name = "表数据总数")
private Long tableDataCount;
/**
*
*/
@Excel(name = "表注释")
private String tableNameAnnotation;
}

View File

@ -17,6 +17,7 @@ import com.etl.data.structure.service.IAssetStructureTableService;
import com.etl.data.structure.service.IAssetTableDetailsService;
import com.etl.data.type.domain.DataType;
import com.etl.data.type.service.IDataTypeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -34,7 +35,9 @@ import java.util.stream.Collectors;
* @date 2024-04-21
*/
@Service
@Log4j2
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements IDataSourceService {
@Autowired
private DataSourceMapper dataSourceMapper;
@ -138,61 +141,23 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
new LambdaQueryWrapper<DataSource>()
.eq(DataSource::getId, id)
);
String jdbcUrl = "";
String driveClass = "";
boolean flag = false;
DataType dataType = dataTypeService.getOne(
new LambdaQueryWrapper<DataType>()
.eq(DataType::getId, dataSource.getTypeId())
);
try {
if (dataType.getDriverManager() != null && dataType.getJdbcPre() != null) {
if ("mysql".equals(dataType.getDataType())) {
driveClass = dataType.getDriverManager();
jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + "/" + dataSource.getDataSourceDatabaseName() + "?" + dataSource.getAdditionalConfiguration();
}
if ("oracle".equals(dataType.getDataType())) {
driveClass = dataType.getDriverManager();
jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ":" + dataSource.getDataSourceDatabaseName();
}
if ("sqlserver".equals(dataType.getDataType())) {
driveClass = dataType.getDriverManager();
jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ";databaseName=" + dataSource.getDataSourceDatabaseName();
}
flag = testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword());
} else {
// redis
//连接指定的redis
Jedis jedis = new Jedis(dataSource.getDataSourceIp(), Integer.valueOf(dataSource.getDataSourcePort()));
//如果有密码则需要下面这一行
if (dataSource.getDataSourcePassword() != null && !dataSource.getDataSourcePassword().equals("")) {
jedis.auth(dataSource.getDataSourcePassword());
}
//查看服务是否运行,运行正常的话返回一个PONG否则返回一个连接错误
try {
jedis.ping();
flag = true;
} catch (Exception e) {
flag = false;
}
}
if (flag) {
this.update(
new UpdateWrapper<DataSource>()
.set("status", "S")
.eq("id", id)
);
} else {
this.update(
new UpdateWrapper<DataSource>()
.set("status", "E")
.eq("id", id)
);
}
return flag;
} catch (Exception e) {
return flag;
boolean flag = false;
if (dataType.getDriverManager() != null && dataType.getJdbcPre() != null) {
flag = testDatasource(dataSource, dataType);
} else {
// 调用redis测试是否连接方法
flag = this.redisConnection(dataSource);
}
if (flag) {
this.update(new UpdateWrapper<DataSource>().set("status", "S").eq("id", id));
} else {
this.update(new UpdateWrapper<DataSource>().set("status", "E").eq("id", id));
}
return flag;
}
/**
@ -591,4 +556,90 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
return false;
}
}
/**
*
* @param dataSource
* @param dataType
* @return
*/
public boolean testDatasource(DataSource dataSource, DataType dataType) {
if ("mysql".equals(dataType.getDataType())) {
log.info("MySQL测试连接");
return this.mysqlConnection(dataSource, dataType);
}
if ("oracle".equals(dataType.getDataType())) {
log.info("Oracle测试连接");
return this.oracleConnection(dataSource, dataType);
}
if ("sqlserver".equals(dataType.getDataType())) {
log.info("SQLServer测试连接");
return this.sqlserverConnection(dataSource, dataType);
}
return false;
}
/**
* mysql
*
* @param dataSource
* @param dataType
* @return
*/
public boolean mysqlConnection(DataSource dataSource, DataType dataType) {
String driveClass = dataType.getDriverManager();
String jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + "/" + dataSource.getDataSourceDatabaseName() + "?" + dataSource.getAdditionalConfiguration();
return testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword());
}
/**
* oracle
*
* @param dataSource
* @param dataType
* @return
*/
public boolean oracleConnection(DataSource dataSource, DataType dataType) {
String driveClass = dataType.getDriverManager();
String jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ":" + dataSource.getDataSourceDatabaseName();
return testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword());
}
/**
* sqlserver
*
* @param dataSource
* @param dataType
* @return
*/
public boolean sqlserverConnection(DataSource dataSource, DataType dataType) {
String driveClass = dataType.getDriverManager();
String jdbcUrl = dataType.getJdbcPre() + dataSource.getDataSourceIp() + ":" + dataSource.getDataSourcePort() + ";databaseName=" + dataSource.getDataSourceDatabaseName();
return testDatasource(driveClass, jdbcUrl, dataSource.getDataSourceUsername(), dataSource.getDataSourcePassword());
}
/**
* redis
*
* @param dataSource
* @return
*/
public boolean redisConnection(DataSource dataSource) {
try {
//连接指定的redis
Jedis jedis = new Jedis(dataSource.getDataSourceIp(), Integer.valueOf(dataSource.getDataSourcePort()));
//如果有密码则需要下面这一行
if (dataSource.getDataSourcePassword() != null && !dataSource.getDataSourcePassword().equals("")) {
jedis.auth(dataSource.getDataSourcePassword());
}
//查看服务是否运行,运行正常的话返回一个PONG否则返回一个连接错误
jedis.ping();
return true;
} catch (Exception e) {
log.info("redis连接失败{}", e.getMessage());
return false;
}
}
}