优化Gateway限流

1.x
tanghc 5 years ago
parent 13f8686000
commit 2ced9cf894
  1. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java
  2. 8
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway.configuration;
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.LimitFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.ParameterFormatterFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.ValidateFilter;
@ -90,6 +91,11 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
return new ValidateFilter();
}
@Bean
LimitFilter limitFilter() {
return new LimitFilter();
}
@Bean
LoadBalancerClientExtFilter loadBalancerClientExtFilter() {
return new LoadBalancerClientExtFilter();

@ -17,7 +17,6 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@ -41,7 +40,7 @@ public class LimitFilter implements GlobalFilter, Ordered {
}
// 单个限流功能未开启
if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) {
return null;
return chain.filter(exchange);
}
byte limitType = configLimitDto.getLimitType().byteValue();
LimitManager limitManager = ApiConfig.getInstance().getLimitManager();
@ -82,12 +81,15 @@ public class LimitFilter implements GlobalFilter, Ordered {
List<ConfigLimitDto> limitConfigList = new ArrayList<>();
for (String limitKey : limitKeys) {
ConfigLimitDto configLimitDto = limitConfigManager.get(limitKey);
if (configLimitDto == null) {
continue;
}
limitConfigList.add(configLimitDto);
}
if (limitConfigList.isEmpty()) {
return null;
}
Collections.sort(limitConfigList, Comparator.comparing(ConfigLimitDto::getOrderIndex));
limitConfigList.sort(Comparator.comparing(ConfigLimitDto::getOrderIndex));
return limitConfigList.get(0);
}
}

Loading…
Cancel
Save