增加新功能 控制台显示受实例掉线影响的服务列表

This commit is contained in:
2023-03-08 10:50:58 +08:00
parent cea2a3f57a
commit 341bf5769b
5 changed files with 175 additions and 17 deletions
@@ -6,6 +6,7 @@ import com.mzaxd.noodles.service.EveryDayDataService;
import com.mzaxd.noodles.service.HostDetectorService; import com.mzaxd.noodles.service.HostDetectorService;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -52,4 +53,9 @@ public class DashboardController {
return dashboardService.getRecentConsoleList(); return dashboardService.getRecentConsoleList();
} }
@GetMapping("/getAffectedServirList")
public ResponseResult getAffectedServirList() {
return dashboardService.getAffectedServirList();
}
} }
@@ -0,0 +1,55 @@
package com.mzaxd.noodles.domain.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.List;
/**
* @author Mzaxd
* @since 2023-03-08 10:23
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class AffectedServirListVo {
/**
* 主键id
*/
private Long id;
/**
* 名称
*/
private String name;
/**
* 描述
*/
private String description;
/**
* 图像
*/
private String avatar;
/**
* 备注
*/
private String remark;
/**
* 所有关联的host
*/
private List<HostVo> hosts;
/**
* 所有关联的容器
*/
private List<ContainerVo> containers;
}
@@ -12,11 +12,9 @@ import com.mzaxd.noodles.domain.entity.HostMachine;
import com.mzaxd.noodles.service.*; import com.mzaxd.noodles.service.*;
import com.mzaxd.noodles.util.RedisCache; import com.mzaxd.noodles.util.RedisCache;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -70,7 +68,7 @@ public class CheckInstancesStatus{
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (Objects.nonNull(set)){ if (!CollectionUtils.isEmpty(set)){
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(container.getId().toString())) { if (set.contains(container.getId().toString())) {
return; return;
@@ -108,7 +106,7 @@ public class CheckInstancesStatus{
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS);
if (Objects.nonNull(set)){ if (!CollectionUtils.isEmpty(set)){
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(vm.getId().toString())) { if (set.contains(vm.getId().toString())) {
return; return;
@@ -148,7 +146,7 @@ public class CheckInstancesStatus{
} }
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
if (Objects.nonNull(set)){ if (!CollectionUtils.isEmpty(set)){
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(hostMachine.getId().toString())) { if (set.contains(hostMachine.getId().toString())) {
return; return;
@@ -19,4 +19,13 @@ public interface DashboardService {
* @return * @return
*/ */
ResponseResult getRecentConsoleList(); ResponseResult getRecentConsoleList();
/**
* 获取所有受影响的服务
*
* @return ResponseResult
* @author mzaxd
* @date 2023/3/8 9:43
*/
ResponseResult getAffectedServirList();
} }
@@ -1,25 +1,21 @@
package com.mzaxd.noodles.service.impl; package com.mzaxd.noodles.service.impl;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzaxd.noodles.constant.RedisConstant;
import com.mzaxd.noodles.domain.ResponseResult; import com.mzaxd.noodles.domain.ResponseResult;
import com.mzaxd.noodles.domain.entity.AuditLog; import com.mzaxd.noodles.domain.entity.*;
import com.mzaxd.noodles.domain.entity.EveryDayData; import com.mzaxd.noodles.domain.vo.*;
import com.mzaxd.noodles.domain.vo.RecentConsoleListVo;
import com.mzaxd.noodles.enums.AppHttpCodeEnum;
import com.mzaxd.noodles.enums.OperationEnum; import com.mzaxd.noodles.enums.OperationEnum;
import com.mzaxd.noodles.service.*; import com.mzaxd.noodles.service.*;
import com.mzaxd.noodles.util.BeanCopyUtils; import com.mzaxd.noodles.util.BeanCopyUtils;
import com.mzaxd.noodles.util.RedisCache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -46,6 +42,15 @@ public class DashboardServiceImpl implements DashboardService {
@Resource @Resource
private SshLinkService sshLinkService; private SshLinkService sshLinkService;
@Resource
private RedisCache redisCache;
@Resource
private ServirHostService servirHostService;
@Resource
private ServirContainerService servirContainerService;
@Override @Override
public ResponseResult getInstancesRealTimeData() { public ResponseResult getInstancesRealTimeData() {
HashMap<String, Object> result = new HashMap<>(); HashMap<String, Object> result = new HashMap<>();
@@ -88,4 +93,89 @@ public class DashboardServiceImpl implements DashboardService {
} }
return ResponseResult.okResult(); return ResponseResult.okResult();
} }
@Override
public ResponseResult getAffectedServirList() {
HashSet<Long> affectedServirIds = new HashSet<>();
//Key是受影响的服务的id,也就是affectedServirId
HashMap<Long, List<Long>> offlineContainer = new HashMap<>();
HashMap<Long, List<Long>> offlineHost = new HashMap<>();
List<AffectedServirListVo> result = new ArrayList<>();
//判断Redis里面有没有掉线容器Id 如果没有就说明没有受影响的服务
Set<String> containerSet = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (!CollectionUtils.isEmpty(containerSet)){
containerSet.forEach(s -> {
LambdaQueryWrapper<ServirContainer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ServirContainer::getContainerId, Long.valueOf(s));
//根据Redis中掉线容器Id来查找所有关联的服务
List<ServirContainer> servirContainers = servirContainerService.list(lambdaQueryWrapper);
//填充受影响的服务Id和 服务-容器关系 的Map
servirContainers.forEach(servirContainer -> {
Long servirId = servirContainer.getServirId();
Long containerId = servirContainer.getContainerId();
affectedServirIds.add(servirId);
if (Objects.nonNull(offlineContainer.get(servirId))) {
offlineContainer.get(servirId).add(containerId);
} else {
List<Long> containerIds = new ArrayList<>();
containerIds.add(containerId);
offlineContainer.put(servirId, containerIds);
}
});
});
}
//判断Redis里面有没有掉线的物理机Id或虚拟机Id 如果没有就说明没有受影响的服务
Set<String> vmSet = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS);
Set<String> serverSet = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
Set<String> hostSet = new HashSet<>();
hostSet.addAll(vmSet);
hostSet.addAll(serverSet);
if (!CollectionUtils.isEmpty(hostSet)){
hostSet.forEach(s -> {
LambdaQueryWrapper<ServirHost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ServirHost::getHostId, Long.valueOf(s));
//根据Redis中掉线hostId来查找所有关联的服务
List<ServirHost> servirHosts = servirHostService.list(lambdaQueryWrapper);
//填充受影响的服务Id和 服务-容器关系 的Map
servirHosts.forEach(servirHost -> {
Long servirId = servirHost.getServirId();
Long hostId = servirHost.getHostId();
affectedServirIds.add(servirId);
if (Objects.nonNull(offlineHost.get(servirId))) {
offlineHost.get(servirId).add(hostId);
} else {
List<Long> hostIds = new ArrayList<>();
hostIds.add(hostId);
offlineHost.put(servirId, hostIds);
}
});
});
}
affectedServirIds.forEach(servirId -> {
//查询对应服务
Servir servir = servirService.getById(servirId);
AffectedServirListVo affectedServirListVo = BeanCopyUtils.copyBean(servir, AffectedServirListVo.class);
//根据offlineContainer查找对应的容器
List<Long> containerIds = offlineContainer.get(servirId);
//根据服务Id查询所有掉线container
if (!CollectionUtils.isEmpty(containerIds)) {
List<Container> offlineContainers = containerIds.stream().map(containerId -> containerService.getById(containerId)).collect(Collectors.toList());
List<ContainerVo> containerVos = BeanCopyUtils.copyBeanList(offlineContainers, ContainerVo.class);
affectedServirListVo.setContainers(containerVos);
}
//根据offlineContainer查找对应的容器
List<Long> hostIds = offlineHost.get(servirId);
//根据offlineHost查找对应的物理机或虚拟机
if (!CollectionUtils.isEmpty(hostIds)) {
List<HostMachine> offlineHosts = hostIds.stream().map(hostId -> hostMachineService.getById(hostId)).collect(Collectors.toList());
List<HostVo> hostVos = BeanCopyUtils.copyBeanList(offlineHosts, HostVo.class);
affectedServirListVo.setHosts(hostVos);
}
result.add(affectedServirListVo);
});
return ResponseResult.okResult(result);
}
} }