diff --git a/sf-common/src/main/java/com/sf/common/utils/URLUtils.java b/sf-common/src/main/java/com/sf/common/utils/URLUtils.java index 7d80329..b361d86 100644 --- a/sf-common/src/main/java/com/sf/common/utils/URLUtils.java +++ b/sf-common/src/main/java/com/sf/common/utils/URLUtils.java @@ -1,15 +1,14 @@ package com.sf.common.utils; import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; import org.hibernate.validator.internal.util.DomainNameUtil; -import javax.security.auth.x500.X500Principal; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; /** * 功能描述: URL工具类 @@ -17,6 +16,7 @@ import java.util.regex.PatternSyntaxException; * @author a_kun * @date 2024/4/23 14:04 */ +@Slf4j @UtilityClass public class URLUtils { @@ -79,20 +79,20 @@ public class URLUtils { return valueHolder; } - public static boolean isUrlAlive(String urlString,int connectTimeout,int readTimeout) { + public static boolean isUrlAlive(String urlString,int timeout) { HttpURLConnection connection = null; try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setConnectTimeout(connectTimeout); // 设置连接超时时间 - connection.setReadTimeout(readTimeout); // 设置读取超时时间 + connection.setRequestMethod("HEAD"); + connection.setConnectTimeout(timeout); // 设置连接超时时间 + connection.setReadTimeout(timeout); // 设置读取超时时间 int responseCode = connection.getResponseCode(); // 判断响应码是否表示成功(200-399 范围内) return (200 <= responseCode && responseCode <= 399); } catch (Exception e) { - e.printStackTrace(); + log.error("URL活跃检测发生错误!url:" + urlString + " ; message:" + e.getMessage()); return false; } finally { if (connection != null) { diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayRouteService.java b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayRouteService.java index 1f9a65e..af37463 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayRouteService.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayRouteService.java @@ -46,6 +46,14 @@ public interface IGatewayRouteService */ public int updateGatewayRoute(InsertOrUpdateGatewayRouteDTO gatewayRoute); + /** + * 修改路由管理 + * + * @param gatewayRoute 路由管理 + * @return 结果 + */ + public int updateGatewayRoute(GatewayRoute gatewayRoute); + /** * 批量删除路由管理 * diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java index 2dc2329..e3117b1 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java @@ -23,6 +23,7 @@ import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import javax.annotation.Resource; import javax.validation.constraints.NotBlank; /** @@ -34,10 +35,10 @@ import javax.validation.constraints.NotBlank; @Service public class GatewayRouteServiceImpl implements IGatewayRouteService { - @Autowired + @Resource private GatewayRouteMapper gatewayRouteMapper; - @Autowired + @Resource private IGatewayServerService gatewayServerService; /** @@ -154,17 +155,22 @@ public class GatewayRouteServiceImpl implements IGatewayRouteService gatewayRoute.setRouteActiveMonitoringPath(dto.getRouteActiveMonitoringPath()); gatewayRoute.setRouteActiveMonitoringTimeout(dto.getRouteActiveMonitoringTimeout()); gatewayRoute.setModified(SecurityUtils.getUsername()); - gatewayRoute.setUpdateTime(DateUtils.getNowDate()); if (GatewayDataStatus.DISABLE.getCode().equals(dto.getRouteStatusActiveMonitoring())){ gatewayRoute.setRouteActiveStatus(RouteActiveStatus.NOT_ENABLED.getCode()); } try { - return gatewayRouteMapper.updateGatewayRoute(gatewayRoute); + return updateGatewayRoute(gatewayRoute); }catch (DuplicateKeyException e){ throw new IllegalArgumentException("路由名称不可重复!"); } } + @Override + public int updateGatewayRoute(GatewayRoute gatewayRoute) { + gatewayRoute.setUpdateTime(DateUtils.getNowDate()); + return gatewayRouteMapper.updateGatewayRoute(gatewayRoute); + } + /** * 批量删除路由管理 * diff --git a/sf-service/src/main/java/com/sf/service/gateway/task/GatewayRouteURLMonitor.java b/sf-service/src/main/java/com/sf/service/gateway/task/GatewayRouteURLMonitor.java new file mode 100644 index 0000000..629038c --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/task/GatewayRouteURLMonitor.java @@ -0,0 +1,74 @@ +package com.sf.service.gateway.task; + +import cn.hutool.core.thread.ExecutorBuilder; +import cn.hutool.core.thread.RejectPolicy; +import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson2.JSON; +import com.sf.common.utils.URLUtils; +import com.sf.service.gateway.domain.GatewayRoute; +import com.sf.service.gateway.enums.GatewayDataStatus; +import com.sf.service.gateway.enums.RouteActiveStatus; +import com.sf.service.gateway.service.IGatewayRouteService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 功能描述: 路由url活跃监测 + * + * @author a_kun + * @date 2024/5/6 10:11 + */ +@Slf4j +@Component("gatewayRouteURLMonitor") +public class GatewayRouteURLMonitor { + + private static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2; + + + private static ThreadPoolExecutor threadPoolExecutor = ExecutorBuilder.create() + .setCorePoolSize(CORE_POOL_SIZE).setMaxPoolSize(CORE_POOL_SIZE + 2) + .setWorkQueue(new LinkedBlockingQueue<>(1024)) + .setThreadFactory(ThreadUtil.createThreadFactory("GatewayRouteURLMonitor")) + .setHandler(RejectPolicy.DISCARD_OLDEST.getValue()) + .build(); + + @Resource + private IGatewayRouteService gatewayRouteService; + + public void monitor() { + log.info("开始路由url活跃监测任务"); + GatewayRoute query = new GatewayRoute(); + query.setRouteStatusActiveMonitoring(GatewayDataStatus.ENABLE.getCode()); + List routeList = gatewayRouteService.selectGatewayRouteList(query); + routeList.forEach(route -> threadPoolExecutor.execute(() -> { + log.info("路由规则:" + route.getRouteName() + "开始路由url活跃监测"); + if (StrUtil.isNotBlank(route.getRouteContent())) { + boolean urlAlive = true; + List routeContentList = JSON.parseArray(route.getRouteContent(), GatewayRoute.RouteContent.class); + for (GatewayRoute.RouteContent content : routeContentList) { + // 有一个异常就不用监测其他的了 + if (!urlAlive) { + break; + } + String urlString = content.getServerAddress() + route.getRouteActiveMonitoringPath(); + log.info("URL:" + urlString); + urlAlive = URLUtils.isUrlAlive(urlString, route.getRouteActiveMonitoringTimeout().intValue()); + } + RouteActiveStatus routeActiveStatus = urlAlive ? RouteActiveStatus.NORMAL : RouteActiveStatus.ABNORMAL; + GatewayRoute update = new GatewayRoute(); + update.setId(route.getId()); + update.setRouteActiveStatus(routeActiveStatus.getCode()); + gatewayRouteService.updateGatewayRoute(update); + } + })); + log.info("结束路由url活跃监测任务"); + } + + +} diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayRouteMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayRouteMapper.xml index cde9041..ecdb4df 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayRouteMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayRouteMapper.xml @@ -35,6 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and route_name like concat('%', #{routeName}, '%') and request_protocol = #{requestProtocol} and created = #{created} + and route_status_active_monitoring = #{routeStatusActiveMonitoring} and create_time between #{params.beginCreateTime} and #{params.endCreateTime} and id in