网关配置同步至网关
This commit is contained in:
parent
fd71ace2d9
commit
5548ee6c44
@ -144,3 +144,11 @@ file:
|
||||
bucket:
|
||||
#name: ruoyi
|
||||
name: sac
|
||||
|
||||
sac:
|
||||
gateway:
|
||||
# host: 192.168.1.68
|
||||
# port: 5566
|
||||
host: 192.168.1.23
|
||||
port: 1775
|
||||
|
||||
|
@ -42,6 +42,11 @@
|
||||
<version>1.6.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.smarterFramework</groupId>
|
||||
<artifactId>sf-vertx-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -19,6 +19,7 @@ import com.sf.service.index.util.AppUtils;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -75,4 +76,17 @@ public class GatewayConfigController extends BaseController
|
||||
return success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改网关配置
|
||||
*/
|
||||
@Log(title = "网关配置同步", businessType = BusinessType.UPDATE)
|
||||
@PostMapping("/sync")
|
||||
public AjaxResult synchronization()
|
||||
{
|
||||
String appCode = AppUtils.getAppCodeFromRequestHeader();
|
||||
Assert.hasText(appCode,"应用信息错误");
|
||||
gatewayConfigService.synchronization(appCode);
|
||||
return success();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,22 +57,7 @@ public class GatewayRouteController extends BaseController
|
||||
{
|
||||
startPage();
|
||||
List<GatewayRoute> list = gatewayRouteService.selectGatewayRouteList(gatewayRoute);
|
||||
List<GatewayRouteListVO> voList = list.stream().map(item ->{
|
||||
GatewayRouteListVO vo = new GatewayRouteListVO();
|
||||
vo.setId(item.getId());
|
||||
vo.setRouteName(item.getRouteName());
|
||||
vo.setRequestProtocol(item.getRequestProtocol());
|
||||
vo.setRouteType(item.getRouteType());
|
||||
vo.setRouteContent(JSON.parseArray(item.getRouteContent(),GatewayRoute.RouteContent.class));
|
||||
vo.setRouteStatusActiveMonitoring(item.getRouteStatusActiveMonitoring());
|
||||
vo.setRouteActiveMonitoringPath(item.getRouteActiveMonitoringPath());
|
||||
vo.setRouteActiveMonitoringTimeout(item.getRouteActiveMonitoringTimeout());
|
||||
vo.setCreated(item.getCreated());
|
||||
vo.setCreateTime(item.getCreateTime());
|
||||
vo.setRouteActiveStatus(item.getRouteActiveStatus());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<GatewayRouteListVO> voList = list.stream().map(GatewayRouteListVO::convert).collect(Collectors.toList());
|
||||
return getDataTable(voList);
|
||||
}
|
||||
|
||||
|
@ -74,23 +74,10 @@ public class GatewayServerController extends BaseController {
|
||||
.map(GatewayServer::getRouteId)
|
||||
.collect(Collectors.toSet());
|
||||
List<GatewayRoute> routeList = gatewayRouteService.selectGatewayRouteByIds(routeIds);
|
||||
Map<Long, String> routeIdAndNameMap = routeList.stream().collect(Collectors.toMap(GatewayRoute::getId, GatewayRoute::getRouteName));
|
||||
List<GatewayServerListVO> voList = 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.setRemark(item.getRemark());
|
||||
vo.setStatus(item.getStatus());
|
||||
vo.setCreated(item.getCreated());
|
||||
vo.setModified(item.getModified());
|
||||
vo.setServiceModel(item.getServiceModel());
|
||||
vo.setRouteId(item.getRouteId());
|
||||
vo.setCreateTime(item.getCreateTime());
|
||||
vo.setUpdateTime(item.getUpdateTime());
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
Map<Long, GatewayRoute> routeMap = routeList.stream().collect(Collectors.toMap(GatewayRoute::getId, Function.identity()));
|
||||
List<GatewayServerListVO> voList = list.stream()
|
||||
.map(item -> GatewayServerListVO.convert(item,GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel()) ? routeMap.get(item.getRouteId()) : null))
|
||||
.collect(Collectors.toList());
|
||||
return getDataTable(voList);
|
||||
}
|
||||
|
||||
@ -227,17 +214,9 @@ public class GatewayServerController extends BaseController {
|
||||
.map(GatewayServer::getRouteId)
|
||||
.collect(Collectors.toSet());
|
||||
List<GatewayRoute> routeList = gatewayRouteService.selectGatewayRouteByIds(routeIds);
|
||||
Map<Long, String> 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());
|
||||
Map<Long, GatewayRoute> routeMap = routeList.stream().collect(Collectors.toMap(GatewayRoute::getId, Function.identity()));
|
||||
return list.stream()
|
||||
.map(item -> GatewayServerListVO.convert(item,GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel()) ? routeMap.get(item.getRouteId()) : null))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.sf.service.gateway.domain.vo;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.sf.common.annotation.Excel;
|
||||
import com.sf.service.gateway.domain.GatewayRoute;
|
||||
import com.sf.service.gateway.enums.GatewayRouteType;
|
||||
@ -62,4 +63,23 @@ public class GatewayRouteListVO {
|
||||
@ApiModelProperty("路由活跃状态(0=探活未开启,1=状态获取中,2=正常,3=异常)")
|
||||
private String routeActiveStatus;
|
||||
|
||||
public static GatewayRouteListVO convert(GatewayRoute gatewayRoute){
|
||||
if (gatewayRoute == null) {
|
||||
return null;
|
||||
}
|
||||
GatewayRouteListVO vo = new GatewayRouteListVO();
|
||||
vo.setId(gatewayRoute.getId());
|
||||
vo.setRouteName(gatewayRoute.getRouteName());
|
||||
vo.setRequestProtocol(gatewayRoute.getRequestProtocol());
|
||||
vo.setRouteType(gatewayRoute.getRouteType());
|
||||
vo.setRouteContent(JSON.parseArray(gatewayRoute.getRouteContent(),GatewayRoute.RouteContent.class));
|
||||
vo.setRouteStatusActiveMonitoring(gatewayRoute.getRouteStatusActiveMonitoring());
|
||||
vo.setRouteActiveMonitoringPath(gatewayRoute.getRouteActiveMonitoringPath());
|
||||
vo.setRouteActiveMonitoringTimeout(gatewayRoute.getRouteActiveMonitoringTimeout());
|
||||
vo.setCreated(gatewayRoute.getCreated());
|
||||
vo.setCreateTime(gatewayRoute.getCreateTime());
|
||||
vo.setRouteActiveStatus(gatewayRoute.getRouteActiveStatus());
|
||||
return vo;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,9 @@ package com.sf.service.gateway.domain.vo;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.sf.common.annotation.Excel;
|
||||
import com.sf.common.core.domain.BaseEntity;
|
||||
import com.sf.service.gateway.domain.GatewayRoute;
|
||||
import com.sf.service.gateway.domain.GatewayServer;
|
||||
import com.sf.service.gateway.enums.GatewayServiceModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
@ -47,12 +50,38 @@ public class GatewayServerListVO {
|
||||
@ApiModelProperty("路由规则id")
|
||||
private Long routeId;
|
||||
|
||||
/** 创建时间 */
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/** 更新时间 */
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date updateTime;
|
||||
|
||||
|
||||
public static GatewayServerListVO convert(GatewayServer gatewayServer, GatewayRoute linkRoute) {
|
||||
if (gatewayServer == null) {
|
||||
return null;
|
||||
}
|
||||
GatewayServerListVO vo = new GatewayServerListVO();
|
||||
|
||||
vo.setId(gatewayServer.getId());
|
||||
vo.setServerName(gatewayServer.getServerName());
|
||||
vo.setServerAddress(GatewayServiceModel.ROUTE.getCode().equals(gatewayServer.getServiceModel()) ?
|
||||
linkRoute.getRouteName()
|
||||
: gatewayServer.getServerAddress());
|
||||
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());
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
|
@ -19,4 +19,11 @@ public interface IGatewayConfigService {
|
||||
GatewayConfig selectGatewayConfigByAppCode(String appCode);
|
||||
|
||||
void updateGatewayConfig(GatewayConfig gatewayConfig);
|
||||
|
||||
/**
|
||||
* 同步配置到网关
|
||||
*
|
||||
* @param appCode
|
||||
*/
|
||||
void synchronization(String appCode);
|
||||
}
|
||||
|
@ -1,20 +1,34 @@
|
||||
package com.sf.service.gateway.service.impl;
|
||||
|
||||
import apijson.JSON;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.sf.common.constant.HttpStatus;
|
||||
import com.sf.common.exception.ServiceException;
|
||||
import com.sf.common.utils.DateUtils;
|
||||
import com.sf.common.utils.SecurityUtils;
|
||||
import com.sf.service.gateway.domain.GatewayConfig;
|
||||
import com.sf.service.gateway.domain.*;
|
||||
import com.sf.service.gateway.enums.EncryptionAlgorithm;
|
||||
import com.sf.service.gateway.enums.GatewayDataStatus;
|
||||
import com.sf.service.gateway.enums.GatewayServiceModel;
|
||||
import com.sf.service.gateway.enums.GatewayStrategyType;
|
||||
import com.sf.service.gateway.mapper.GatewayConfigMapper;
|
||||
import com.sf.service.gateway.service.IGatewayConfigService;
|
||||
import com.sf.service.gateway.service.*;
|
||||
import com.sf.service.index.util.AppUtils;
|
||||
import com.sf.vertx.api.pojo.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 功能描述:
|
||||
@ -22,15 +36,37 @@ import java.util.Date;
|
||||
* @author a_kun
|
||||
* @date 2024/4/25 17:24
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class GatewayConfigServiceImpl implements IGatewayConfigService {
|
||||
|
||||
@Value("${sac.gateway.host:}")
|
||||
private String sacGatewayHost;
|
||||
|
||||
@Value("${sac.gateway.port:5566}")
|
||||
private Integer sacGatewayPort;
|
||||
|
||||
private static final String SYNC_CONFIG_PATH = "/vertx/app/config";
|
||||
|
||||
|
||||
@Resource
|
||||
private GatewayConfigMapper gatewayConfigMapper;
|
||||
|
||||
@Resource
|
||||
private IGatewayServerService gatewayServerService;
|
||||
|
||||
@Resource
|
||||
private IGatewayRouteService gatewayRouteService;
|
||||
|
||||
@Resource
|
||||
private IGatewayInterfaceInfoService gatewayInterfaceInfoService;
|
||||
|
||||
@Resource
|
||||
private IGatewayStrategyService gatewayStrategyService;
|
||||
|
||||
@Override
|
||||
public GatewayConfig selectGatewayConfigByAppCode(String appCode) {
|
||||
if (StringUtils.hasText(appCode)){
|
||||
if (StringUtils.hasText(appCode)) {
|
||||
return gatewayConfigMapper.selectByAppCode(appCode);
|
||||
}
|
||||
return null;
|
||||
@ -50,7 +86,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService {
|
||||
gatewayConfig.setCreateTime(nowDate);
|
||||
gatewayConfig.setUpdateTime(nowDate);
|
||||
gatewayConfigMapper.insert(gatewayConfig);
|
||||
}else {
|
||||
} else {
|
||||
gatewayConfig.setModified(username);
|
||||
gatewayConfig.setCreateTime(nowDate);
|
||||
gatewayConfig.setUpdateTime(nowDate);
|
||||
@ -58,36 +94,214 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService {
|
||||
}
|
||||
}
|
||||
|
||||
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(),"公钥内容不能为空");
|
||||
@Override
|
||||
public void synchronization(String appCode) {
|
||||
Assert.hasText(sacGatewayHost, "网关地址未配置");
|
||||
Assert.notNull(sacGatewayPort, "网关端口未配置");
|
||||
String urlStr = "http://" + sacGatewayHost + ":" + sacGatewayPort + SYNC_CONFIG_PATH;
|
||||
AppConfig appConfig = new AppConfig();
|
||||
appConfig.setAppCode(appCode);
|
||||
GatewayConfig gatewayConfig = selectGatewayConfigByAppCode(appCode);
|
||||
if (gatewayConfig != null) {
|
||||
appConfig.setDataSecurity(initDataSecurity(gatewayConfig));
|
||||
appConfig.setApiCurrentLimitingConfig(initApiCurrentLimitStrategy(gatewayConfig));
|
||||
appConfig.setAppCurrentLimitingConfig(initAppCurrentLimitStrategy(gatewayConfig));
|
||||
appConfig.setService(initService(appCode));
|
||||
// appConfig.setAdvancedConfig();
|
||||
}
|
||||
try {
|
||||
String response = HttpUtil.post(urlStr, JSON.toJSONString(appConfig));
|
||||
log.info("sync gateway response : {}", response);
|
||||
if (StringUtils.hasText(response)) {
|
||||
JSONObject jsonObject = JSON.parseObject(response);
|
||||
if (jsonObject.getIntValue("code") == HttpStatus.SUCCESS) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("网关配置同步失败:", e);
|
||||
throw new ServiceException("网关配置同步失败,请稍后重试");
|
||||
}
|
||||
throw new ServiceException("网关配置同步失败,请稍后重试");
|
||||
}
|
||||
|
||||
private List<SacService> initService(String appCode) {
|
||||
List<SacService> service = Collections.emptyList();
|
||||
GatewayServer queryServer = new GatewayServer();
|
||||
queryServer.setAppCode(appCode);
|
||||
queryServer.setStatus(GatewayDataStatus.ENABLE.getCode());
|
||||
List<GatewayServer> serverList = gatewayServerService.selectGatewayServerList(queryServer);
|
||||
if (CollUtil.isNotEmpty(serverList)) {
|
||||
// 如果是路由模式的服务,查询对应的路由服务信息
|
||||
Collection<Long> routeIds = serverList.stream().filter(item -> GatewayServiceModel.ROUTE.getCode().equals(item.getServiceModel()))
|
||||
.map(GatewayServer::getRouteId)
|
||||
.collect(Collectors.toSet());
|
||||
List<GatewayRoute> routeList = gatewayRouteService.selectGatewayRouteByIds(routeIds);
|
||||
Map<Long, GatewayRoute> routeMap = routeList.stream().collect(Collectors.toMap(GatewayRoute::getId, Function.identity()));
|
||||
// 查询接口
|
||||
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());
|
||||
Map<Long, List<GatewayInterfaceInfo>> groupByServerInterfaceMap = interfaceInfoList.stream().collect(Collectors.groupingBy(GatewayInterfaceInfo::getServerId));
|
||||
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()));
|
||||
// 启用的
|
||||
strategyList = strategyList.stream().filter(strategy -> GatewayDataStatus.ENABLE.getCode().equals(strategy.getStatus())).collect(Collectors.toList());
|
||||
Map<Long, GatewayStrategy> strategyMap = strategyList.stream().collect(Collectors.toMap(GatewayStrategy::getId, Function.identity()));
|
||||
return serverList.stream().map(server -> {
|
||||
SacService sacService = new SacService();
|
||||
sacService.setServiceName(server.getServerName());
|
||||
sacService.setServiceModel(server.getServiceModel());
|
||||
sacService.setServerAddress(convertToServerAddress(server.getServerAddress()));
|
||||
List<GatewayInterfaceInfo> currentServerApiList = groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList());
|
||||
List<ApiConfig> apiConfigList = currentServerApiList.stream().map(api -> {
|
||||
List<GatewayInterfaceLinkStrategy> currentApiLinkStrategy = groupByInterfaceLinkMap.getOrDefault(api.getId(), Collections.emptyList());
|
||||
List<Strategy> 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);
|
||||
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<GatewayRoute.RouteContent> 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;
|
||||
}
|
||||
|
||||
private Strategy convertToStrategy(GatewayStrategy currentStrategy) {
|
||||
Strategy strategy = new Strategy();
|
||||
strategy.setType(currentStrategy.getStrategyType());
|
||||
switch (GatewayStrategyType.getByCode(currentStrategy.getStrategyType())) {
|
||||
case CURRENT_LIMITING:
|
||||
strategy.setThreshold(currentStrategy.getCurrentLimitingThreshold());
|
||||
strategy.setTimeWindow(currentStrategy.getCurrentLimitingTimeWindow());
|
||||
strategy.setDefaultResponse(currentStrategy.getCurrentLimitingResponse());
|
||||
case CIRCUIT_BREAKER:
|
||||
strategy.setThreshold(currentStrategy.getCircuitBreakerThreshold());
|
||||
strategy.setTimeWindow(currentStrategy.getCircuitBreakerTimeWindow());
|
||||
strategy.setRecovery_interval(currentStrategy.getCircuitBreakerRecoveryInterval());
|
||||
strategy.setDefaultResponse(currentStrategy.getCircuitBreakerResponse());
|
||||
case AUTHENTICATION:
|
||||
case PERMISSION:
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
return strategy;
|
||||
}
|
||||
|
||||
private ServerAddress convertToServerAddress(String serverAddress) {
|
||||
if (StringUtils.hasText(serverAddress)) {
|
||||
try {
|
||||
URL url = new URL(serverAddress);
|
||||
ServerAddress address = new ServerAddress();
|
||||
address.setAddress(serverAddress);
|
||||
address.setProtocol(url.getProtocol());
|
||||
address.setHost(url.getHost());
|
||||
address.setPort(url.getPort() == -1 ? 80 : url.getPort());
|
||||
address.setPath(url.getPath());
|
||||
return address;
|
||||
} catch (MalformedURLException ignored) {
|
||||
}
|
||||
}
|
||||
if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getApiCurrentLimitingStatus())){
|
||||
Assert.notNull(gatewayConfig.getApiCurrentLimitingThreshold(),"限流值不能为空");
|
||||
Assert.isTrue(gatewayConfig.getApiCurrentLimitingThreshold() >= 0,"限流值>=0");
|
||||
return null;
|
||||
}
|
||||
|
||||
private Strategy initAppCurrentLimitStrategy(GatewayConfig gatewayConfig) {
|
||||
if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getAppCurrentLimitingStatus())) {
|
||||
Strategy appCurrentLimitStrategy = new Strategy();
|
||||
appCurrentLimitStrategy.setType(GatewayStrategyType.CURRENT_LIMITING.getCode());
|
||||
appCurrentLimitStrategy.setThreshold(gatewayConfig.getAppCurrentLimitingThreshold());
|
||||
appCurrentLimitStrategy.setTimeWindow(gatewayConfig.getAppCurrentLimitingTimeWindow());
|
||||
appCurrentLimitStrategy.setDefaultResponse(gatewayConfig.getAppCurrentLimitingDefaultResponse());
|
||||
return appCurrentLimitStrategy;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Strategy initApiCurrentLimitStrategy(GatewayConfig gatewayConfig) {
|
||||
if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getApiCurrentLimitingStatus())) {
|
||||
Strategy apiCurrentLimitStrategy = new Strategy();
|
||||
apiCurrentLimitStrategy.setType(GatewayStrategyType.CURRENT_LIMITING.getCode());
|
||||
apiCurrentLimitStrategy.setThreshold(gatewayConfig.getApiCurrentLimitingThreshold());
|
||||
apiCurrentLimitStrategy.setTimeWindow(gatewayConfig.getApiCurrentLimitingTimeWindow());
|
||||
apiCurrentLimitStrategy.setDefaultResponse(gatewayConfig.getApiCurrentLimitingDefaultResponse());
|
||||
return apiCurrentLimitStrategy;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化数据加密配置
|
||||
*
|
||||
* @param gatewayConfig
|
||||
*/
|
||||
private DataSecurity initDataSecurity(GatewayConfig gatewayConfig) {
|
||||
if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getDataEncryptionStatus())) {
|
||||
DataSecurity dataSecurity = new DataSecurity();
|
||||
dataSecurity.setAlgorithm(gatewayConfig.getEncryptionAlgorithm());
|
||||
dataSecurity.setPublicKey(gatewayConfig.getPublicKey());
|
||||
dataSecurity.setPrivateKey(gatewayConfig.getPrivateKey());
|
||||
return dataSecurity;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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");
|
||||
} else {
|
||||
Assert.isTrue(gatewayConfig.getApiCurrentLimitingTimeWindow() >= 0, "限流窗口值>=0");
|
||||
}
|
||||
Assert.isTrue(JSON.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息");
|
||||
Assert.isTrue(apijson.JSON.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息");
|
||||
}
|
||||
if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getAppCurrentLimitingStatus())){
|
||||
Assert.notNull(gatewayConfig.getAppCurrentLimitingThreshold(),"限流值不能为空");
|
||||
Assert.isTrue(gatewayConfig.getAppCurrentLimitingThreshold() >= 0,"限流值>=0");
|
||||
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");
|
||||
} else {
|
||||
Assert.isTrue(gatewayConfig.getAppCurrentLimitingTimeWindow() >= 0, "限流窗口值>=0");
|
||||
}
|
||||
Assert.isTrue(JSON.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息");
|
||||
Assert.isTrue(apijson.JSON.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user