From e30c8e597ce9f66d7b4aea889b98a8877b2e9ae5 Mon Sep 17 00:00:00 2001 From: Mzaxd Date: Thu, 23 Mar 2023 12:10:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E5=AE=9E=E4=BE=8B=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=A3=80=E6=B5=8B=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../noodles/job/CheckInstancesStatus.java | 107 +++++++++++------- .../noodles/service/NotificationService.java | 36 ++++++ .../service/impl/NotificationServiceImpl.java | 59 +++++----- .../java/com/mzaxd/noodles/util/UrlUtil.java | 2 +- src/main/resources/application-dev.yml | 15 ++- .../noodles/NoodlesApplicationTests.java | 57 +++++++++- 6 files changed, 201 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java b/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java index 869f9aa..f283be4 100644 --- a/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java +++ b/src/main/java/com/mzaxd/noodles/job/CheckInstancesStatus.java @@ -13,16 +13,14 @@ import com.mzaxd.noodles.util.CpuUtil; import com.mzaxd.noodles.util.RedisCache; import com.mzaxd.noodles.util.UrlUtil; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.concurrent.*; /** @@ -50,6 +48,9 @@ public class CheckInstancesStatus { @Resource private RedisCache redisCache; + @Resource + private RedisTemplate redisTemplate; + /** * 检查实例状态并且发送对应的提醒 */ @@ -96,21 +97,29 @@ public class CheckInstancesStatus { container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); - if (!CollectionUtils.isEmpty(set)) { - // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 - if (set.contains(container.getId().toString())) { + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } + // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 + if (set.contains(container.getId().toString())) { + return container; + } else { + // 根据实例对应的提醒方式进行提醒 + if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { return container; - } else { - // 根据实例对应的提醒方式进行提醒 - if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { - return container; - } else { - notificationService.sendContainerOfflineNotification(container.getId()); - } + } + if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) { + notificationService.sendContainerOfflineNotification(container.getId()); + set.add(container.getId().toString()); + } + if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) { + notificationService.sendContainerOfflineEmail(container.getId()); + } + if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) { + notificationService.sendContainerOfflineNotificationEmail(container.getId()); + set.add(container.getId().toString()); } } - // 存入 Redis - set.add(container.getId().toString()); redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set); } return container; @@ -145,7 +154,7 @@ public class CheckInstancesStatus { } //通过Ping的方式判断是否在线 if (StringUtils.hasText(vm.getServerAddress())) { - if (UrlUtil.isHostOnline(vm.getServerAddress())){ + if (UrlUtil.isHostOnline(vm.getServerAddress())) { log.info("[实例状态检测]:与{}建立连接成功", vm.getName()); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE); return; @@ -160,21 +169,29 @@ public class CheckInstancesStatus { vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS); - if (!CollectionUtils.isEmpty(set)) { - //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 - if (set.contains(vm.getId().toString())) { + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } + //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 + if (set.contains(vm.getId().toString())) { + return; + } else { + //根据实例对应的提醒方式进行提醒 + if (vm.getNotify().equals(SystemConstant.NOTIFY_NO)) { return; - } else { - //根据实例对应的提醒方式进行提醒 - if (vm.getNotify().equals(SystemConstant.NOTIFY_NO)) { - return; - } else { - notificationService.sendVmOfflineNotification(vm.getId()); - } + } + if (vm.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) { + notificationService.sendContainerOfflineNotification(vm.getId()); + set.add(vm.getId().toString()); + } + if (vm.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) { + notificationService.sendContainerOfflineEmail(vm.getId()); + } + if (vm.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) { + notificationService.sendContainerOfflineNotificationEmail(vm.getId()); + set.add(vm.getId().toString()); } } - //存入redis - set.add(vm.getId().toString()); redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set); } }); @@ -200,21 +217,29 @@ public class CheckInstancesStatus { } //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS); - if (!CollectionUtils.isEmpty(set)) { - //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 - if (set.contains(hostMachine.getId().toString())) { + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } + //如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 + if (set.contains(hostMachine.getId().toString())) { + return; + } else { + //根据实例对应的提醒方式进行提醒 + if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_NO)) { return; - } else { - //根据实例对应的提醒方式进行提醒 - if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_NO)) { - return; - } else { - notificationService.sendHostOfflineNotification(hostMachine.getId()); - } + } + if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) { + notificationService.sendContainerOfflineNotification(hostMachine.getId()); + set.add(hostMachine.getId().toString()); + } + if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) { + notificationService.sendContainerOfflineEmail(hostMachine.getId()); + } + if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) { + notificationService.sendContainerOfflineNotificationEmail(hostMachine.getId()); + set.add(hostMachine.getId().toString()); } } - //存入redis - set.add(hostMachine.getId().toString()); redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set); } }); diff --git a/src/main/java/com/mzaxd/noodles/service/NotificationService.java b/src/main/java/com/mzaxd/noodles/service/NotificationService.java index 6cbf40d..3ee8c30 100644 --- a/src/main/java/com/mzaxd/noodles/service/NotificationService.java +++ b/src/main/java/com/mzaxd/noodles/service/NotificationService.java @@ -17,18 +17,54 @@ public interface NotificationService extends IService { */ void sendContainerOfflineNotification(Long id); + /** + * 发送容器掉线通知和邮件 + * @param id + */ + void sendContainerOfflineNotificationEmail(Long id); + + /** + * 发送容器掉线邮件 + * @param id + */ + void sendContainerOfflineEmail(Long id); + /** * 发送虚拟机掉线通知 * @param id */ void sendVmOfflineNotification(Long id); + /** + * 发送虚拟机掉线邮件 + * @param id + */ + void sendVmOfflineEmail(Long id); + + /** + * 发送虚拟机掉线通知和邮件 + * @param id + */ + void sendVmOfflineNotificationEmail(Long id); + /** * 发送物理机掉线通知 * @param id */ void sendHostOfflineNotification(Long id); + /** + * 发送物理机掉线邮件 + * @param id + */ + void sendHostOfflineEmail(Long id); + + /** + * 发送物理机掉线通知和邮件 + * @param id + */ + void sendHostOfflineNotificationEmail(Long id); + /** * 提醒列表 diff --git a/src/main/java/com/mzaxd/noodles/service/impl/NotificationServiceImpl.java b/src/main/java/com/mzaxd/noodles/service/impl/NotificationServiceImpl.java index 7b88663..1484edb 100644 --- a/src/main/java/com/mzaxd/noodles/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/mzaxd/noodles/service/impl/NotificationServiceImpl.java @@ -55,15 +55,10 @@ public class NotificationServiceImpl extends ServiceImpl notificationWrapper = new LambdaQueryWrapper<>(); @@ -219,13 +231,6 @@ public class NotificationServiceImpl extends ServiceImpl containers = containerService.list(); + + containers.forEach(container -> { + try { + if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) { + container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN); + return; + } + //通过Socket(IP+端口)判断是否在线 + if (StringUtils.hasText(container.getServerAddress())) { + if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) { + container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + return; + } + } + //通过HTTP请求判断是否在线 + HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true); + log.info("[实例状态检测]:与{}建立连接成功", container.getName()); + container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING); + } catch (Exception exception) { + log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName()); + container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); + // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 + Set set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); + if (CollectionUtils.isEmpty(set)) { + set = new HashSet<>(); + } + // 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 + if (set.contains(container.getId().toString())) { + return; + } else { + // 根据实例对应的提醒方式进行提醒 + if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { + return; + } + if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) { + notificationService.sendContainerOfflineNotification(container.getId()); + set.add(container.getId().toString()); + } + if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) { + notificationService.sendContainerOfflineEmail(container.getId()); + } + if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) { + notificationService.sendContainerOfflineNotificationEmail(container.getId()); + set.add(container.getId().toString()); + } + } + redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set); + } + }); + } }