diff --git a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayConfigController.java b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayConfigController.java index 3c006ee..4220338 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayConfigController.java +++ b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayConfigController.java @@ -71,7 +71,6 @@ public class GatewayConfigController extends BaseController public AjaxResult synchronization() { String appCode = AppUtils.getAppCodeFromRequestHeader(); - Assert.hasText(appCode,"应用信息错误"); gatewayConfigService.synchronization(appCode); return success(); } 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 e4f8d52..1851e05 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 @@ -98,57 +98,13 @@ public class GatewayServerController extends BaseController { @PreAuthorize("@ss.hasPermi('gateway:server:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { - 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()); - }else { - vo.setServerAddress(gatewayServer.getServerAddress()); - } - vo.setId(gatewayServer.getId()); - vo.setServerName(gatewayServer.getServerName()); - vo.setRemark(gatewayServer.getRemark()); - vo.setStatus(gatewayServer.getStatus()); - vo.setCreated(gatewayServer.getCreated()); - vo.setModified(gatewayServer.getModified()); - vo.setServiceModel(gatewayServer.getServiceModel()); - vo.setRouteId(gatewayServer.getRouteId()); - vo.setCreateTime(gatewayServer.getCreateTime()); - vo.setUpdateTime(gatewayServer.getUpdateTime()); - List interfaceInfoListVOList = interfaceInfoList.stream().map(info -> { - GatewayInterfaceInfoListVO interfaceInfoListVO = new GatewayInterfaceInfoListVO(); - interfaceInfoListVO.setId(info.getId()); - interfaceInfoListVO.setInterfaceName(info.getInterfaceName()); - interfaceInfoListVO.setInterfacePath(info.getInterfacePath()); - interfaceInfoListVO.setRequestMethod(info.getRequestMethod()); - interfaceInfoListVO.setDescription(info.getDescription()); - 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); - + GatewayRoute route = gatewayRouteService.selectGatewayRouteById(gatewayServer.getRouteId()); + GatewayServerDetailedVO vo = GatewayServerDetailedVO.convert(gatewayServer,route,interfaceInfoList,linkList,strategyList); return success(vo); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java index b2efc86..7875d7b 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java @@ -79,10 +79,15 @@ public class GatewayInterfaceInfo extends BaseEntity @ApiModelProperty("网关服务id") private Long serverId; + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + @ApiModelProperty("Mock状态(0=停用,1=启用)") private String mockStatus; @ApiModelProperty("Mock响应,json格式") private String mockResponse; + + } diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayServer.java b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayServer.java index 98d8945..d800024 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayServer.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayServer.java @@ -8,6 +8,8 @@ import com.sf.common.annotation.Excel; import com.sf.common.core.domain.BaseEntity; import org.hibernate.validator.constraints.URL; +import javax.validation.constraints.DecimalMax; +import javax.validation.constraints.DecimalMin; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; @@ -54,4 +56,12 @@ public class GatewayServer extends BaseEntity @ApiModelProperty("路由规则id") private Long routeId; + @ApiModelProperty(" 服务类型,SAC=SAC规范服务,OPEN=开放服务") + private String serviceType; + + @DecimalMin(value = "1",message = "超时时间为大于等于1的正整数") + @DecimalMax(value = "150000",message = "超时时间不能大于150000") + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + } 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 06c1377..f5f4e0f 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 @@ -9,9 +9,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.validator.constraints.Length; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Pattern; +import javax.validation.constraints.*; import java.util.List; @@ -62,6 +60,11 @@ public class InsertGatewayInterfaceInfoDTO { @ApiModelProperty("网关服务id") private Long serverId; + @DecimalMin(value = "1",message = "超时时间为大于等于1的正整数") + @DecimalMax(value = "150000",message = "超时时间不能大于150000") + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + @ApiModelProperty("策略ID集合") private List strategyIds; 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 9bc288c..711959f 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 @@ -8,9 +8,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.validator.constraints.Length; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Pattern; +import javax.validation.constraints.*; import java.util.List; @@ -65,6 +63,11 @@ public class UpdateGatewayInterfaceInfoDTO { @ApiModelProperty("网关服务id") private Long serverId; + @DecimalMin(value = "1",message = "超时时间为大于等于1的正整数") + @DecimalMax(value = "150000",message = "超时时间不能大于150000") + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + @ApiModelProperty("策略ID集合") private List strategyIds; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java index c292c7c..d3e5d3d 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java @@ -109,6 +109,9 @@ public class GatewayInterfaceInfoDetailedVO { @ApiModelProperty("网关服务名称") private String serverName; + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + @ApiModelProperty private List strategyList; @@ -137,6 +140,7 @@ public class GatewayInterfaceInfoDetailedVO { vo.setUpdateTime(gatewayInterfaceInfo.getUpdateTime()); vo.setServerId(gatewayInterfaceInfo.getServerId()); vo.setServerName(gatewayServer.getServerName()); + vo.setTimeout(gatewayInterfaceInfo.getTimeout()); vo.setMockStatus(gatewayInterfaceInfo.getMockStatus()); vo.setMockResponse(gatewayInterfaceInfo.getMockResponse()); // 策略信息 diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java index 2d31f9e..71d86b9 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java @@ -104,6 +104,9 @@ public class GatewayInterfaceInfoListVO { @ApiModelProperty("网关服务名称") private String serverName; + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + @ApiModelProperty private List strategyList; @@ -127,6 +130,7 @@ public class GatewayInterfaceInfoListVO { vo.setCreateTime(info.getCreateTime()); vo.setServerId(info.getServerId()); vo.setServerName(gatewayServer != null ? gatewayServer.getServerName() : EMPTY); + vo.setTimeout(info.getTimeout()); vo.setMockStatus(info.getMockStatus()); vo.setMockResponse(info.getMockResponse()); return vo; 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 54f92cb..75cc9e6 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 @@ -1,21 +1,29 @@ package com.sf.service.gateway.domain.vo; import com.fasterxml.jackson.annotation.JsonFormat; +import com.sf.service.gateway.domain.*; +import com.sf.service.gateway.enums.GatewayServiceModel; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.apache.commons.lang3.StringUtils.EMPTY; /** * 服务详细信息 - * + * * @author zoukun * @date 2024-04-22 */ @Data -@ApiModel( description = "服务详细信息展示对象") +@ApiModel(description = "服务详细信息展示对象") public class GatewayServerDetailedVO { private static final long serialVersionUID = 1L; @@ -42,17 +50,71 @@ public class GatewayServerDetailedVO { @ApiModelProperty("服务模式(NORMAL=普通模式,ROUTE=路由模式)") private String serviceModel; + @ApiModelProperty(" 服务类型,SAC=SAC规范服务,OPEN=开放服务") + private String serviceType; + @ApiModelProperty("路由规则id") private Long routeId; - /** 创建时间 */ + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + + /** + * 创建时间 + */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; - /** 更新时间 */ + /** + * 更新时间 + */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; @ApiModelProperty("包含的接口信息") private List interfaceInfoList; + + public static GatewayServerDetailedVO convert(GatewayServer gatewayServer + , GatewayRoute route + , List interfaceInfoList + , List linkList + , List strategyList + ) { + if (gatewayServer == null) { + return null; + } + Map> groupByInterfaceLinkMap = linkList.stream().collect(Collectors.groupingBy(GatewayInterfaceLinkStrategy::getInterfaceId)); + Map strategyMap = strategyList.stream().collect(Collectors.toMap(GatewayStrategy::getId, Function.identity())); + GatewayServerDetailedVO vo = new GatewayServerDetailedVO(); + if (GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel())) { + vo.setServerAddress(route != null ? route.getRouteName() : EMPTY); + } else { + vo.setServerAddress(gatewayServer.getServerAddress()); + } + vo.setId(gatewayServer.getId()); + vo.setServerName(gatewayServer.getServerName()); + vo.setRemark(gatewayServer.getRemark()); + vo.setStatus(gatewayServer.getStatus()); + vo.setCreated(gatewayServer.getCreated()); + vo.setModified(gatewayServer.getModified()); + vo.setServiceModel(gatewayServer.getServiceModel()); + vo.setServiceType(gatewayServer.getServiceType()); + vo.setRouteId(gatewayServer.getRouteId()); + vo.setTimeout(gatewayServer.getTimeout()); + vo.setCreateTime(gatewayServer.getCreateTime()); + vo.setUpdateTime(gatewayServer.getUpdateTime()); + List interfaceInfoListVOList = interfaceInfoList.stream().map(info -> { + GatewayInterfaceInfoListVO interfaceInfoListVO = GatewayInterfaceInfoListVO.convert(info,gatewayServer); + // 策略信息 + List currentLinks = groupByInterfaceLinkMap.getOrDefault(info.getId(), Collections.emptyList()); + List strategyListVOList = currentLinks.stream() + .map(link -> GatewayStrategyListVO.convert(strategyMap.get(link.getStrategyId()))) + .collect(Collectors.toList()); + interfaceInfoListVO.setStrategyList(strategyListVOList); + return interfaceInfoListVO; + }).collect(Collectors.toList()); + vo.setInterfaceInfoList(interfaceInfoListVOList); + return vo; + } + } 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 ea8959a..76b70f2 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 @@ -50,6 +50,12 @@ public class GatewayServerListVO { @ApiModelProperty("路由规则id") private Long routeId; + @ApiModelProperty(" 服务类型,SAC=SAC规范服务,OPEN=开放服务") + private String serviceType; + + @ApiModelProperty("超时时间,单位毫秒,1-150000之间") + private Integer timeout; + /** * 创建时间 */ @@ -79,7 +85,9 @@ public class GatewayServerListVO { vo.setCreated(gatewayServer.getCreated()); vo.setModified(gatewayServer.getModified()); vo.setServiceModel(gatewayServer.getServiceModel()); + vo.setServiceType(gatewayServer.getServiceType()); vo.setRouteId(gatewayServer.getRouteId()); + vo.setTimeout(gatewayServer.getTimeout()); vo.setCreateTime(gatewayServer.getCreateTime()); vo.setUpdateTime(gatewayServer.getUpdateTime()); return vo; diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/GatewayServiceType.java b/sf-service/src/main/java/com/sf/service/gateway/enums/GatewayServiceType.java new file mode 100644 index 0000000..0ec53a7 --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/enums/GatewayServiceType.java @@ -0,0 +1,45 @@ +package com.sf.service.gateway.enums; + +import org.springframework.util.StringUtils; + +/** + * 网关服务类型 + * + * @author zoukun + */ +public enum GatewayServiceType +{ + SAC("SAC", "SAC规范服务"), + OPEN("OPEN", "开放服务"), + ; + + private final String code; + private final String info; + + GatewayServiceType(String code, String info) + { + this.code = code; + this.info = info; + } + + public String getCode() + { + return code; + } + + public String getInfo() + { + return info; + } + + public static GatewayServiceType getByCode(String code){ + if (StringUtils.hasText(code)) { + for (GatewayServiceType value : GatewayServiceType.values()) { + if (value.code.equals(code)){ + return value; + } + } + } + return null; + } +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java b/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java index 8d52b3f..ba838e3 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java +++ b/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java @@ -38,29 +38,4 @@ public enum MatchType } return null; } - /* - 密钥生成方法 - 根据您的业务需求,查看生成密钥的方法。密钥包括 RSA 密钥、ECC 密钥、国密密钥。 - - 前置条件 - 您已通过 OpenSSL 官网 下载并安装 OpenSSL 工具(1.1.1 或以上版本)。 - - 生成 RSA 密钥 - 打开 OpenSSL 工具,使用以下命令行生成 RSA 私钥。您可以选择生成 1024 或 2048 位的私钥。 - openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048 - 根据 RSA 私钥生成 RSA 公钥。 - openssl rsa -pubout -in private_key.pem -out public_key.pem - - 生成 ECC 密钥 - 打开 OpenSSL 工具,使用以下命令行生成 ECC 的密钥对。您必须选择 secp256k1 椭圆曲线算法。 - openssl ecparam -name secp256k1 -genkey -noout -out secp256k1-key.pem - 根据 secp256k1-key.pem 密钥对生成 ECC 公钥。 - openssl ec -in secp256k1-key.pem -pubout -out ecpubkey.pem - - 生成国密密钥 - 打开 OpenSSL 工具,使用以下命令行生成国密 SM2 私钥 sm2-key.pem。 - openssl ecparam -name SM2 -genkey -noout -out sm2-key.pem - 根据 sm2-key.pem 密钥对生成国密 SM2 公钥 sm2pubkey.pem。 - openssl ec -in sm2-key.pem -pubout -out sm2pubkey.pem - */ } diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/RequestMethod.java b/sf-service/src/main/java/com/sf/service/gateway/enums/RequestMethod.java new file mode 100644 index 0000000..224516e --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/enums/RequestMethod.java @@ -0,0 +1,44 @@ +package com.sf.service.gateway.enums; + +/** + * 支持的请求方法 + * + * @author zoukun + */ +public enum RequestMethod +{ + GET("GET", "GET"), + POST("POST", "POST"), + PUT("PUT", "PUT"), + DELETE("DELETE", "DELETE"), + HEAD("HEAD", "HEAD"), + ; + + private final String code; + private final String info; + + RequestMethod(String code, String info) + { + this.code = code; + this.info = info; + } + + public String getCode() + { + return code; + } + + public String getInfo() + { + return info; + } + + public static RequestMethod getByCode(String code){ + for (RequestMethod value : RequestMethod.values()) { + if (value.code.equals(code)){ + return value; + } + } + return null; + } +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/RequestProtocol.java b/sf-service/src/main/java/com/sf/service/gateway/enums/RequestProtocol.java index 604e17a..10de24d 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/enums/RequestProtocol.java +++ b/sf-service/src/main/java/com/sf/service/gateway/enums/RequestProtocol.java @@ -38,29 +38,5 @@ public enum RequestProtocol } return null; } - /* - 密钥生成方法 - 根据您的业务需求,查看生成密钥的方法。密钥包括 RSA 密钥、ECC 密钥、国密密钥。 - 前置条件 - 您已通过 OpenSSL 官网 下载并安装 OpenSSL 工具(1.1.1 或以上版本)。 - - 生成 RSA 密钥 - 打开 OpenSSL 工具,使用以下命令行生成 RSA 私钥。您可以选择生成 1024 或 2048 位的私钥。 - openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048 - 根据 RSA 私钥生成 RSA 公钥。 - openssl rsa -pubout -in private_key.pem -out public_key.pem - - 生成 ECC 密钥 - 打开 OpenSSL 工具,使用以下命令行生成 ECC 的密钥对。您必须选择 secp256k1 椭圆曲线算法。 - openssl ecparam -name secp256k1 -genkey -noout -out secp256k1-key.pem - 根据 secp256k1-key.pem 密钥对生成 ECC 公钥。 - openssl ec -in secp256k1-key.pem -pubout -out ecpubkey.pem - - 生成国密密钥 - 打开 OpenSSL 工具,使用以下命令行生成国密 SM2 私钥 sm2-key.pem。 - openssl ecparam -name SM2 -genkey -noout -out sm2-key.pem - 根据 sm2-key.pem 密钥对生成国密 SM2 公钥 sm2pubkey.pem。 - openssl ec -in sm2-key.pem -pubout -out sm2pubkey.pem - */ } 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 25f0008..9bf9084 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 @@ -48,6 +48,8 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { private static final String SYNC_CONFIG_PATH = "/vertx/app/config"; + private static final Long DEFAULT_API_TIMEOUT = 3000L; + @Resource private GatewayConfigMapper gatewayConfigMapper; @@ -138,7 +140,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { List interfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(serverList.stream().map(GatewayServer::getId).collect(Collectors.toSet())); // 启用的 interfaceInfoList = interfaceInfoList.stream().filter(api -> GatewayDataStatus.ENABLE.getCode().equals(api.getStatus())).collect(Collectors.toList()); - if (CollUtil.isEmpty(interfaceInfoList)){ + if (CollUtil.isEmpty(interfaceInfoList)) { // 没有配置接口,直接结束 return service; } @@ -159,46 +161,49 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { return serverList.stream() .filter(server -> CollUtil.isNotEmpty(groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()))) .map(server -> { - SacService sacService = new SacService(); - sacService.setServiceName(server.getServerName()); - sacService.setServiceModel(server.getServiceModel()); - sacService.setServerAddress(convertToServerAddress(server.getServerAddress())); - List currentServerApiList = groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()); - List apiConfigList = currentServerApiList.stream().map(api -> { - List currentApiLinkStrategy = groupByInterfaceLinkMap.getOrDefault(api.getId(), Collections.emptyList()); - List currentApiStrategyList = currentApiLinkStrategy.stream() - .map(link -> convertToStrategy(strategyMap.get(link.getStrategyId()))) - .collect(Collectors.toList()); - ApiConfig apiConfig = new ApiConfig(); - apiConfig.setApiCode(api.getApiCode()); - apiConfig.setUri(api.getInterfacePath()); - apiConfig.setMethod(api.getRequestMethod()); - apiConfig.setStrategy(currentApiStrategyList); - if (GatewayDataStatus.ENABLE.getCode().equals(api.getMockStatus())){ - apiConfig.setMockResponse(api.getMockResponse()); - } - return apiConfig; - }).collect(Collectors.toList()); - sacService.setApiConfig(apiConfigList); - // 设置路由 - if (GatewayServiceModel.ROUTE.getCode().equals(server.getServiceModel())) { - GatewayRoute route = routeMap.get(server.getRouteId()); - Router routeConfig = new Router(); - routeConfig.setRouteType(route.getRouteType()); - List routeContents = JSON.parseArray(route.getRouteContent(), GatewayRoute.RouteContent.class); - routeConfig.setRouteContent(routeContents.stream().map(content -> { - RouteContent routeContent = new RouteContent(); - routeContent.setServerAddress(convertToServerAddress(content.getServerAddress())); - routeContent.setWeight(content.getWeight()); - routeContent.setHeaderKey(content.getHeaderKey()); - routeContent.setHeaderValues(content.getHeaderValues()); - routeContent.setMatchType(content.getMatchType()); - return routeContent; - }).collect(Collectors.toList())); - sacService.setRouteConfig(routeConfig); - } - return sacService; - }).collect(Collectors.toList()); + long serviceTimeout = server.getTimeout() != null ? server.getTimeout().longValue() : DEFAULT_API_TIMEOUT; + SacService sacService = new SacService(); + sacService.setServiceName(server.getServerName()); + sacService.setServiceModel(server.getServiceModel()); + sacService.setServiceType(server.getServiceType()); + sacService.setServerAddress(convertToServerAddress(server.getServerAddress())); + List currentServerApiList = groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()); + List apiConfigList = currentServerApiList.stream().map(api -> { + List currentApiLinkStrategy = groupByInterfaceLinkMap.getOrDefault(api.getId(), Collections.emptyList()); + List currentApiStrategyList = currentApiLinkStrategy.stream() + .map(link -> convertToStrategy(strategyMap.get(link.getStrategyId()))) + .collect(Collectors.toList()); + ApiConfig apiConfig = new ApiConfig(); + apiConfig.setApiCode(api.getApiCode()); + apiConfig.setUri(api.getInterfacePath()); + apiConfig.setMethod(api.getRequestMethod()); + apiConfig.setTimeout(api.getTimeout() != null ? api.getTimeout().longValue() : serviceTimeout); + apiConfig.setStrategy(currentApiStrategyList); + if (GatewayDataStatus.ENABLE.getCode().equals(api.getMockStatus())) { + apiConfig.setMockResponse(api.getMockResponse()); + } + return apiConfig; + }).collect(Collectors.toList()); + sacService.setApiConfig(apiConfigList); + // 设置路由 + if (GatewayServiceModel.ROUTE.getCode().equals(server.getServiceModel())) { + GatewayRoute route = routeMap.get(server.getRouteId()); + Router routeConfig = new Router(); + routeConfig.setRouteType(route.getRouteType()); + List routeContents = JSON.parseArray(route.getRouteContent(), GatewayRoute.RouteContent.class); + routeConfig.setRouteContent(routeContents.stream().map(content -> { + RouteContent routeContent = new RouteContent(); + routeContent.setServerAddress(convertToServerAddress(content.getServerAddress())); + routeContent.setWeight(content.getWeight()); + routeContent.setHeaderKey(content.getHeaderKey()); + routeContent.setHeaderValues(content.getHeaderValues()); + routeContent.setMatchType(content.getMatchType()); + return routeContent; + }).collect(Collectors.toList())); + sacService.setRouteConfig(routeConfig); + } + return sacService; + }).collect(Collectors.toList()); } return service; } 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 f7856af..8bf78ae 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 @@ -192,6 +192,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); gatewayInterfaceInfo.setDocument(document); gatewayInterfaceInfo.setServerId(dto.getServerId()); + gatewayInterfaceInfo.setTimeout(dto.getTimeout()); gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo); @@ -219,6 +220,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); gatewayInterfaceInfo.setDocument(document); gatewayInterfaceInfo.setServerId(dto.getServerId()); + gatewayInterfaceInfo.setTimeout(dto.getTimeout()); gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); updateGatewayInterfaceInfo(gatewayInterfaceInfo); diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayServerServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayServerServiceImpl.java index cb6a145..8e76523 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayServerServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayServerServiceImpl.java @@ -4,12 +4,15 @@ import java.util.*; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; +import cn.hutool.core.lang.UUID; import com.sf.common.utils.DateUtils; import com.sf.common.utils.URLUtils; import com.sf.service.gateway.domain.GatewayInterfaceInfo; import com.sf.service.gateway.domain.GatewayServer; import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.enums.GatewayServiceModel; +import com.sf.service.gateway.enums.GatewayServiceType; +import com.sf.service.gateway.enums.RequestMethod; import com.sf.service.gateway.mapper.GatewayServerMapper; import com.sf.service.gateway.service.IGatewayInterfaceInfoService; import com.sf.service.gateway.service.IGatewayServerService; @@ -17,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; /** @@ -69,21 +73,38 @@ public class GatewayServerServiceImpl implements IGatewayServerService * @return 结果 */ @Override + @Transactional(rollbackFor = Exception.class) public int insertGatewayServer(GatewayServer gatewayServer) { - checkServerModelConfig(gatewayServer); + checkInsertOrUpdateServerModelConfig(gatewayServer); Date nowDate = DateUtils.getNowDate(); gatewayServer.setCreateTime(nowDate); gatewayServer.setUpdateTime(nowDate); try { - return gatewayServerMapper.insertGatewayServer(gatewayServer); + int i = gatewayServerMapper.insertGatewayServer(gatewayServer); + if (GatewayServiceType.OPEN.getCode().equals(gatewayServer.getServiceType())){ + // 类型为开放服务的,默认生成一条接口配置 + GatewayInterfaceInfo interfaceInfo = new GatewayInterfaceInfo(); + interfaceInfo.setApiCode("sac_" + UUID.fastUUID().toString(true)); + interfaceInfo.setInterfaceName("[" + gatewayServer.getServerName() + "]开放服务默认接口配置"); + interfaceInfo.setInterfacePath("*"); + interfaceInfo.setRequestMethod(RequestMethod.POST.getCode()); + interfaceInfo.setDescription("[" + gatewayServer.getServerName() + "]开放服务默认接口配置"); + interfaceInfo.setVersion("v1"); + interfaceInfo.setStatus(GatewayDataStatus.ENABLE.getCode()); + interfaceInfo.setServerId(gatewayServer.getId()); + gatewayInterfaceInfoService.insertGatewayInterfaceInfo(interfaceInfo); + } + return i; }catch (DuplicateKeyException e){ throw new IllegalArgumentException("服务名称不可重复!"); } } - private void checkServerModelConfig(GatewayServer gatewayServer) { + private void checkInsertOrUpdateServerModelConfig(GatewayServer gatewayServer) { GatewayServiceModel serviceModel = GatewayServiceModel.getByCode(gatewayServer.getServiceModel()); + GatewayServiceType serviceType = GatewayServiceType.getByCode(gatewayServer.getServiceType()); + Assert.notNull(serviceType,"不支持的服务类型!"); Assert.notNull(serviceModel,"不支持的服务模式!"); switch (serviceModel){ case NORMAL: @@ -107,7 +128,7 @@ public class GatewayServerServiceImpl implements IGatewayServerService @Override public int updateGatewayServer(GatewayServer gatewayServer) { - checkServerModelConfig(gatewayServer); + checkInsertOrUpdateServerModelConfig(gatewayServer); gatewayServer.setUpdateTime(DateUtils.getNowDate()); try { return gatewayServerMapper.updateGatewayServer(gatewayServer); diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml index c83330d..957d85d 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml @@ -21,13 +21,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + select id, interface_name,api_code, interface_path, request_method, description, version, status, document, created, create_time, modified, update_time , - server_id,mock_status,mock_response + server_id,mock_status,mock_response,timeout from Gateway_interface_info @@ -81,6 +82,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" server_id, mock_status, mock_response, + timeout, #{interfaceName}, @@ -99,6 +101,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{serverId}, #{mockStatus}, #{mockResponse}, + #{timeout}, @@ -118,6 +121,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" server_id = #{serverId}, mock_status = #{mockStatus}, mock_response = #{mockResponse}, + timeout = #{timeout}, where id = #{id} diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml index 6f1a12d..b5a9a44 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayServerMapper.xml @@ -16,12 +16,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + select id, server_name, server_address, remark, status, created, create_time, modified, update_time , - service_model,route_id + service_model,route_id,service_type,timeout from Gateway_server @@ -32,6 +34,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and server_address = #{serverAddress} and status = #{status} and service_model = #{serviceModel} + and service_type = #{serviceType} and created = #{created} and app_code = #{appCode} and create_time between #{params.beginCreateTime} and #{params.endCreateTime} @@ -84,6 +87,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" app_code, service_model, route_id, + service_type, + timeout, #{serverName}, @@ -97,6 +102,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{appCode}, #{serviceModel}, #{routeId}, + #{serviceType}, + #{timeout}, @@ -111,6 +118,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update_time = #{updateTime}, service_model = #{serviceModel}, route_id = #{routeId}, + service_type = #{serviceType}, + timeout = #{timeout}, where id = #{id}