feat:并发优化

This commit is contained in:
nieziyan 2023-12-14 19:29:18 +08:00
parent 01b6f4e9bf
commit ac3b790622
12 changed files with 147 additions and 63 deletions
jeecg-boot-base-core/src/main/java/org/jeecg
jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules

View File

@ -200,7 +200,7 @@ public class EmailServiceManager {
store.connect(host, username, password); store.connect(host, username, password);
return store.isConnected(); return store.isConnected();
} catch (Exception e) { } catch (Exception e) {
log.error("收件邮箱服务[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage()); log.error("收件邮箱服务[Host: {}, Port: {}, Username: {}]连接异常: {}",host, port, username, e.getMessage());
return false; return false;
} }
} }
@ -233,7 +233,7 @@ public class EmailServiceManager {
store.connect(host, username, password); store.connect(host, username, password);
return store.isConnected(); return store.isConnected();
} catch (Exception e) { } catch (Exception e) {
log.error("收件邮箱服务SSL[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage()); log.error("收件邮箱服务SSL[Host: {}, Port: {}, Username: {}]连接异常: {}",host, port, username, e.getMessage());
return false; return false;
} }
} }
@ -264,7 +264,7 @@ public class EmailServiceManager {
transport.connect(host, username, password); transport.connect(host, username, password);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error("发件邮箱服务[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage()); log.error("发件邮箱服务[Host: {}, Port: {}, Username: {}]连接异常: {}",host, port, username, e.getMessage());
return false; return false;
} }
} }
@ -297,7 +297,7 @@ public class EmailServiceManager {
transport.connect(host, username, password); transport.connect(host, username, password);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error("发件邮箱服务SSL[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage()); log.error("发件邮箱服务SSL[Host: {}, Port: {}, Username: {}]连接异常: {}",host, port, username, e.getMessage());
return false; return false;
} }
} }

View File

@ -20,13 +20,15 @@ public class EmailUtil {
if (ObjectUtil.isNotNull(emailType)){ if (ObjectUtil.isNotNull(emailType)){
switch (emailType){ switch (emailType){
case RECEIVE_EMAIL: case RECEIVE_EMAIL:
return manager.canReceive() || manager.canReceiveSSL(); return manager.canReceiveSSL() || manager.canReceive();
case SEND_EMAIL: case SEND_EMAIL:
return manager.canSend() || manager.canSendSSL(); return manager.canSendSSL() || manager.canSend();
default:
break;
} }
} }
// 如果emailType(收件/发件)不确定 直接测试能否收件/发件 // 如果emailType(收件/发件)不确定 直接测试能否收件/发件
return manager.canReceive() || manager.canReceiveSSL() return manager.canReceiveSSL() || manager.canSendSSL()
|| manager.canSend() || manager.canSendSSL(); || manager.canSend() || manager.canReceive();
} }
} }

View File

@ -1,17 +0,0 @@
package org.jeecg.modules.base.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class NameString implements Serializable {
private String name;
private String value;
}

View File

@ -15,10 +15,17 @@ public class NameValue implements Serializable {
private Boolean value; private Boolean value;
private String valueT;
private Integer usage; // 单位 MB private Integer usage; // 单位 MB
public NameValue(String name, Boolean value) { public NameValue(String name, Boolean value) {
this.name = name; this.name = name;
this.value = value; this.value = value;
} }
public NameValue(String name, String valueT) {
this.name = name;
this.valueT = valueT;
}
} }

View File

