增加服务类型及超时时间配置功能

This commit is contained in:
akun 2024-05-15 17:39:03 +08:00
parent a9decb5582
commit 87eec45a52
19 changed files with 289 additions and 154 deletions

View File

@ -71,7 +71,6 @@ public class GatewayConfigController extends BaseController
public AjaxResult synchronization() public AjaxResult synchronization()
{ {
String appCode = AppUtils.getAppCodeFromRequestHeader(); String appCode = AppUtils.getAppCodeFromRequestHeader();
Assert.hasText(appCode,"应用信息错误");
gatewayConfigService.synchronization(appCode); gatewayConfigService.synchronization(appCode);
return success(); return success();
} }

View File

@ -98,57 +98,13 @@ public class GatewayServerController extends BaseController {
@PreAuthorize("@ss.hasPermi('gateway:server:query')") @PreAuthorize("@ss.hasPermi('gateway:server:query')")
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id) { public AjaxResult getInfo(@PathVariable("id") Long id) {
GatewayServerDetailedVO vo = new GatewayServerDetailedVO();
GatewayServer gatewayServer = gatewayServerService.selectGatewayServerById(id); GatewayServer gatewayServer = gatewayServerService.selectGatewayServerById(id);
List<GatewayInterfaceInfo> interfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(Collections.singleton(id)); List<GatewayInterfaceInfo> interfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(Collections.singleton(id));
List<GatewayInterfaceLinkStrategy> linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceInfoList.stream().map(GatewayInterfaceInfo::getId).collect(Collectors.toSet())); List<GatewayInterfaceLinkStrategy> linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceInfoList.stream().map(GatewayInterfaceInfo::getId).collect(Collectors.toSet()));
Map<Long, List<GatewayInterfaceLinkStrategy>> groupByInterfaceLinkMap = linkList.stream().collect(Collectors.groupingBy(GatewayInterfaceLinkStrategy::getInterfaceId));
List<GatewayStrategy> strategyList = gatewayStrategyService.selectGatewayStrategyByIds(linkList.stream().map(GatewayInterfaceLinkStrategy::getStrategyId).collect(Collectors.toSet())); List<GatewayStrategy> strategyList = gatewayStrategyService.selectGatewayStrategyByIds(linkList.stream().map(GatewayInterfaceLinkStrategy::getStrategyId).collect(Collectors.toSet()));
Map<Long, GatewayStrategy> strategyMap = strategyList.stream().collect(Collectors.toMap(GatewayStrategy::getId, Function.identity())); GatewayRoute route = gatewayRouteService.selectGatewayRouteById(gatewayServer.getRouteId());
if (GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel())){ GatewayServerDetailedVO vo = GatewayServerDetailedVO.convert(gatewayServer,route,interfaceInfoList,linkList,strategyList);
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<GatewayInterfaceInfoListVO> 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<GatewayInterfaceLinkStrategy> currentLinks = groupByInterfaceLinkMap.getOrDefault(info.getId(), Collections.emptyList());
List<GatewayStrategyListVO> 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); return success(vo);
} }

View File

@ -79,10 +79,15 @@ public class GatewayInterfaceInfo extends BaseEntity
@ApiModelProperty("网关服务id") @ApiModelProperty("网关服务id")
private Long serverId; private Long serverId;
@ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
@ApiModelProperty("Mock状态(0=停用,1=启用)") @ApiModelProperty("Mock状态(0=停用,1=启用)")
private String mockStatus; private String mockStatus;
@ApiModelProperty("Mock响应json格式") @ApiModelProperty("Mock响应json格式")
private String mockResponse; private String mockResponse;
} }

View File

@ -8,6 +8,8 @@ import com.sf.common.annotation.Excel;
import com.sf.common.core.domain.BaseEntity; import com.sf.common.core.domain.BaseEntity;
import org.hibernate.validator.constraints.URL; 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.NotBlank;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
@ -54,4 +56,12 @@ public class GatewayServer extends BaseEntity
@ApiModelProperty("路由规则id") @ApiModelProperty("路由规则id")
private Long routeId; 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;
} }

