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

View File

@ -200,7 +200,7 @@ public class EmailServiceManager {
store.connect(host, username, password);
return store.isConnected();
} 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;
}
}
@ -233,7 +233,7 @@ public class EmailServiceManager {
store.connect(host, username, password);
return store.isConnected();
} 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;
}
}
@ -264,7 +264,7 @@ public class EmailServiceManager {
transport.connect(host, username, password);
return true;
} 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;
}
}
@ -297,7 +297,7 @@ public class EmailServiceManager {
transport.connect(host, username, password);
return true;
} 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;
}
}

View File

@ -20,13 +20,15 @@ public class EmailUtil {
if (ObjectUtil.isNotNull(emailType)){
switch (emailType){
case RECEIVE_EMAIL:
return manager.canReceive() || manager.canReceiveSSL();
return manager.canReceiveSSL() || manager.canReceive();
case SEND_EMAIL:
return manager.canSend() || manager.canSendSSL();
return manager.canSendSSL() || manager.canSend();
default:
break;
}
}
// 如果emailType(收件/发件)不确定 直接测试能否收件/发件
return manager.canReceive() || manager.canReceiveSSL()
|| manager.canSend() || manager.canSendSSL();
return manager.canReceiveSSL() || 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 String valueT;
private Integer usage; // 单位 MB
public NameValue(String name, Boolean value) {
this.name = name;
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.JDBCUtil;
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.entity.monitor.Host;
import org.jeecg.modules.base.entity.monitor.Servers;
@ -136,7 +135,7 @@ public class StatusAspect {
status = host.getStatus();
}
// 更新该服务器状态信息
redisUtil.hset(key, id, new NameString(name, status));
redisUtil.hset(key, id, new NameValue(name, status));
// 更新该服务器的HostId
serverService.updateById(server);
}catch (FeignException.Unauthorized e){

View File

@ -9,23 +9,22 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ControllerAdvice(assignableTypes = SystemMonitorController.class)
@RestControllerAdvice(assignableTypes = SystemMonitorController.class)
@Slf4j
public class MonitorExceptionHandler {
@ExceptionHandler(FeignException.Unauthorized.class)
public ResponseEntity<Result<?>> handleFeignExceptionUnauthorized(FeignException.Unauthorized e) {
public Result<?> handleFeignExceptionUnauthorized(FeignException.Unauthorized e) {
ManageUtil.refreshToken();
log.warn("运管服务Token失效,Token已刷新");
return ResponseEntity.status(HttpStatus.OK)
.body(Result.error("Management system token is invalid and refreshed"));
return Result.error("Management system token is invalid and refreshed");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Result<?>> handleException(Exception e) {
public Result<?> handleException(Exception e) {
log.error("运管服务调用异常: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.OK)
.body(Result.error("Management system is abnormal, data cannot be displayed"));
return 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();
serverDto.setAlarmRed(alarms > 0);
// 设置服务器状态信息
NameString nameValue = (NameString)statusMap.get(id);
NameValue nameValue = (NameValue)statusMap.get(id);
String status = ServerStatus.UNKNOWN.getValue();
if (ObjectUtil.isNotNull(nameValue)){
String value = nameValue.getValue();
if (StrUtil.isNotBlank(value))
status = value;
String valueT = nameValue.getValueT();
if (StrUtil.isNotBlank(valueT))
status = valueT;
}
serverDto.setStatus(status);
// 设置服务器的硬件使用情况信息
@ -336,11 +336,11 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
// 获取该服务器的状态并保存
if (ObjectUtil.isNotNull(host)) {
String status = host.getStatus();
values.put(serverId, new NameString(name, status));
values.put(serverId, new NameValue(name, status));
continue;
}
// 当前服务器不在监控服务器列表 将它的状态设置为未知
values.put(serverId, new NameString(name, ServerStatus.UNKNOWN.getValue()));
values.put(serverId, new NameValue(name, ServerStatus.UNKNOWN.getValue()));
}
redisUtil.hmset(key, values);
}catch (FeignException.Unauthorized e){
@ -356,9 +356,9 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
public String getNameById(String id) {
String key = RedisConstant.SERVER_STATUS;
if (redisUtil.hHasKey(key, id)){
NameString nS = (NameString) redisUtil.hget(key, id);
if (ObjectUtil.isNotNull(nS))
return nS.getName();
NameValue nameValue = (NameValue) redisUtil.hget(key, id);
if (ObjectUtil.isNotNull(nameValue))
return nameValue.getName();
}
return getById(id).getName();
}
@ -369,7 +369,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
for (SysServer sysServer : sysServers) {
String id = sysServer.getId();
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);
}

View File

@ -37,7 +37,14 @@ public class ManageUtil {
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 * 2 / 1000 - 10);
redisUtil.set(RedisConstant.MANAGE_TOKEN, token, JwtUtil.EXPIRE_TIME / 1000 - 10);
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.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
String message = String.format("您的数据库: [%s]", databaseName);
message += String.format(",设定的预警规则: %s", ruleName);
message += String.format(",预警信息为: %s,当前值为: %s", operator, current);
alarmLog.setAlarmInfo(message);
StringBuilder message = new StringBuilder();
message.append(String.format("您的数据源: [%s]", databaseName));
message.append(String.format(",设定的预警规则: %s", ruleName));
message.append(String.format(",预警信息为: %s,当前值为: %s", operator, current));
alarmLog.setAlarmInfo(message.toString());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
@ -113,11 +114,11 @@ public class DatabaseJob extends Monitor implements Job{
// 发送报警信息
String groupId = alarmRule.getContactId();
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) {
log.error("Database预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (RuntimeException e){
}catch (Exception e){
log.error("Database监控异常: {}", e.getMessage());
}
}

View File

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

View File

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