重写实例状态检测功能-新增不带协议的URL检测和不带协议不带端口号的URL检测功能
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.mzaxd.noodles.job;
|
||||
|
||||
import cn.hutool.http.HttpException;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.mzaxd.noodles.constant.RedisConstant;
|
||||
@@ -12,6 +11,7 @@ import com.mzaxd.noodles.domain.entity.HostMachine;
|
||||
import com.mzaxd.noodles.service.*;
|
||||
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.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -76,10 +76,18 @@ public class CheckInstancesStatus {
|
||||
for (Container container : containers) {
|
||||
futures.add(executor.submit(() -> {
|
||||
try {
|
||||
if (!StringUtils.hasText(container.getWebUi())) {
|
||||
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
|
||||
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
|
||||
return container;
|
||||
}
|
||||
//通过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 container;
|
||||
}
|
||||
}
|
||||
//通过HTTP请求判断是否在线
|
||||
HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true);
|
||||
log.info("[实例状态检测]:与{}建立连接成功", container.getName());
|
||||
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
|
||||
@@ -131,10 +139,19 @@ public class CheckInstancesStatus {
|
||||
List<HostMachine> vms = hostMachineService.list(vmWrapper);
|
||||
vms.forEach(vm -> {
|
||||
try {
|
||||
if (!StringUtils.hasText(vm.getManageIp())) {
|
||||
if (!StringUtils.hasText(vm.getServerAddress()) && !StringUtils.hasText(vm.getServerAddress())) {
|
||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_UNKNOWN);
|
||||
return;
|
||||
}
|
||||
//通过Ping的方式判断是否在线
|
||||
if (StringUtils.hasText(vm.getServerAddress())) {
|
||||
if (UrlUtil.isHostOnline(vm.getServerAddress())){
|
||||
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//通过HTTP请求的方式判断是否在线
|
||||
HttpRequest.get(vm.getManageIp()).setConnectionTimeout(5000).execute(true);
|
||||
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
|
||||
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.mzaxd.noodles.util;
|
||||
|
||||
import com.mzaxd.noodles.constant.UrlConstant;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author root
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UrlUtil {
|
||||
|
||||
public static String getUrl(String protocol, String ip, String port, String path) {
|
||||
@@ -33,5 +38,26 @@ public class UrlUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getHostname(String address) {
|
||||
String[] parts = address.split(":");
|
||||
return parts[0];
|
||||
}
|
||||
|
||||
public static boolean isHostOnline(String ipAddress) throws IOException {
|
||||
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
||||
// 超时时间为5秒
|
||||
return inetAddress.isReachable(5000);
|
||||
}
|
||||
|
||||
public static boolean isServiceOnline(String serverName, int port) throws IOException {
|
||||
Socket socket = new Socket(serverName, port);
|
||||
log.info("[实例状态检测]:" + serverName + "服务已经开启");
|
||||
socket.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int getPort(String address) {
|
||||
String[] parts = address.split(":");
|
||||
return Integer.parseInt(parts[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,25 @@ import com.mzaxd.noodles.service.*;
|
||||
import com.mzaxd.noodles.util.CpuUtil;
|
||||
import com.mzaxd.noodles.util.RedisCache;
|
||||
import com.mzaxd.noodles.util.SystemInfoUtils;
|
||||
import com.mzaxd.noodles.util.UrlUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.Socket;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@@ -452,4 +460,52 @@ class NoodlesApplicationTests {
|
||||
double seconds = (double) elapsedTime / 1_000_000_000.0; // 将纳秒转换为秒
|
||||
System.out.println("代码执行时间:" + seconds + " 秒");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pingTest() {
|
||||
String ipAddress = "192.168.1.90"; // 要检查的IP地址
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
||||
boolean isReachable = inetAddress.isReachable(5000); // 超时时间为5秒
|
||||
if (isReachable) {
|
||||
System.out.println("虚拟机在线");
|
||||
} else {
|
||||
System.out.println("虚拟机不在线");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serviceTest() {
|
||||
try {
|
||||
String serverName = "wiz.elizaveta.top";
|
||||
int port = 700;
|
||||
Socket socket = new Socket(serverName, port);
|
||||
System.out.println("MySQL服务已经开启");
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("MySQL服务没有开启或者远程服务器无法访问");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hostnameTest() {
|
||||
String hostname = UrlUtil.getHostname("192.168.1.90:8080");
|
||||
System.out.println(hostname);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void portTest() {
|
||||
String address = "192.168.1.103:3306";
|
||||
String regex = ":(\\d+)$"; // 匹配冒号后面的数字
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(address);
|
||||
if (matcher.find()) {
|
||||
System.out.println(matcher.group(1));
|
||||
} else {
|
||||
System.out.println("解析失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user