@ -0,0 +1,80 @@
package org.jeecg.modules;
import cn.hutool.core.collection.ListUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jeecg.common.email.EmailServiceManager;
import org.jeecg.modules.base.entity.postgre.SysEmail;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class Demo {
public static void main(String[] args) {
EmailServiceManager manager = EmailServiceManager.getInstance();
SysEmail email = new SysEmail();
// Get
email.setEmailServerAddress("imap.qiye.163.com");
email.setUsername("cnndc.rn.ng@ndc.org.cn");
email.setPassword("cnndc66367220");
email.setPort(993);
/*email.setEmailServerAddress("imap.exmail.qq.com");
email.setUsername("xiaoguangbin@hivekion.com");
email.setPassword("Ans9sLY4kVnux7ai");
email.setPort(143);
// Send
email.setEmailServerAddress("smtphz.qiye.163.com");
email.setUsername("cnndc.rn.ng@ndc.org.cn");
email.setPassword("cnndc66367220");
email.setPort(465);
email.setEmailServerAddress("smtp.163.com");
email.setUsername("armd_auto@163.com");
email.setPassword("NVOWHFOGWVOFILVV");
email.setPort(25);*/
/*manager.init(email);
long start = System.currentTimeMillis();
System.out.println(manager.canReceiveSSL() || manager.canReceive());
long end = System.currentTimeMillis();
System.out.println("连接耗时: " + (end - start) / 1000 + "s");*/
test();
}
public static void test(){
SysEmail email1 = new SysEmail();
email1.setEmailServerAddress("imap.qiye.163.com");
email1.setUsername("cnndc.rn.ng@ndc.org.cn");
email1.setPassword("cnndc66367220");
email1.setPort(993);
/*SysEmail email2 = new SysEmail();
email2.setEmailServerAddress("imap.qiye.163.com");
email2.setUsername("cnndc.rn.ng@ndc.org.cn");
email2.setPassword("cnndc66367220");
email2.setPort(993);*/
SysEmail email2 = new SysEmail();
email2.setEmailServerAddress("imap.exmail.qq.com");
email2.setUsername("xiaoguangbin@hivekion.com");
email2.setPassword("Ans9sLY4kVnux7ai");
email2.setPort(143);
List<SysEmail> list = ListUtil.toList(email1, email2);
EmailServiceManager manager = EmailServiceManager.getInstance();
for (SysEmail email : list) {
manager.init(email);
CompletableFuture.runAsync(() -> {
boolean success = manager.canReceive();
});
}
}
}

View File

@ -15,7 +15,6 @@ import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.util.EmailUtil; import org.jeecg.common.util.EmailUtil;
import org.jeecg.common.util.JDBCUtil; import org.jeecg.common.util.JDBCUtil;
import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.dto.NameString;
import org.jeecg.modules.base.dto.NameValue; import org.jeecg.modules.base.dto.NameValue;
import org.jeecg.modules.base.entity.monitor.Host; import org.jeecg.modules.base.entity.monitor.Host;
import org.jeecg.modules.base.entity.monitor.Servers; import org.jeecg.modules.base.entity.monitor.Servers;
@ -136,7 +135,7 @@ public class StatusAspect {
status = host.getStatus(); status = host.getStatus();
} }
// 更新该服务器状态信息 // 更新该服务器状态信息
redisUtil.hset(key, id, new NameString(name, status)); redisUtil.hset(key, id, new NameValue(name, status));
// 更新该服务器的HostId // 更新该服务器的HostId
serverService.updateById(server); serverService.updateById(server);
}catch (FeignException.Unauthorized e){ }catch (FeignException.Unauthorized e){

View File

@ -9,23 +9,22 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ControllerAdvice(assignableTypes = SystemMonitorController.class) @RestControllerAdvice(assignableTypes = SystemMonitorController.class)
@Slf4j @Slf4j
public class MonitorExceptionHandler { public class MonitorExceptionHandler {
@ExceptionHandler(FeignException.Unauthorized.class) @ExceptionHandler(FeignException.Unauthorized.class)
public ResponseEntity<Result<?>> handleFeignExceptionUnauthorized(FeignException.Unauthorized e) { public Result<?> handleFeignExceptionUnauthorized(FeignException.Unauthorized e) {
ManageUtil.refreshToken(); ManageUtil.refreshToken();
log.warn("运管服务Token失效,Token已刷新"); log.warn("运管服务Token失效,Token已刷新");
return ResponseEntity.status(HttpStatus.OK) return Result.error("Management system token is invalid and refreshed");
.body(Result.error("Management system token is invalid and refreshed"));
} }
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ResponseEntity<Result<?>> handleException(Exception e) { public Result<?> handleException(Exception e) {
log.error("运管服务调用异常: {}", e.getMessage()); log.error("运管服务调用异常: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.OK) return Result.error("Management system is abnormal, data cannot be displayed");
.body(Result.error("Management system is abnormal, data cannot be displayed"));
} }
} }

View File

@ -95,12 +95,12 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
String hostId = serverDto.getHostId(); String hostId = serverDto.getHostId();
serverDto.setAlarmRed(alarms > 0); serverDto.setAlarmRed(alarms > 0);
// 设置服务器状态信息 // 设置服务器状态信息
NameString nameValue = (NameString)statusMap.get(id); NameValue nameValue = (NameValue)statusMap.get(id);
String status = ServerStatus.UNKNOWN.getValue(); String status = ServerStatus.UNKNOWN.getValue();
if (ObjectUtil.isNotNull(nameValue)){ if (ObjectUtil.isNotNull(nameValue)){
String value = nameValue.getValue(); String valueT = nameValue.getValueT();
if (StrUtil.isNotBlank(value)) if (StrUtil.isNotBlank(valueT))
status = value; status = valueT;
} }
serverDto.setStatus(status); serverDto.setStatus(status);
// 设置服务器的硬件使用情况信息 // 设置服务器的硬件使用情况信息
@ -336,11 +336,11 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
// 获取该服务器的状态并保存 // 获取该服务器的状态并保存
if (ObjectUtil.isNotNull(host)) { if (ObjectUtil.isNotNull(host)) {
String status = host.getStatus(); String status = host.getStatus();
values.put(serverId, new NameString(name, status)); values.put(serverId, new NameValue(name, status));
continue; continue;
} }
// 当前服务器不在监控服务器列表 将它的状态设置为未知 // 当前服务器不在监控服务器列表 将它的状态设置为未知
values.put(serverId, new NameString(name, ServerStatus.UNKNOWN.getValue())); values.put(serverId, new NameValue(name, ServerStatus.UNKNOWN.getValue()));
} }
redisUtil.hmset(key, values); redisUtil.hmset(key, values);
}catch (FeignException.Unauthorized e){ }catch (FeignException.Unauthorized e){
@ -356,9 +356,9 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
public String getNameById(String id) { public String getNameById(String id) {
String key = RedisConstant.SERVER_STATUS; String key = RedisConstant.SERVER_STATUS;
if (redisUtil.hHasKey(key, id)){ if (redisUtil.hHasKey(key, id)){
NameString nS = (NameString) redisUtil.hget(key, id); NameValue nameValue = (NameValue) redisUtil.hget(key, id);
if (ObjectUtil.isNotNull(nS)) if (ObjectUtil.isNotNull(nameValue))
return nS.getName(); return nameValue.getName();
} }
return getById(id).getName(); return getById(id).getName();
} }
@ -369,7 +369,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
for (SysServer sysServer : sysServers) { for (SysServer sysServer : sysServers) {
String id = sysServer.getId(); String id = sysServer.getId();
String name = sysServer.getName(); String name = sysServer.getName();
values.put(id, new NameString(name, ServerStatus.UNKNOWN.getValue())); values.put(id, new NameValue(name, ServerStatus.UNKNOWN.getValue()));
} }
redisUtil.hmset(key, values); redisUtil.hmset(key, values);
} }

View File

@ -37,7 +37,14 @@ public class ManageUtil {
LoginVo loginVo = new LoginVo(username, password); LoginVo loginVo = new LoginVo(username, password);
Result<LoginResult> loginRes = monitorSystem.login(loginVo); Result<LoginResult> loginRes = monitorSystem.login(loginVo);
String token = loginRes.getResult().getToken(); String token = loginRes.getResult().getToken();
redisUtil.set(RedisConstant.MANAGE_TOKEN, token, JwtUtil.EXPIRE_TIME * 2 / 1000 - 10); redisUtil.set(RedisConstant.MANAGE_TOKEN, token, JwtUtil.EXPIRE_TIME / 1000 - 10);
return token; return token;
} }
public static void refreshToken(){
LoginVo loginVo = new LoginVo(username, password);
Result<LoginResult> loginRes = monitorSystem.login(loginVo);
String token = loginRes.getResult().getToken();
redisUtil.set(RedisConstant.MANAGE_TOKEN, token, JwtUtil.EXPIRE_TIME / 1000 - 10);
}
} }

View File

@ -99,10 +99,11 @@ public class DatabaseJob extends Monitor implements Job{
alarmLog.setOperator(operator); alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current)); alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName(); String ruleName = alarmRule.getName();
String message = String.format("您的数据库: [%s]", databaseName); StringBuilder message = new StringBuilder();
message += String.format(",设定的预警规则: %s", ruleName); message.append(String.format("您的数据源: [%s]", databaseName));
message += String.format(",预警信息为: %s,当前值为: %s", operator, current); message.append(String.format(",设定的预警规则: %s", ruleName));
alarmLog.setAlarmInfo(message); message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog); getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有) // 规则触发报警后,设置该规则的沉默周期(如果有)
@ -113,11 +114,11 @@ public class DatabaseJob extends Monitor implements Job{
// 发送报警信息 // 发送报警信息
String groupId = alarmRule.getContactId(); String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification(); String notific = alarmRule.getNotification();
getSendMessage().send("Database Monitor Warn Message", message, groupId, notific); getSendMessage().send("Database Monitor Warn Message", message.toString(), groupId, notific);
} }
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error("Database预警规则: {}解析失败,失败原因: {}", operator, e.getMessage()); log.error("Database预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (RuntimeException e){ }catch (Exception e){
log.error("Database监控异常: {}", e.getMessage()); log.error("Database监控异常: {}", e.getMessage());
} }
} }

View File

@ -85,10 +85,11 @@ public class EmailJob extends Monitor implements Job{
alarmLog.setOperator(operator); alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current)); alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName(); String ruleName = alarmRule.getName();
String message = String.format("您的Email: [%s]", emailName); StringBuilder message = new StringBuilder();
message += String.format(",设定的预警规则: %s", ruleName); message.append(String.format("您的邮箱: [%s]", emailName));
message += String.format(",预警信息为: %s,当前值为: %s", operator, current); message.append(String.format(",设定的预警规则: %s", ruleName));
alarmLog.setAlarmInfo(message); message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog); getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有) // 规则触发报警后,设置该规则的沉默周期(如果有)
@ -99,11 +100,11 @@ public class EmailJob extends Monitor implements Job{
// 发送报警信息 // 发送报警信息
String groupId = alarmRule.getContactId(); String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification(); String notific = alarmRule.getNotification();
getSendMessage().send("Email Monitor Warn Message", message, groupId, notific); getSendMessage().send("Email Monitor Warn Message", message.toString(), groupId, notific);
} }
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error("Email预警规则: {}解析失败,失败原因: {}", operator, e.getMessage()); log.error("Email预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (RuntimeException e){ }catch (Exception e){
log.error("Email监控异常: {}", e.getMessage()); log.error("Email监控异常: {}", e.getMessage());
} }
} }

View File

@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
@ -100,10 +101,11 @@ public class ServerJob extends Monitor implements Job {
alarmLog.setOperator(operator); alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current)); alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName(); String ruleName = alarmRule.getName();
String message = String.format("您的服务器: [%s]", serverName); StringBuilder message = new StringBuilder();
message += String.format(",设定的预警规则: %s", ruleName); message.append(String.format("您的服务器: [%s]", serverName));
message += String.format(",预警信息为: %s,当前值为: %s", operator, current); message.append(String.format(",设定的预警规则: %s", ruleName));
alarmLog.setAlarmInfo(message); message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog); getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有) // 规则触发报警后,设置该规则的沉默周期(如果有)
@ -114,11 +116,14 @@ public class ServerJob extends Monitor implements Job {
// 发送报警信息 // 发送报警信息
String groupId = alarmRule.getContactId(); String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification(); String notific = alarmRule.getNotification();
getSendMessage().send("Server Monitor Warn Message", message, groupId, notific); getSendMessage().send("Server Monitor Warn Message", message.toString(), groupId, notific);
} }
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询ItemHistory信息异常: Token失效,已刷新Token");
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error("Server预警规则: {}解析失败,失败原因: {}", operator, e.getMessage()); log.error("Server预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (RuntimeException e){ }catch (Exception e){
log.error("Server监控异常: {}", e.getMessage()); log.error("Server监控异常: {}", e.getMessage());
} }
} }