refactor(): 重构测试连接
parent
1e1797f443
commit
543a97c3ac
|
@ -50,6 +50,9 @@ public class AssetStructureTable extends BaseEntity {
|
|||
@Excel(name = "表数据总数")
|
||||
private Long tableDataCount;
|
||||
|
||||
/**
|
||||
* 表注释
|
||||
*/
|
||||
@Excel(name = "表注释")
|
||||
private String tableNameAnnotation;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue