From 7887d1ac62332d4bfb18703113f553e18590f740 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 21 Jan 2020 09:40:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=99=90=E6=B5=81BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/DefaultLimitConfigManager.java | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java index 54efd924..0af913b8 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java @@ -3,6 +3,7 @@ package com.gitee.sop.gatewaycommon.manager; import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto; import org.apache.commons.lang.StringUtils; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -39,22 +40,60 @@ public class DefaultLimitConfigManager implements LimitConfigManager { } } + /** + * // 根据路由ID限流 + * routeId, + * // 根据appKey限流 + * appKey, + * // 根据路由ID + appKey限流 + * routeId + appKey, + * + * // 根据ip限流 + * ip, + * // 根据ip+路由id限流 + * ip + routeId, + * // 根据ip+appKey限流 + * ip + appKey, + * // 根据ip+路由id+appKey限流 + * ip + routeId + appKey, + * @param configLimitDto + * @return + */ protected Set buildKeys(ConfigLimitDto configLimitDto) { Set keys = new HashSet<>(); String routeId = Optional.ofNullable(configLimitDto.getRouteId()).orElse(""); String appKey = Optional.ofNullable(configLimitDto.getAppKey()).orElse(""); String limitIp = Optional.ofNullable(configLimitDto.getLimitIp()).orElse("").replaceAll("\\s", ""); - String baseKey = routeId.trim() + appKey.trim(); - keys.add(baseKey); - + // 根据路由ID限流 + if (StringUtils.isNotBlank(routeId) && StringUtils.isBlank(appKey) && StringUtils.isBlank(limitIp)) { + keys.add(routeId); + } + // 根据appKey限流 + if (StringUtils.isBlank(routeId) && StringUtils.isNotBlank(appKey) && StringUtils.isBlank(limitIp)) { + keys.add(appKey); + } + // 根据路由ID + appKey限流 + if (StringUtils.isNotBlank(routeId) && StringUtils.isNotBlank(appKey) && StringUtils.isBlank(limitIp)) { + keys.add(routeId.trim() + appKey.trim()); + } + Set baseKeys = new HashSet<>(keys); + // 根据ip限流 + if (StringUtils.isBlank(routeId) && StringUtils.isBlank(appKey) && StringUtils.isNotBlank(limitIp)) { + String[] ips = limitIp.split("\\,|\\,"); + keys.addAll(Arrays.asList(ips)); + } + // 根据ip+路由id限流 + // 根据ip+appKey限流 + // 根据ip+路由id+appKey限流 if (StringUtils.isNotBlank(limitIp)) { String[] ips = limitIp.split("\\,|\\,"); for (String ip : ips) { - keys.add(ip + baseKey); + for (String baseKey : baseKeys) { + keys.add(ip + baseKey); + } } } - return keys; }