mirror of https://gitee.com/stylefeng/roses
				
				
				
			【7.0.3】增加数据源连接自我检测
							parent
							
								
									ce6e6f1194
								
							
						
					
					
						commit
						dfb9e777ed
					
				| 
						 | 
				
			
			@ -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.
 | 
			
		||||
 *
 | 
			
		||||
 * Guns采用APACHE 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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为:{},异常为:{}"),
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 添加数据源失败,当前环境已经存在同名数据源
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 备注,摘要
 | 
			
		||||
     */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
 *
 | 
			
		||||
 * Guns采用APACHE 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);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue