【7.0.3】增加数据源连接自我检测

pull/13/head
fengshuonan 2021-04-22 11:19:37 +08:00
parent ce6e6f1194
commit dfb9e777ed
7 changed files with 181 additions and 3 deletions

View File

@ -0,0 +1,57 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.dsctn.api.enums;
import lombok.Getter;
/**
*
*
* @author fengshuonan
* @date 2021/4/22 11:02
*/
@Getter
public enum DataSourceStatusEnum {
/**
*
*/
ENABLE(1, "正常"),
/**
*
*/
ERROR(2, "无法连接");
private final Integer code;
private final String message;
DataSourceStatusEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}

View File

@ -71,7 +71,7 @@ public enum DatasourceContainerExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
VALIDATE_DATASOURCE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DatasourceContainerConstants.DS_CTN_EXCEPTION_STEP_CODE + "07", "检验数据库连接失败请检查连接是否可用url为{}"),
VALIDATE_DATASOURCE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DatasourceContainerConstants.DS_CTN_EXCEPTION_STEP_CODE + "07", "检验数据库连接失败请检查连接是否可用url为{},异常为:{}"),
/**
*

View File

@ -32,6 +32,14 @@
<version>7.0.3</version>
</dependency>
<!--定时任务的api-->
<!--定时轮询数据源的状态,更新到库中-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>timer-api</artifactId>
<version>7.0.3</version>
</dependency>
<!--数据库sdk-->
<!--数据库dao框架-->
<dependency>

View File

@ -78,6 +78,18 @@ public class DatabaseInfo extends BaseEntity {
@TableField("password")
private String password;
/**
* 1-2-
*/
@TableField("status_flag")
private Integer statusFlag;
/**
*
*/
@TableField("error_description")
private String errorDescription;
/**
*
*/

View File

@ -94,4 +94,13 @@ public interface DatabaseInfoService extends IService<DatabaseInfo> {
*/
List<DatabaseInfo> findList(DatabaseInfoRequest databaseInfoRequest);
/**
*
*
* @param param
* @author fengshuonan
* @date 2021/4/22 10:46
*/
void validateConnection(DatabaseInfoRequest param);
}

View File

@ -168,13 +168,13 @@ public class DatabaseInfoServiceImpl extends ServiceImpl<DatabaseInfoMapper, Dat
* @author fengshuonan
* @date 2020/11/1 21:50
*/
private void validateConnection(DatabaseInfoRequest param) {
public void validateConnection(DatabaseInfoRequest param) {
Connection conn = null;
try {
Class.forName(param.getJdbcDriver());
conn = DriverManager.getConnection(param.getJdbcUrl(), param.getUsername(), param.getPassword());
} catch (Exception e) {
throw new DatasourceContainerException(VALIDATE_DATASOURCE_ERROR, param.getJdbcUrl());
throw new DatasourceContainerException(VALIDATE_DATASOURCE_ERROR, param.getJdbcUrl(), e.getMessage());
} finally {
if (conn != null) {
try {

View File

@ -0,0 +1,92 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.dsctn.modular.timer;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.dsctn.api.enums.DataSourceStatusEnum;
import cn.stylefeng.roses.kernel.dsctn.api.pojo.request.DatabaseInfoRequest;
import cn.stylefeng.roses.kernel.dsctn.modular.entity.DatabaseInfo;
import cn.stylefeng.roses.kernel.dsctn.modular.service.DatabaseInfoService;
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
*
*
* @author fengshuonan
* @date 2021/4/22 10:45
*/
@Component
public class DataSourceStatusCheckTimer implements TimerAction {
@Resource
private DatabaseInfoService databaseInfoService;
@Override
public void action(String params) {
// 获取所有的数据源信息
List<DatabaseInfo> list = databaseInfoService.list();
if (ObjectUtil.isEmpty(list)) {
return;
}
// 校验每个数据库连接的信息
for (DatabaseInfo databaseInfo : list) {
// 设置jdbc相关连接
DatabaseInfoRequest databaseInfoRequest = new DatabaseInfoRequest();
databaseInfoRequest.setJdbcDriver(databaseInfo.getJdbcDriver());
databaseInfoRequest.setJdbcUrl(databaseInfo.getJdbcUrl());
databaseInfoRequest.setUsername(databaseInfo.getUsername());
databaseInfoRequest.setPassword(databaseInfo.getPassword());
// 检测每个连接的准确性
try {
databaseInfoService.validateConnection(databaseInfoRequest);
} catch (Exception exception) {
// 如果有错误信息,将错误信息存储到表中
String errorMessage = exception.getMessage();
databaseInfo.setStatusFlag(DataSourceStatusEnum.ERROR.getCode());
databaseInfo.setErrorDescription(errorMessage);
databaseInfoService.updateById(databaseInfo);
continue;
}
// 如果数据库状态为空,则修改为正常
if (!DataSourceStatusEnum.ENABLE.getCode().equals(databaseInfo.getStatusFlag())) {
databaseInfo.setStatusFlag(DataSourceStatusEnum.ENABLE.getCode());
databaseInfoService.updateById(databaseInfo);
}
}
}
}