fix:使用AOP和异步优化系统

This commit is contained in:
nieziyan 2023-12-13 20:13:16 +08:00
parent 432cdbea0b
commit e352f7a043
8 changed files with 163 additions and 71 deletions

View File

@ -0,0 +1,150 @@
package org.jeecg.modules.aspect;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.netease.qiye.qiyeopenplatform.sdk.QiyeOpenPlatSDK;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.jeecg.common.constant.MonitorConstant;
import org.jeecg.common.constant.RedisConstant;
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;
import org.jeecg.modules.base.entity.postgre.SysDatabase;
import org.jeecg.modules.base.entity.postgre.SysEmail;
import org.jeecg.modules.base.entity.postgre.SysServer;
import org.jeecg.modules.base.enums.ServerStatus;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorAlarm;
import org.jeecg.modules.qiyeEmail.base.InstanceSDK;
import org.jeecg.modules.qiyeEmail.base.RParam;
import org.jeecg.modules.qiyeEmail.base.dto.AccountInfo;
import org.jeecg.modules.qiyeEmail.service.Account;
import org.jeecg.modules.service.ISysEmailService;
import org.jeecg.modules.service.ISysServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
import static org.jeecg.modules.base.enums.Qiye.IS;
@Slf4j
@Aspect
@Component
public class StatusAspect {
@Autowired
private RedisUtil redisUtil;
@Autowired
private MonitorAlarm monitorAlarm;
@Autowired
private ISysServerService serverService;
// 新增|修改邮箱服务器信息后 异步更新其状态信息
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..)) || " +
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.create(..))")
public void updateEamilStatus(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0){
SysEmail email = (SysEmail) args[0];
String id = email.getId();
String name = email.getName();
Integer isQiye = email.getIsQiye();
boolean isConn = EmailUtil.isConnection(email);
NameValue nameValue = new NameValue(name, isConn);
if (ObjectUtil.isNotNull(isQiye) && isQiye == IS.getValue()){
String username = email.getUsername();
String[] info = StrUtil.split(username, SymbolConstant.AT);
QiyeOpenPlatSDK platSDK = InstanceSDK.getInstance();
if (ArrayUtil.length(info) == 2 && ObjectUtil.isNotNull(platSDK)){
String accountName = info[0];
String domain = info[1];
RParam param = new RParam(accountName, domain);
AccountInfo accountInfo = Account.getMailAccountInfo(platSDK, param);
Integer usedQuota = accountInfo.getUsedQuota();
nameValue.setUsage(usedQuota);
}
}
String statusKey = RedisConstant.EMAIL_STATUS;
redisUtil.hset(statusKey, id, nameValue);
}
}
/*
* 新增|修改数据源信息后 异步更新其状态信息
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.update(..)) || " +
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.create(..))")
public void updateDatabaseStatus(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0) {
SysDatabase database = (SysDatabase) args[0];
String id = database.getId();
String name = database.getName();
String dbUrl = database.getDbUrl();
String dbDriver = database.getDbDriver();
String dbUsername = database.getDbUsername();
String dbPassword = database.getDbPassword();
boolean isConn = JDBCUtil.isConnection(dbUrl, dbDriver, dbUsername, dbPassword);
String statusKey = RedisConstant.DATABASE_STATUS;
redisUtil.hset(statusKey, id, new NameValue(name, isConn));
}
}
/*
* 新增|修改服务器信息后 异步更新其状态信息
* 更新服务器的状态(-1未知 1正常 2离线 3告警)和它对应的系统监控的HostId
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.update(..)) || " +
"execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.create(..))")
public void updateServerStatus(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0) {
String key = RedisConstant.SERVER_STATUS;
String status = ServerStatus.UNKNOWN.getValue(); // 初始值为-1
SysServer server = (SysServer) args[0];
String id = server.getId();
String name = server.getName();
String ipAddress = server.getIpAddress();
try {
String token = ManageUtil.getToken();
Servers servers = monitorAlarm.listApp(ipAddress, MonitorConstant.SERVER_APP, token).getResult();
// 获取所有监控主机信息
List<Host> hosts = servers.getRecords();
for (Host host : hosts) {
String code = host.getCode();
if (!StrUtil.equals(ipAddress, code))
continue;
server.setHostId(host.getHostId());
status = host.getStatus();
}
// 更新该服务器状态信息
redisUtil.hset(key, id, new NameString(name, status));
// 更新该服务器的HostId
serverService.updateById(server);
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
}catch (Exception e){
log.error("向运管系统查询Hosts信息异常: {}", e.getMessage());
}
}
}
}

View File

@ -23,6 +23,11 @@ public interface MonitorAlarm {
Result<Servers> listApp(@RequestParam("type") String type,
@RequestHeader("X-Access-Token") String token);
@GetMapping("/omms/device/monitor/list") // 获取所有 服务器/数据库服务 信息
Result<Servers> listApp(@RequestParam("code") String code,
@RequestParam("type") String type,
@RequestHeader("X-Access-Token") String token);
@GetMapping("/omms/device/monitor/list") // 获取所有在线 服务器/数据库服务 信息
Result<Servers> listOnApp(@RequestParam("status") String status,
@RequestParam("type") String type,

View File

@ -62,7 +62,7 @@ public class Account {
R<Map<String, Object>> result = platSDK.commonInvoke(reqParam, AccountAPI.DOMAINS);
Map<String, Object> data = result.getData();
if (ObjectUtil.isNull(data))
return ListUtil.toList("ndc.org.cn","");
return ListUtil.toList("ndc.org.cn","nrl.org.cn");
List<Domain> domains = BeanUtil.mapToBean(data, Domains.class, CopyOptions.create())
.getDomainList();
return domains.stream().map(Domain::getDomain).collect(Collectors.toList());

View File

@ -105,7 +105,7 @@ public class CalculateConcServiceImpl implements CalculateConcService {
return nuclideAvgService.saveBatch(manAvgs);
}catch (Throwable e){
e.printStackTrace();
log.error("核素浓度计算过程异常,异常信息为:{}",e.getMessage());
log.error("核素浓度计算过程异常: {}", e.getMessage());
return false;
}
}

View File

@ -129,10 +129,8 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
}
}
boolean success = save(sysDatabase);
if (success){
// saveOrUpdateStatus(sysDatabase);
if (success)
return Result.OK(Prompt.ADD_SUCC);
}
return Result.error(Prompt.ADD_ERR);
}
@ -172,10 +170,8 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
}
}
boolean success = updateById(sysDatabase);
if (success) {
// saveOrUpdateStatus(sysDatabase);
if (success)
return Result.OK(Prompt.UPDATE_SUCC);
}
return Result.error(Prompt.UPDATE_ERR);
}
@ -404,21 +400,6 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
return ReUtil.getGroup1(regex, url);
}
/*
* 新增|修改数据源信息后更新数据源状态值
* */
private void saveOrUpdateStatus(SysDatabase database){
String id = database.getId();
String name = database.getName();
String dbUrl = database.getDbUrl();
String dbDriver = database.getDbDriver();
String dbUsername = database.getDbUsername();
String dbPassword = database.getDbPassword();
boolean isConn = JDBCUtil.isConnection(dbUrl, dbDriver, dbUsername, dbPassword);
String statusKey = RedisConstant.DATABASE_STATUS;
redisUtil.hset(statusKey, id, new NameValue(name, isConn));
}
/*
* 删除指定id的数据源的状态值
* */