View File

@ -9,9 +9,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List; import java.util.List;
@ -62,6 +60,11 @@ public class InsertGatewayInterfaceInfoDTO {
@ApiModelProperty("网关服务id") @ApiModelProperty("网关服务id")
private Long serverId; private Long serverId;
@DecimalMin(value = "1",message = "超时时间为大于等于1的正整数")
@DecimalMax(value = "150000",message = "超时时间不能大于150000")
@ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
@ApiModelProperty("策略ID集合") @ApiModelProperty("策略ID集合")
private List<Long> strategyIds; private List<Long> strategyIds;

View File

@ -8,9 +8,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List; import java.util.List;
@ -65,6 +63,11 @@ public class UpdateGatewayInterfaceInfoDTO {
@ApiModelProperty("网关服务id") @ApiModelProperty("网关服务id")
private Long serverId; private Long serverId;
@DecimalMin(value = "1",message = "超时时间为大于等于1的正整数")
@DecimalMax(value = "150000",message = "超时时间不能大于150000")
@ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
@ApiModelProperty("策略ID集合") @ApiModelProperty("策略ID集合")
private List<Long> strategyIds; private List<Long> strategyIds;

View File

@ -109,6 +109,9 @@ public class GatewayInterfaceInfoDetailedVO {
@ApiModelProperty("网关服务名称") @ApiModelProperty("网关服务名称")
private String serverName; private String serverName;
@ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
@ApiModelProperty @ApiModelProperty
private List<GatewayStrategyListVO> strategyList; private List<GatewayStrategyListVO> strategyList;
@ -137,6 +140,7 @@ public class GatewayInterfaceInfoDetailedVO {
vo.setUpdateTime(gatewayInterfaceInfo.getUpdateTime()); vo.setUpdateTime(gatewayInterfaceInfo.getUpdateTime());
vo.setServerId(gatewayInterfaceInfo.getServerId()); vo.setServerId(gatewayInterfaceInfo.getServerId());
vo.setServerName(gatewayServer.getServerName()); vo.setServerName(gatewayServer.getServerName());
vo.setTimeout(gatewayInterfaceInfo.getTimeout());
vo.setMockStatus(gatewayInterfaceInfo.getMockStatus()); vo.setMockStatus(gatewayInterfaceInfo.getMockStatus());
vo.setMockResponse(gatewayInterfaceInfo.getMockResponse()); vo.setMockResponse(gatewayInterfaceInfo.getMockResponse());
// 策略信息 // 策略信息

View File

@ -104,6 +104,9 @@ public class GatewayInterfaceInfoListVO {
@ApiModelProperty("网关服务名称") @ApiModelProperty("网关服务名称")
private String serverName; private String serverName;
@ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
@ApiModelProperty @ApiModelProperty
private List<GatewayStrategyListVO> strategyList; private List<GatewayStrategyListVO> strategyList;
@ -127,6 +130,7 @@ public class GatewayInterfaceInfoListVO {
vo.setCreateTime(info.getCreateTime()); vo.setCreateTime(info.getCreateTime());
vo.setServerId(info.getServerId()); vo.setServerId(info.getServerId());
vo.setServerName(gatewayServer != null ? gatewayServer.getServerName() : EMPTY); vo.setServerName(gatewayServer != null ? gatewayServer.getServerName() : EMPTY);
vo.setTimeout(info.getTimeout());
vo.setMockStatus(info.getMockStatus()); vo.setMockStatus(info.getMockStatus());
vo.setMockResponse(info.getMockResponse()); vo.setMockResponse(info.getMockResponse());
return vo; return vo;

View File

@ -1,12 +1,20 @@
package com.sf.service.gateway.domain.vo; package com.sf.service.gateway.domain.vo;
import com.fasterxml.jackson.annotation.JsonFormat; 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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; 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;
/** /**
* 服务详细信息 * 服务详细信息
@ -15,7 +23,7 @@ import java.util.List;
* @date 2024-04-22 * @date 2024-04-22
*/ */
@Data @Data
@ApiModel( description = "服务详细信息展示对象") @ApiModel(description = "服务详细信息展示对象")
public class GatewayServerDetailedVO { public class GatewayServerDetailedVO {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -42,17 +50,71 @@ public class GatewayServerDetailedVO {
@ApiModelProperty("服务模式NORMAL=普通模式ROUTE=路由模式)") @ApiModelProperty("服务模式NORMAL=普通模式ROUTE=路由模式)")
private String serviceModel; private String serviceModel;
@ApiModelProperty(" 服务类型SAC=SAC规范服务OPEN=开放服务")
private String serviceType;
@ApiModelProperty("路由规则id") @ApiModelProperty("路由规则id")
private Long routeId; private Long routeId;
/** 创建时间 */ @ApiModelProperty("超时时间,单位毫秒,1-150000之间")
private Integer timeout;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; private Date createTime;
/** 更新时间 */ /**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime; private Date updateTime;
@ApiModelProperty("包含的接口信息") @ApiModelProperty("包含的接口信息")
private List<GatewayInterfaceInfoListVO> interfaceInfoList; private List<GatewayInterfaceInfoListVO> interfaceInfoList;
public static GatewayServerDetailedVO convert(GatewayServer gatewayServer
, GatewayRoute route
, List<GatewayInterfaceInfo> interfaceInfoList
, List<GatewayInterfaceLinkStrategy> linkList
, List<GatewayStrategy> strategyList
) {
if (gatewayServer == null) {
return null;
}
Map<Long, List<GatewayInterfaceLinkStrategy>> groupByInterfaceLinkMap = linkList.stream().collect(Collectors.groupingBy(GatewayInterfaceLinkStrategy::getInterfaceId));
Map<Long, GatewayStrategy> 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<GatewayInterfaceInfoListVO> interfaceInfoListVOList = interfaceInfoList.stream().map(info -> {
GatewayInterfaceInfoListVO interfaceInfoListVO = GatewayInterfaceInfoListVO.convert(info,gatewayServer);
// 策略信息
List<GatewayInterfaceLinkStrategy> currentLinks = groupByInterfaceLinkMap.getOrDefault(info.getId(), Collections.emptyList());
List<GatewayStrategyListVO> 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;
}
} }

View File

@ -50,6 +50,12 @@ public class GatewayServerListVO {
@ApiModelProperty("路由规则id") @ApiModelProperty("路由规则id")
private Long routeId; 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.setCreated(gatewayServer.getCreated());
vo.setModified(gatewayServer.getModified()); vo.setModified(gatewayServer.getModified());
vo.setServiceModel(gatewayServer.getServiceModel()); vo.setServiceModel(gatewayServer.getServiceModel());
vo.setServiceType(gatewayServer.getServiceType());
vo.setRouteId(gatewayServer.getRouteId()); vo.setRouteId(gatewayServer.getRouteId());
vo.setTimeout(gatewayServer.getTimeout());
vo.setCreateTime(gatewayServer.getCreateTime()); vo.setCreateTime(gatewayServer.getCreateTime());
vo.setUpdateTime(gatewayServer.getUpdateTime()); vo.setUpdateTime(gatewayServer.getUpdateTime());
return vo; return vo;

View File

@ -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;
}
}

View File

@ -38,29 +38,4 @@ public enum MatchType
} }
return null; 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
*/
} }

View File

@ -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;
}
}

View File

@ -38,29 +38,5 @@ public enum RequestProtocol
} }
return null; 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
*/
} }

View File

@ -48,6 +48,8 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService {
private static final String SYNC_CONFIG_PATH = "/vertx/app/config"; private static final String SYNC_CONFIG_PATH = "/vertx/app/config";
private static final Long DEFAULT_API_TIMEOUT = 3000L;
@Resource @Resource
private GatewayConfigMapper gatewayConfigMapper; private GatewayConfigMapper gatewayConfigMapper;
@ -138,7 +140,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService {
List<GatewayInterfaceInfo> interfaceInfoList = gatewayInterfaceInfoService.selectGatewayInterfaceInfoByServerIds(serverList.stream().map(GatewayServer::getId).collect(Collectors.toSet())); List<GatewayInterfaceInfo> 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()); interfaceInfoList = interfaceInfoList.stream().filter(api -> GatewayDataStatus.ENABLE.getCode().equals(api.getStatus())).collect(Collectors.toList());
if (CollUtil.isEmpty(interfaceInfoList)){ if (CollUtil.isEmpty(interfaceInfoList)) {
// 没有配置接口直接结束 // 没有配置接口直接结束
return service; return service;
} }
@ -159,46 +161,49 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService {
return serverList.stream() return serverList.stream()
.filter(server -> CollUtil.isNotEmpty(groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()))) .filter(server -> CollUtil.isNotEmpty(groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList())))
.map(server -> { .map(server -> {
SacService sacService = new SacService(); long serviceTimeout = server.getTimeout() != null ? server.getTimeout().longValue() : DEFAULT_API_TIMEOUT;
sacService.setServiceName(server.getServerName()); SacService sacService = new SacService();
sacService.setServiceModel(server.getServiceModel()); sacService.setServiceName(server.getServerName());
sacService.setServerAddress(convertToServerAddress(server.getServerAddress())); sacService.setServiceModel(server.getServiceModel());
List<GatewayInterfaceInfo> currentServerApiList = groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()); sacService.setServiceType(server.getServiceType());
List<ApiConfig> apiConfigList = currentServerApiList.stream().map(api -> { sacService.setServerAddress(convertToServerAddress(server.getServerAddress()));
List<GatewayInterfaceLinkStrategy> currentApiLinkStrategy = groupByInterfaceLinkMap.getOrDefault(api.getId(), Collections.emptyList()); List<GatewayInterfaceInfo> currentServerApiList = groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList());
List<Strategy> currentApiStrategyList = currentApiLinkStrategy.stream() List<ApiConfig> apiConfigList = currentServerApiList.stream().map(api -> {
.map(link -> convertToStrategy(strategyMap.get(link.getStrategyId()))) List<GatewayInterfaceLinkStrategy> currentApiLinkStrategy = groupByInterfaceLinkMap.getOrDefault(api.getId(), Collections.emptyList());
.collect(Collectors.toList()); List<Strategy> currentApiStrategyList = currentApiLinkStrategy.stream()
ApiConfig apiConfig = new ApiConfig(); .map(link -> convertToStrategy(strategyMap.get(link.getStrategyId())))
apiConfig.setApiCode(api.getApiCode()); .collect(Collectors.toList());
apiConfig.setUri(api.getInterfacePath()); ApiConfig apiConfig = new ApiConfig();
apiConfig.setMethod(api.getRequestMethod()); apiConfig.setApiCode(api.getApiCode());
apiConfig.setStrategy(currentApiStrategyList); apiConfig.setUri(api.getInterfacePath());
if (GatewayDataStatus.ENABLE.getCode().equals(api.getMockStatus())){ apiConfig.setMethod(api.getRequestMethod());
apiConfig.setMockResponse(api.getMockResponse()); apiConfig.setTimeout(api.getTimeout() != null ? api.getTimeout().longValue() : serviceTimeout);
} apiConfig.setStrategy(currentApiStrategyList);
return apiConfig; if (GatewayDataStatus.ENABLE.getCode().equals(api.getMockStatus())) {
}).collect(Collectors.toList()); apiConfig.setMockResponse(api.getMockResponse());
sacService.setApiConfig(apiConfigList); }
// 设置路由 return apiConfig;
if (GatewayServiceModel.ROUTE.getCode().equals(server.getServiceModel())) { }).collect(Collectors.toList());
GatewayRoute route = routeMap.get(server.getRouteId()); sacService.setApiConfig(apiConfigList);
Router routeConfig = new Router(); // 设置路由
routeConfig.setRouteType(route.getRouteType()); if (GatewayServiceModel.ROUTE.getCode().equals(server.getServiceModel())) {
List<GatewayRoute.RouteContent> routeContents = JSON.parseArray(route.getRouteContent(), GatewayRoute.RouteContent.class); GatewayRoute route = routeMap.get(server.getRouteId());
routeConfig.setRouteContent(routeContents.stream().map(content -> { Router routeConfig = new Router();
RouteContent routeContent = new RouteContent(); routeConfig.setRouteType(route.getRouteType());
routeContent.setServerAddress(convertToServerAddress(content.getServerAddress())); List<GatewayRoute.RouteContent> routeContents = JSON.parseArray(route.getRouteContent(), GatewayRoute.RouteContent.class);
routeContent.setWeight(content.getWeight()); routeConfig.setRouteContent(routeContents.stream().map(content -> {
routeContent.setHeaderKey(content.getHeaderKey()); RouteContent routeContent = new RouteContent();
routeContent.setHeaderValues(content.getHeaderValues()); routeContent.setServerAddress(convertToServerAddress(content.getServerAddress()));
routeContent.setMatchType(content.getMatchType()); routeContent.setWeight(content.getWeight());
return routeContent; routeContent.setHeaderKey(content.getHeaderKey());
}).collect(Collectors.toList())); routeContent.setHeaderValues(content.getHeaderValues());
sacService.setRouteConfig(routeConfig); routeContent.setMatchType(content.getMatchType());
} return routeContent;
return sacService; }).collect(Collectors.toList()));
}).collect(Collectors.toList()); sacService.setRouteConfig(routeConfig);
}
return sacService;
}).collect(Collectors.toList());
} }
return service; return service;
} }

