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 034ca21..7d80329 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 @@ -4,6 +4,7 @@ import lombok.experimental.UtilityClass; 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; @@ -78,6 +79,28 @@ public class URLUtils { return valueHolder; } + public static boolean isUrlAlive(String urlString,int connectTimeout,int readTimeout) { + HttpURLConnection connection = null; + try { + URL url = new URL(urlString); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(connectTimeout); // 设置连接超时时间 + connection.setReadTimeout(readTimeout); // 设置读取超时时间 + int responseCode = connection.getResponseCode(); + + // 判断响应码是否表示成功(200-399 范围内) + return (200 <= responseCode && responseCode <= 399); + } catch (Exception e) { + e.printStackTrace(); + return false; + } finally { + if (connection != null) { + connection.disconnect(); // 断开连接 + } + } + } + private static class ValueHolder { private final String protocol; private final String host; diff --git a/sf-framework/src/main/java/com/sf/framework/web/exception/GlobalExceptionHandler.java b/sf-framework/src/main/java/com/sf/framework/web/exception/GlobalExceptionHandler.java index cb7a93f..0769387 100644 --- a/sf-framework/src/main/java/com/sf/framework/web/exception/GlobalExceptionHandler.java +++ b/sf-framework/src/main/java/com/sf/framework/web/exception/GlobalExceptionHandler.java @@ -84,6 +84,16 @@ public class GlobalExceptionHandler return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); } + /** + * 请求参数错误 + */ + @ExceptionHandler(IllegalArgumentException.class) + public AjaxResult handleIllegalArgumentException(IllegalArgumentException e) + { + log.error(e.getMessage(), e); + return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage()); + } + /** * 拦截未知的运行时异常 */ diff --git a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayServerController.java b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayServerController.java index 643ce2a..4f6db01 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayServerController.java +++ b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayServerController.java @@ -1,24 +1,24 @@ package com.sf.service.gateway.controller; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; -import com.sf.service.gateway.domain.GatewayInterfaceInfo; -import com.sf.service.gateway.domain.GatewayRoute; -import com.sf.service.gateway.domain.GatewayServer; +import com.sf.service.gateway.domain.*; +import com.sf.service.gateway.domain.dto.QueryGatewayServerDTO; +import com.sf.service.gateway.domain.dto.QueryGatewayStrategyDTO; import com.sf.service.gateway.domain.dto.UpdateDataStatusDTO; import com.sf.service.gateway.domain.vo.GatewayInterfaceInfoListVO; import com.sf.service.gateway.domain.vo.GatewayServerDetailedVO; import com.sf.service.gateway.domain.vo.GatewayServerListVO; +import com.sf.service.gateway.domain.vo.GatewayStrategyListVO; import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.enums.GatewayServiceModel; import com.sf.service.gateway.service.IGatewayInterfaceInfoService; import com.sf.service.gateway.service.IGatewayRouteService; import com.sf.service.gateway.service.IGatewayServerService; +import com.sf.service.gateway.service.IGatewayStrategyService; import com.sf.service.index.util.AppUtils; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; @@ -57,6 +57,9 @@ public class GatewayServerController extends BaseController { @Autowired private IGatewayInterfaceInfoService gatewayInterfaceInfoService; + @Autowired + private IGatewayStrategyService gatewayStrategyService; + /** * 查询服务管理列表 */ @@ -78,6 +81,7 @@ public class GatewayServerController extends BaseController { vo.setServerName(item.getServerName()); vo.setServerAddress(GatewayServiceModel.ROUTE.getCode().equals(item.getServiceModel()) ? routeIdAndNameMap.get(item.getRouteId()) : item.getServerAddress()); + vo.setRemark(item.getRemark()); vo.setStatus(item.getStatus()); vo.setCreated(item.getCreated()); vo.setModified(item.getModified()); @@ -111,6 +115,10 @@ public class GatewayServerController extends BaseController { GatewayServerDetailedVO vo = new GatewayServerDetailedVO(); GatewayServer gatewayServer = gatewayServerService.selectGatewayServerById(id); List interfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(Collections.singleton(id)); + List linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceInfoList.stream().map(GatewayInterfaceInfo::getId).collect(Collectors.toSet())); + Map> groupByInterfaceLinkMap = linkList.stream().collect(Collectors.groupingBy(GatewayInterfaceLinkStrategy::getInterfaceId)); + List strategyList = gatewayStrategyService.selectGatewayStrategyByIds(linkList.stream().map(GatewayInterfaceLinkStrategy::getStrategyId).collect(Collectors.toSet())); + Map strategyMap = strategyList.stream().collect(Collectors.toMap(GatewayStrategy::getId, Function.identity())); if (GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel())){ GatewayRoute route = gatewayRouteService.selectGatewayRouteById(gatewayServer.getRouteId()); vo.setServerAddress(route.getRouteName()); @@ -119,6 +127,7 @@ public class GatewayServerController extends BaseController { } vo.setId(gatewayServer.getId()); vo.setServerName(gatewayServer.getServerName()); + vo.setRemark(gatewayServer.getRemark()); vo.setStatus(gatewayServer.getStatus()); vo.setCreated(gatewayServer.getCreated()); vo.setModified(gatewayServer.getModified()); @@ -136,9 +145,24 @@ public class GatewayServerController extends BaseController { interfaceInfoListVO.setVersion(info.getVersion()); interfaceInfoListVO.setStatus(info.getStatus()); interfaceInfoListVO.setDocument(info.getDocument()); + // 策略信息 + List currentLinks = groupByInterfaceLinkMap.getOrDefault(info.getId(), Collections.emptyList()); + List strategyListVOList = currentLinks.stream().map(link -> { + GatewayStrategy strategy = strategyMap.get(link.getStrategyId()); + GatewayStrategyListVO strategyListVO = new GatewayStrategyListVO(); + strategyListVO.setId(strategy.getId()); + strategyListVO.setStrategyName(strategy.getStrategyName()); + strategyListVO.setStrategyType(strategy.getStrategyType()); + strategyListVO.setDescription(strategy.getDescription()); + strategyListVO.setStatus(strategy.getStatus()); + return strategyListVO; + }) + .collect(Collectors.toList()); + interfaceInfoListVO.setStrategyList(strategyListVOList); return interfaceInfoListVO; }).collect(Collectors.toList()); vo.setInterfaceInfoList(interfaceInfoListVOList); + return success(vo); } @@ -182,4 +206,38 @@ public class GatewayServerController extends BaseController { public AjaxResult updateStatus(@Validated @RequestBody UpdateDataStatusDTO dto) { return toAjax(gatewayServerService.updateGatewayServerStatusByIds(dto.getIds(), GatewayDataStatus.getByCode(dto.getStatus()))); } + + /** + * 查询可绑定的策略列表(状态为启用的,不分页) + */ + @GetMapping("/bindableList") + public List bindableList(QueryGatewayServerDTO dto) + { + GatewayServer gatewayServer = new GatewayServer(); + gatewayServer.setStatus(GatewayDataStatus.ENABLE.getCode()); + gatewayServer.setServiceModel(dto.getServiceModel()); + gatewayServer.setAppCode(AppUtils.getAppCodeFromRequestHeader()); + Map params = new HashMap<>(8); + params.put("ids",dto.getIds()); + params.put("excludedIds",dto.getExcludedIds()); + gatewayServer.setParams(params); + List list = gatewayServerService.selectGatewayServerList(gatewayServer); + // 如果是路由模式的服务,查询对应的路由服务信息 + Collection routeIds = list.stream().filter(item -> GatewayServiceModel.ROUTE.getCode().equals(item.getServiceModel())) + .map(GatewayServer::getRouteId) + .collect(Collectors.toSet()); + List routeList = gatewayRouteService.selectGatewayRouteByIds(routeIds); + Map routeIdAndNameMap = routeList.stream().collect(Collectors.toMap(GatewayRoute::getId, GatewayRoute::getRouteName)); + return list.stream().map(item -> { + GatewayServerListVO vo = new GatewayServerListVO(); + vo.setId(item.getId()); + vo.setServerName(item.getServerName()); + vo.setServerAddress(GatewayServiceModel.ROUTE.getCode().equals(item.getServiceModel()) ? + routeIdAndNameMap.get(item.getRouteId()) : item.getServerAddress()); + vo.setStatus(item.getStatus()); + vo.setServiceModel(item.getServiceModel()); + vo.setRouteId(item.getRouteId()); + return vo; + }).collect(Collectors.toList()); + } } diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java index 681c70a..7ed3d32 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java @@ -8,6 +8,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import java.util.List; @@ -47,6 +48,7 @@ public class InsertGatewayInterfaceInfoDTO { @ApiModelProperty("接口文档(文档地址)") private String document; + @NotNull(message = "网关服务不能为空") @ApiModelProperty("网关服务id") private Long serverId; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/QueryGatewayServerDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/QueryGatewayServerDTO.java new file mode 100644 index 0000000..af52d1f --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/QueryGatewayServerDTO.java @@ -0,0 +1,32 @@ + +package com.sf.service.gateway.domain.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.List; + +/** + * 功能描述: + * 查询网关服务 + * @author a_kun + * @date 2024/4/23 10:38 + */ +@ApiModel("查询网关服务DTO") +@Data +public class QueryGatewayServerDTO { + + @ApiModelProperty("ids") + private List ids; + + @ApiModelProperty("状态(0停用 1启用)") + private String status; + + @ApiModelProperty("需要排除的id") + private List excludedIds; + + @ApiModelProperty("服务模式(NORMAL=普通模式,ROUTE=路由模式)") + private String serviceModel; + +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java index 62f8a8e..2011004 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java @@ -50,6 +50,7 @@ public class UpdateGatewayInterfaceInfoDTO { @ApiModelProperty("接口文档(文档地址)") private String document; + @NotNull(message = "网关服务不能为空") @ApiModelProperty("网关服务id") private Long serverId; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerDetailedVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerDetailedVO.java index 8bac930..54f92cb 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerDetailedVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerDetailedVO.java @@ -27,6 +27,9 @@ public class GatewayServerDetailedVO { @ApiModelProperty("服务地址,路由模式为路由名称") private String serverAddress; + @ApiModelProperty("备注") + private String remark; + @ApiModelProperty("服务状态") private String status; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerListVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerListVO.java index 7847216..1254623 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerListVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayServerListVO.java @@ -29,6 +29,9 @@ public class GatewayServerListVO { @ApiModelProperty("服务地址,路由模式为路由名称") private String serverAddress; + @ApiModelProperty("备注") + private String remark; + @ApiModelProperty("服务状态") private String status; diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/EncryptionAlgorithm.java b/sf-service/src/main/java/com/sf/service/gateway/enums/EncryptionAlgorithm.java index 64c0db4..0dc8717 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/enums/EncryptionAlgorithm.java +++ b/sf-service/src/main/java/com/sf/service/gateway/enums/EncryptionAlgorithm.java @@ -1,5 +1,7 @@ package com.sf.service.gateway.enums; +import org.springframework.util.StringUtils; + /** * 支持的加密算法 * 目前支持 ECC、RSA 和国密(SM2) @@ -33,9 +35,11 @@ public enum EncryptionAlgorithm } public static EncryptionAlgorithm getByCode(String code){ - for (EncryptionAlgorithm value : EncryptionAlgorithm.values()) { - if (value.code.equals(code)){ - return value; + if (StringUtils.hasText(code)){ + for (EncryptionAlgorithm value : EncryptionAlgorithm.values()) { + if (value.code.equals(code)){ + return value; + } } } return null; diff --git a/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceLinkStrategyMapper.java b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceLinkStrategyMapper.java index a481772..acaeb12 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceLinkStrategyMapper.java +++ b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceLinkStrategyMapper.java @@ -4,6 +4,7 @@ import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; import org.apache.ibatis.annotations.Param; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -29,6 +30,8 @@ public interface GatewayInterfaceLinkStrategyMapper { void deleteByInterfaceIds(@Param("interfaceIds") Collection interfaceIds); List selectGatewayInterfaceLinkStrategyByInterfaceIds(@Param("interfaceIds") Collection interfaceIds); + + List selectGatewayInterfaceLinkStrategyByStrategyIds(@Param("strategyIds") Collection strategyIds); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java index 3af1156..5562486 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java @@ -1,13 +1,17 @@ package com.sf.service.gateway.service.impl; +import apijson.JSON; import com.sf.common.utils.DateUtils; import com.sf.common.utils.SecurityUtils; import com.sf.service.gateway.domain.GatewayConfig; +import com.sf.service.gateway.enums.EncryptionAlgorithm; +import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.mapper.GatewayConfigMapper; import com.sf.service.gateway.service.IGatewayConfigService; import com.sf.service.index.util.AppUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import java.util.Date; @@ -34,6 +38,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { @Override public void updateGatewayConfig(GatewayConfig gatewayConfig) { + checkInsertOrUpdateGatewayConfig(gatewayConfig); String appCode = AppUtils.getAppCodeFromRequestHeader(); String username = SecurityUtils.getUsername(); Date nowDate = DateUtils.getNowDate(); @@ -52,4 +57,37 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { gatewayConfigMapper.updateByAppCodeSelective(gatewayConfig); } } + + private void checkInsertOrUpdateGatewayConfig(GatewayConfig gatewayConfig) { + if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getDataEncryptionStatus())){ + EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithm.getByCode(gatewayConfig.getEncryptionAlgorithm()); + Assert.notNull(encryptionAlgorithm,"不支持的加密算法"); + Assert.hasText(gatewayConfig.getPrivateKey(),"密钥内容不能为空"); + if (EncryptionAlgorithm.RSA.equals(encryptionAlgorithm)){ + Assert.hasText(gatewayConfig.getPublicKey(),"公钥内容不能为空"); + } + } + if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getApiCurrentLimitingStatus())){ + Assert.notNull(gatewayConfig.getApiCurrentLimitingThreshold(),"限流值不能为空"); + Assert.isTrue(gatewayConfig.getApiCurrentLimitingThreshold() >= 0,"限流值>=0"); + // 页面没有,这里给个默认值 + if (gatewayConfig.getApiCurrentLimitingTimeWindow() == null) { + gatewayConfig.setApiCurrentLimitingTimeWindow(1); + }else { + Assert.isTrue(gatewayConfig.getApiCurrentLimitingTimeWindow() >= 0,"限流窗口值>=0"); + } + Assert.isTrue(JSON.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息"); + } + if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getAppCurrentLimitingStatus())){ + Assert.notNull(gatewayConfig.getAppCurrentLimitingThreshold(),"限流值不能为空"); + Assert.isTrue(gatewayConfig.getAppCurrentLimitingThreshold() >= 0,"限流值>=0"); + // 页面没有,这里给个默认值 + if (gatewayConfig.getAppCurrentLimitingTimeWindow() == null) { + gatewayConfig.setAppCurrentLimitingTimeWindow(1); + }else { + Assert.isTrue(gatewayConfig.getAppCurrentLimitingTimeWindow() >= 0,"限流窗口值>=0"); + } + Assert.isTrue(JSON.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息"); + } + } } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java index f9d2541..0f0123d 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java @@ -11,6 +11,7 @@ import com.sf.service.gateway.domain.GatewayStrategy; import com.sf.service.gateway.domain.dto.InsertGatewayInterfaceInfoDTO; import com.sf.service.gateway.domain.dto.UpdateGatewayInterfaceInfoDTO; import com.sf.service.gateway.enums.GatewayDataStatus; +import com.sf.service.gateway.enums.GatewayStrategyType; import com.sf.service.gateway.mapper.GatewayInterfaceInfoMapper; import com.sf.service.gateway.mapper.GatewayInterfaceLinkStrategyMapper; import com.sf.service.gateway.service.IGatewayInterfaceInfoService; @@ -221,7 +222,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer List strategyList = gatewayStrategyService.selectGatewayStrategyByIds(strategyIds); Map> groupByStrategyTypeMap = strategyList.stream().collect(Collectors.groupingBy(GatewayStrategy::getStrategyType)); groupByStrategyTypeMap.forEach((type,strategies) ->{ - Assert.isTrue(strategies.size() == 1,type + "类型只能选择一个!"); + Assert.isTrue(strategies.size() == 1, Objects.requireNonNull(GatewayStrategyType.getByCode(type)).getInfo() + "类型只能选择一个!"); }); } 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 98ab095..2dc2329 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 @@ -123,12 +123,15 @@ public class GatewayRouteServiceImpl implements IGatewayRouteService default: break; } - - } if (GatewayRouteType.WEIGHT_ROUTE.equals(routeType)){ Assert.isTrue(sumWeight == 100,"权重总值为100!"); } + if (GatewayDataStatus.ENABLE.getCode().equals(dto.getRouteStatusActiveMonitoring())){ + Long routeActiveMonitoringTimeout = dto.getRouteActiveMonitoringTimeout(); + Assert.notNull(routeActiveMonitoringTimeout,"路由探活超时时间不能为空"); + Assert.isTrue(0 <= routeActiveMonitoringTimeout && routeActiveMonitoringTimeout <= 30000,"路由探活超时时间在0-30000之间!"); + } } /** diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java index 1a1fd9f..6267c63 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java @@ -4,10 +4,16 @@ import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; + +import apijson.JSON; +import cn.hutool.core.collection.CollUtil; import com.sf.common.utils.DateUtils; +import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; +import com.sf.service.gateway.domain.GatewayServer; import com.sf.service.gateway.domain.GatewayStrategy; import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.enums.GatewayStrategyType; +import com.sf.service.gateway.mapper.GatewayInterfaceLinkStrategyMapper; import com.sf.service.gateway.mapper.GatewayStrategyMapper; import com.sf.service.gateway.service.IGatewayStrategyService; import lombok.extern.slf4j.Slf4j; @@ -31,6 +37,9 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService @Autowired private GatewayStrategyMapper gatewayStrategyMapper; + @Autowired + private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper; + /** * 查询策略管理 * @@ -118,15 +127,15 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService Integer currentLimitingThreshold = gatewayStrategy.getCurrentLimitingThreshold(); Integer currentLimitingTimeWindow = gatewayStrategy.getCurrentLimitingTimeWindow(); String currentLimitingResponse = gatewayStrategy.getCurrentLimitingResponse(); - if (currentLimitingThreshold == null){ - gatewayStrategy.setCurrentLimitingThreshold(1); - } - if (currentLimitingTimeWindow == null){ + Assert.notNull(currentLimitingThreshold,"限流值不能为空"); + Assert.isTrue(currentLimitingThreshold >= 0,"限流值>=0"); + // 页面没有,这里给个默认值 + if (currentLimitingTimeWindow == null) { gatewayStrategy.setCurrentLimitingTimeWindow(1); + }else { + Assert.isTrue(currentLimitingTimeWindow >= 0,"限流窗口值>=0"); } - if (!StringUtils.hasText(currentLimitingResponse)){ - gatewayStrategy.setCurrentLimitingResponse("{}"); - } + Assert.isTrue(JSON.isJSONObject(currentLimitingResponse),"请输入正确的JSON格式限流响应信息"); } /** @@ -139,18 +148,13 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService Integer circuitBreakerTimeWindow = gatewayStrategy.getCircuitBreakerTimeWindow(); Integer circuitBreakerRecoveryInterval = gatewayStrategy.getCircuitBreakerRecoveryInterval(); String circuitBreakerResponse = gatewayStrategy.getCircuitBreakerResponse(); - if (circuitBreakerThreshold == null){ - gatewayStrategy.setCircuitBreakerThreshold(1); - } - if (circuitBreakerTimeWindow == null){ - gatewayStrategy.setCircuitBreakerTimeWindow(1); - } - if (circuitBreakerRecoveryInterval == null){ - gatewayStrategy.setCircuitBreakerRecoveryInterval(1); - } - if (!StringUtils.hasText(circuitBreakerResponse)){ - gatewayStrategy.setCircuitBreakerResponse("{}"); - } + Assert.notNull(circuitBreakerThreshold,"接口调用失败阈值不能为空"); + Assert.isTrue(circuitBreakerThreshold >= 0,"限流值>=0"); + Assert.notNull(circuitBreakerTimeWindow,"调用失败阈值对应的单位时间不能为空"); + Assert.isTrue(circuitBreakerTimeWindow >= 0,"调用失败阈值对应的单位时间>=0"); + Assert.notNull(circuitBreakerRecoveryInterval,"熔断后恢复的时间间隔不能为空"); + Assert.isTrue(circuitBreakerRecoveryInterval >= 0,"熔断后恢复的时间间隔>=0"); + Assert.isTrue(JSON.isJSONObject(circuitBreakerResponse),"请输入正确的JSON格式熔断响应信息"); } /** @@ -182,6 +186,7 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService if (ids == null || ids.length == 0) { return 0; } + checkDeleteConditions(CollUtil.newHashSet(ids)); return gatewayStrategyMapper.deleteGatewayStrategyByIds(ids); } @@ -197,9 +202,14 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService if (id == null) { return 0; } + checkDeleteConditions(Collections.singleton(id)); return gatewayStrategyMapper.deleteGatewayStrategyById(id); } + private void checkDeleteConditions(Collection ids) { + List linkList = gatewayInterfaceLinkStrategyMapper.selectGatewayInterfaceLinkStrategyByStrategyIds(ids); + Assert.isTrue(CollUtil.isEmpty(linkList),"策略已关联接口,删除失败");} + @Override public int updateGatewayStrategyStatusByIds(List ids, GatewayDataStatus status) { if (CollectionUtils.isEmpty(ids) || status == null){ diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml index bfa6c95..f019ff5 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml @@ -67,6 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" created, modified, uri_regular, + server_id, #{interfaceName}, @@ -82,6 +83,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{created}, #{modified}, #{uriRegular}, + #{serverId}, @@ -98,6 +100,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" modified = #{modified}, update_time = #{updateTime}, uri_regular = #{uriRegular}, + server_id = #{serverId}, where id = #{id} diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceLinkStrategyMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceLinkStrategyMapper.xml index adaa89a..57339da 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceLinkStrategyMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceLinkStrategyMapper.xml @@ -28,6 +28,15 @@ #{id} + delete from Gateway_interface_link_strategy @@ -60,7 +69,7 @@ - insert into Gateway_interface_link_strategy (interfaceId,strategyId) + insert into Gateway_interface_link_strategy (interface_id,strategy_id) values ( diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml index 0088e1e..6f1a12d 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml @@ -31,9 +31,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and server_name like concat('%', #{serverName}, '%') and server_address = #{serverAddress} and status = #{status} + and service_model = #{serviceModel} and created = #{created} and app_code = #{appCode} and create_time between #{params.beginCreateTime} and #{params.endCreateTime} + + and id in + + #{id} + + + + and id not in + + #{id} + +