View File

@ -30,6 +30,7 @@ import org.jeecg.modules.qiyeEmail.service.Account;
import org.jeecg.modules.service.IAlarmRuleService;
import org.jeecg.modules.service.ISysEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -127,7 +128,6 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
sysEmail.setIsQiye(isQiye(username));
boolean success = save(sysEmail);
if (success) {
// saveOrUpdateStatus(sysEmail);
return Result.OK(Prompt.ADD_SUCC);
}
return Result.error(Prompt.ADD_ERR);
@ -150,7 +150,6 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
sysEmail.setIsQiye(isQiye(username));
boolean success = updateById(sysEmail);
if (success) {
// saveOrUpdateStatus(sysEmail);
// 更新邮箱发件服务器到Redis
// updateSender();
return Result.OK(Prompt.UPDATE_SUCC);
@ -265,17 +264,6 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
return getById(id).getName();
}
/*
* 新增|修改邮箱服务信息后更新邮箱服务状态值
* */
private void saveOrUpdateStatus(SysEmail email){
String id = email.getId();
String name = email.getName();
boolean isConn = EmailUtil.isConnection(email);
String statusKey = RedisConstant.EMAIL_STATUS;
redisUtil.hset(statusKey, id, new NameValue(name, isConn));
}
/*
* 删除指定id的邮箱服务器的状态值
* */
@ -292,7 +280,7 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
if (ArrayUtil.length(info) < 2)
return Qiye.NOT.getValue();
String domain = info[1];
List<String> domains = ListUtil.toList("ndc.org.cn","");
List<String> domains = ListUtil.toList("ndc.org.cn","nrl.org.cn");
boolean contains = CollUtil.contains(domains, domain);
return contains ? Qiye.IS.getValue() : Qiye.NOT.getValue();
}

View File

@ -150,7 +150,6 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
if (CollUtil.isNotEmpty(list(wrapper)))
return Result.error("IP Address" + Prompt.NOT_REPEAT);
// boolean success = saveOrUpatedStatus(sysServer);
boolean success = this.saveOrUpdate(sysServer);
if (success)
return Result.OK(Prompt.ADD_SUCC);
@ -177,7 +176,6 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
if (CollUtil.isNotEmpty(list(wrapper)))
return Result.error("IP Address" + Prompt.NOT_REPEAT);
}
// boolean success = saveOrUpatedStatus(sysServer);
boolean success = this.saveOrUpdate(sysServer);
if (success)
return Result.OK(Prompt.UPDATE_SUCC);
@ -376,38 +374,6 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
redisUtil.hmset(key, values);
}
/*
* 更新服务器的状态(-1未知 1正常 2离线 3告警)和它对应的系统监控的HostId
* */
private boolean saveOrUpatedStatus(SysServer server){
String key = RedisConstant.SERVER_STATUS;
String status = ServerStatus.UNKNOWN.getValue(); // 初始值为-1
String name = server.getName();
try {
String token = ManageUtil.getToken();
Servers servers = monitorAlarm.listApp(MonitorConstant.SERVER_APP, token).getResult();
String ipAddress = server.getIpAddress();
// 获取所有监控主机信息
List<Host> hosts = servers.getRecords();
for (Host host : hosts) {
String code = host.getCode();
if (!StrUtil.equals(ipAddress, code))
continue;
server.setHostId(host.getHostId());
status = host.getStatus();
}
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询Hosts信息异常: Token失效,已刷新Token");
}catch (Exception e){
log.error("向运管系统查询Hosts信息异常: {}", e.getMessage());
}
boolean success = this.saveOrUpdate(server);
String id = server.getId();
redisUtil.hset(key, id, new NameString(name, status));
return success;
}
private void delStatus(String id){
String key = RedisConstant.SERVER_STATUS;
if (redisUtil.hHasKey(key, id))

View File

@ -14,12 +14,14 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
@EnableAsync
@SpringBootApplication
@EnableFeignClients(basePackages = {"org.jeecg"})
@EnableScheduling