增加URL活跃监测任务
This commit is contained in:
parent
4d435267e2
commit
c4d902d288
@ -1,15 +1,14 @@
|
|||||||
package com.sf.common.utils;
|
package com.sf.common.utils;
|
||||||
|
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.hibernate.validator.internal.util.DomainNameUtil;
|
import org.hibernate.validator.internal.util.DomainNameUtil;
|
||||||
|
|
||||||
import javax.security.auth.x500.X500Principal;
|
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.regex.PatternSyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 功能描述: URL工具类
|
* 功能描述: URL工具类
|
||||||
@ -17,6 +16,7 @@ import java.util.regex.PatternSyntaxException;
|
|||||||
* @author a_kun
|
* @author a_kun
|
||||||
* @date 2024/4/23 14:04
|
* @date 2024/4/23 14:04
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class URLUtils {
|
public class URLUtils {
|
||||||
|
|
||||||
@ -79,20 +79,20 @@ public class URLUtils {
|
|||||||
return valueHolder;
|
return valueHolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isUrlAlive(String urlString,int connectTimeout,int readTimeout) {
|
public static boolean isUrlAlive(String urlString,int timeout) {
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
try {
|
try {
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
connection = (HttpURLConnection) url.openConnection();
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("HEAD");
|
||||||
connection.setConnectTimeout(connectTimeout); // 设置连接超时时间
|
connection.setConnectTimeout(timeout); // 设置连接超时时间
|
||||||
connection.setReadTimeout(readTimeout); // 设置读取超时时间
|
connection.setReadTimeout(timeout); // 设置读取超时时间
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
// 判断响应码是否表示成功(200-399 范围内)
|
// 判断响应码是否表示成功(200-399 范围内)
|
||||||
return (200 <= responseCode && responseCode <= 399);
|
return (200 <= responseCode && responseCode <= 399);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("URL活跃检测发生错误!url:" + urlString + " ; message:" + e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
|
@ -46,6 +46,14 @@ public interface IGatewayRouteService
|
|||||||
*/
|
*/
|
||||||
public int updateGatewayRoute(InsertOrUpdateGatewayRouteDTO gatewayRoute);
|
public int updateGatewayRoute(InsertOrUpdateGatewayRouteDTO gatewayRoute);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改路由管理
|
||||||
|
*
|
||||||
|
* @param gatewayRoute 路由管理
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public int updateGatewayRoute(GatewayRoute gatewayRoute);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除路由管理
|
* 批量删除路由管理
|
||||||
*
|
*
|
||||||
|
@ -23,6 +23,7 @@ import org.springframework.dao.DuplicateKeyException;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,10 +35,10 @@ import javax.validation.constraints.NotBlank;
|
|||||||
@Service
|
@Service
|
||||||
public class GatewayRouteServiceImpl implements IGatewayRouteService
|
public class GatewayRouteServiceImpl implements IGatewayRouteService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Resource
|
||||||
private GatewayRouteMapper gatewayRouteMapper;
|
private GatewayRouteMapper gatewayRouteMapper;
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private IGatewayServerService gatewayServerService;
|
private IGatewayServerService gatewayServerService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -154,17 +155,22 @@ public class GatewayRouteServiceImpl implements IGatewayRouteService
|
|||||||
gatewayRoute.setRouteActiveMonitoringPath(dto.getRouteActiveMonitoringPath());
|
gatewayRoute.setRouteActiveMonitoringPath(dto.getRouteActiveMonitoringPath());
|
||||||
gatewayRoute.setRouteActiveMonitoringTimeout(dto.getRouteActiveMonitoringTimeout());
|
gatewayRoute.setRouteActiveMonitoringTimeout(dto.getRouteActiveMonitoringTimeout());
|
||||||
gatewayRoute.setModified(SecurityUtils.getUsername());
|
gatewayRoute.setModified(SecurityUtils.getUsername());
|
||||||
gatewayRoute.setUpdateTime(DateUtils.getNowDate());
|
|
||||||
if (GatewayDataStatus.DISABLE.getCode().equals(dto.getRouteStatusActiveMonitoring())){
|
if (GatewayDataStatus.DISABLE.getCode().equals(dto.getRouteStatusActiveMonitoring())){
|
||||||
gatewayRoute.setRouteActiveStatus(RouteActiveStatus.NOT_ENABLED.getCode());
|
gatewayRoute.setRouteActiveStatus(RouteActiveStatus.NOT_ENABLED.getCode());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return gatewayRouteMapper.updateGatewayRoute(gatewayRoute);
|
return updateGatewayRoute(gatewayRoute);
|
||||||
}catch (DuplicateKeyException e){
|
}catch (DuplicateKeyException e){
|
||||||
throw new IllegalArgumentException("路由名称不可重复!");
|
throw new IllegalArgumentException("路由名称不可重复!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int updateGatewayRoute(GatewayRoute gatewayRoute) {
|
||||||
|
gatewayRoute.setUpdateTime(DateUtils.getNowDate());
|
||||||
|
return gatewayRouteMapper.updateGatewayRoute(gatewayRoute);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除路由管理
|
* 批量删除路由管理
|
||||||
*
|
*
|
||||||
|
@ -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<GatewayRoute> routeList = gatewayRouteService.selectGatewayRouteList(query);
|
||||||
|
routeList.forEach(route -> threadPoolExecutor.execute(() -> {
|
||||||
|
log.info("路由规则:" + route.getRouteName() + "开始路由url活跃监测");
|
||||||
|
if (StrUtil.isNotBlank(route.getRouteContent())) {
|
||||||
|
boolean urlAlive = true;
|
||||||
|
List<GatewayRoute.RouteContent> 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活跃监测任务");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -35,6 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="routeName != null and routeName != ''"> and route_name like concat('%', #{routeName}, '%')</if>
|
<if test="routeName != null and routeName != ''"> and route_name like concat('%', #{routeName}, '%')</if>
|
||||||
<if test="requestProtocol != null and requestProtocol != ''"> and request_protocol = #{requestProtocol}</if>
|
<if test="requestProtocol != null and requestProtocol != ''"> and request_protocol = #{requestProtocol}</if>
|
||||||
<if test="created != null "> and created = #{created}</if>
|
<if test="created != null "> and created = #{created}</if>
|
||||||
|
<if test="routeStatusActiveMonitoring != null "> and route_status_active_monitoring = #{routeStatusActiveMonitoring}</if>
|
||||||
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
|
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
|
||||||
<if test="params.ids != null and params.ids.size() > 0">
|
<if test="params.ids != null and params.ids.size() > 0">
|
||||||
and id in
|
and id in
|
||||||
|
Loading…
x
Reference in New Issue
Block a user