View File

@ -192,6 +192,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId();
gatewayInterfaceInfo.setDocument(document); gatewayInterfaceInfo.setDocument(document);
gatewayInterfaceInfo.setServerId(dto.getServerId()); gatewayInterfaceInfo.setServerId(dto.getServerId());
gatewayInterfaceInfo.setTimeout(dto.getTimeout());
gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); gatewayInterfaceInfo.setMockStatus(dto.getMockStatus());
gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); gatewayInterfaceInfo.setMockResponse(dto.getMockResponse());
Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo); Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo);
@ -219,6 +220,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId();
gatewayInterfaceInfo.setDocument(document); gatewayInterfaceInfo.setDocument(document);
gatewayInterfaceInfo.setServerId(dto.getServerId()); gatewayInterfaceInfo.setServerId(dto.getServerId());
gatewayInterfaceInfo.setTimeout(dto.getTimeout());
gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); gatewayInterfaceInfo.setMockStatus(dto.getMockStatus());
gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); gatewayInterfaceInfo.setMockResponse(dto.getMockResponse());
updateGatewayInterfaceInfo(gatewayInterfaceInfo); updateGatewayInterfaceInfo(gatewayInterfaceInfo);

View File

@ -4,12 +4,15 @@ import java.util.*;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.sf.common.utils.DateUtils; import com.sf.common.utils.DateUtils;
import com.sf.common.utils.URLUtils; import com.sf.common.utils.URLUtils;
import com.sf.service.gateway.domain.GatewayInterfaceInfo; import com.sf.service.gateway.domain.GatewayInterfaceInfo;
import com.sf.service.gateway.domain.GatewayServer; import com.sf.service.gateway.domain.GatewayServer;
import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.enums.GatewayServiceModel; 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.mapper.GatewayServerMapper;
import com.sf.service.gateway.service.IGatewayInterfaceInfoService; import com.sf.service.gateway.service.IGatewayInterfaceInfoService;
import com.sf.service.gateway.service.IGatewayServerService; 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.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -69,21 +73,38 @@ public class GatewayServerServiceImpl implements IGatewayServerService
* @return 结果 * @return 结果
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public int insertGatewayServer(GatewayServer gatewayServer) public int insertGatewayServer(GatewayServer gatewayServer)
{ {
checkServerModelConfig(gatewayServer); checkInsertOrUpdateServerModelConfig(gatewayServer);
Date nowDate = DateUtils.getNowDate(); Date nowDate = DateUtils.getNowDate();
gatewayServer.setCreateTime(nowDate); gatewayServer.setCreateTime(nowDate);
gatewayServer.setUpdateTime(nowDate); gatewayServer.setUpdateTime(nowDate);
try { 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){ }catch (DuplicateKeyException e){
throw new IllegalArgumentException("服务名称不可重复!"); throw new IllegalArgumentException("服务名称不可重复!");
} }
} }
private void checkServerModelConfig(GatewayServer gatewayServer) { private void checkInsertOrUpdateServerModelConfig(GatewayServer gatewayServer) {
GatewayServiceModel serviceModel = GatewayServiceModel.getByCode(gatewayServer.getServiceModel()); GatewayServiceModel serviceModel = GatewayServiceModel.getByCode(gatewayServer.getServiceModel());
GatewayServiceType serviceType = GatewayServiceType.getByCode(gatewayServer.getServiceType());
Assert.notNull(serviceType,"不支持的服务类型!");
Assert.notNull(serviceModel,"不支持的服务模式!"); Assert.notNull(serviceModel,"不支持的服务模式!");
switch (serviceModel){ switch (serviceModel){
case NORMAL: case NORMAL:
@ -107,7 +128,7 @@ public class GatewayServerServiceImpl implements IGatewayServerService
@Override @Override
public int updateGatewayServer(GatewayServer gatewayServer) public int updateGatewayServer(GatewayServer gatewayServer)
{ {
checkServerModelConfig(gatewayServer); checkInsertOrUpdateServerModelConfig(gatewayServer);
gatewayServer.setUpdateTime(DateUtils.getNowDate()); gatewayServer.setUpdateTime(DateUtils.getNowDate());
try { try {
return gatewayServerMapper.updateGatewayServer(gatewayServer); return gatewayServerMapper.updateGatewayServer(gatewayServer);

View File

@ -21,13 +21,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="serverId" column="server_id" /> <result property="serverId" column="server_id" />
<result property="mockStatus" column="mock_status" /> <result property="mockStatus" column="mock_status" />
<result property="mockResponse" column="mock_response" /> <result property="mockResponse" column="mock_response" />
<result property="timeout" column="timeout" />
</resultMap> </resultMap>
<sql id="selectGatewayInterfaceInfoVo"> <sql id="selectGatewayInterfaceInfoVo">
select id, interface_name,api_code, interface_path, request_method, select id, interface_name,api_code, interface_path, request_method,
description, version, status, document, created, description, version, status, document, created,
create_time, modified, update_time , create_time, modified, update_time ,
server_id,mock_status,mock_response server_id,mock_status,mock_response,timeout
from Gateway_interface_info from Gateway_interface_info
</sql> </sql>
@ -81,6 +82,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="serverId != null">server_id,</if> <if test="serverId != null">server_id,</if>
<if test="mockStatus != null">mock_status,</if> <if test="mockStatus != null">mock_status,</if>
<if test="mockResponse != null">mock_response,</if> <if test="mockResponse != null">mock_response,</if>
<if test="timeout != null">timeout,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="interfaceName != null and interfaceName != ''">#{interfaceName},</if> <if test="interfaceName != null and interfaceName != ''">#{interfaceName},</if>
@ -99,6 +101,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="serverId != null">#{serverId},</if> <if test="serverId != null">#{serverId},</if>
<if test="mockStatus != null">#{mockStatus},</if> <if test="mockStatus != null">#{mockStatus},</if>
<if test="mockResponse != null">#{mockResponse},</if> <if test="mockResponse != null">#{mockResponse},</if>
<if test="timeout != null">#{timeout},</if>
</trim> </trim>
</insert> </insert>
@ -118,6 +121,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="serverId != null">server_id = #{serverId},</if> <if test="serverId != null">server_id = #{serverId},</if>
<if test="mockStatus != null">mock_status = #{mockStatus},</if> <if test="mockStatus != null">mock_status = #{mockStatus},</if>
<if test="mockResponse != null">mock_response = #{mockResponse},</if> <if test="mockResponse != null">mock_response = #{mockResponse},</if>
<if test="timeout != null">timeout = #{timeout},</if>
</trim> </trim>
where id = #{id} where id = #{id}
</update> </update>

View File

@ -16,12 +16,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="updateTime" column="update_time" /> <result property="updateTime" column="update_time" />
<result property="serviceModel" column="service_model" /> <result property="serviceModel" column="service_model" />
<result property="routeId" column="route_id" /> <result property="routeId" column="route_id" />
<result property="serviceType" column="service_type" />
<result property="timeout" column="timeout" />
</resultMap> </resultMap>
<sql id="selectGatewayServerVo"> <sql id="selectGatewayServerVo">
select id, server_name, server_address, remark, select id, server_name, server_address, remark,
status, created, create_time, modified, update_time , status, created, create_time, modified, update_time ,
service_model,route_id service_model,route_id,service_type,timeout
from Gateway_server from Gateway_server
</sql> </sql>
@ -32,6 +34,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="serverAddress != null and serverAddress != ''"> and server_address = #{serverAddress}</if> <if test="serverAddress != null and serverAddress != ''"> and server_address = #{serverAddress}</if>
<if test="status != null and status != ''"> and status = #{status}</if> <if test="status != null and status != ''"> and status = #{status}</if>
<if test="serviceModel != null"> and service_model = #{serviceModel}</if> <if test="serviceModel != null"> and service_model = #{serviceModel}</if>
<if test="serviceType != null"> and service_type = #{serviceType}</if>
<if test="created != null "> and created = #{created}</if> <if test="created != null "> and created = #{created}</if>
<if test="appCode != null "> and app_code = #{appCode}</if> <if test="appCode != null "> and app_code = #{appCode}</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>
@ -84,6 +87,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="appCode != null">app_code,</if> <if test="appCode != null">app_code,</if>
<if test="serviceModel != null">service_model,</if> <if test="serviceModel != null">service_model,</if>
<if test="routeId != null">route_id,</if> <if test="routeId != null">route_id,</if>
<if test="serviceType != null">service_type,</if>
<if test="timeout != null">timeout,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="serverName != null and serverName != ''">#{serverName},</if> <if test="serverName != null and serverName != ''">#{serverName},</if>
@ -97,6 +102,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="appCode != null">#{appCode},</if> <if test="appCode != null">#{appCode},</if>
<if test="serviceModel != null">#{serviceModel},</if> <if test="serviceModel != null">#{serviceModel},</if>
<if test="routeId != null">#{routeId},</if> <if test="routeId != null">#{routeId},</if>
<if test="serviceType != null">#{serviceType},</if>
<if test="timeout != null">#{timeout},</if>
</trim> </trim>
</insert> </insert>
@ -111,6 +118,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">update_time = #{updateTime},</if> <if test="updateTime != null">update_time = #{updateTime},</if>
<if test="serviceModel != null">service_model = #{serviceModel},</if> <if test="serviceModel != null">service_model = #{serviceModel},</if>
<if test="routeId != null">route_id = #{routeId},</if> <if test="routeId != null">route_id = #{routeId},</if>
<if test="serviceType != null">service_type = #{serviceType},</if>
<if test="timeout != null">timeout = #{timeout},</if>
</trim> </trim>
where id = #{id} where id = #{id}
</update> </update>