From 13040c949e84b811cd11ab49d442a3dedd0cc366 Mon Sep 17 00:00:00 2001 From: ztzh_xieyun Date: Thu, 9 May 2024 10:48:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=8D=95?= =?UTF-8?q?=E8=8E=B7=E3=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sf/vertx/handle/AppConfigHandle.java | 43 +++++++++++++---- .../com/sf/vertx/handle/BodyHandlerImpl.java | 16 ++++++- .../handle/ParameterCheckHandlerImpl.java | 44 +++++++++++------- .../handle/RateLimitHandlerRedisImpl.java | 46 +++++++++++-------- .../handle/RestfulFailureHandlerImpl.java | 1 - .../service/impl/AppConfigServiceImpl.java | 2 +- .../impl/SharedClientHttpStreamEndpoint.java | 5 +- .../io/vertx/httpproxy/impl/ReverseProxy.java | 2 - 8 files changed, 106 insertions(+), 53 deletions(-) diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandle.java b/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandle.java index 1e050fe..6e3ba63 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandle.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandle.java @@ -67,10 +67,6 @@ public class AppConfigHandle { return DISABLED_APPCODE.contains(appCode); } - public static boolean appDataSecurity(String appCode) { - return CACHE_APP_CONFIG_MAP.get(appCode).getDataSecurity() != null ? true : false; - } - public static Strategy getGlobalAppCurrentLimitingConfig(String appCode) { return GLOBAL_APP_CURRENT_LIMITING_CONFIG_MAP.get(appCode); } @@ -84,7 +80,7 @@ public class AppConfigHandle { } public static boolean isDataSecurity(String appCode) { - return CACHE_APP_CONFIG_MAP.get(appCode).getDataSecurity() != null ? true : false; + return CACHE_APP_CONFIG_MAP.get(appCode) != null && CACHE_APP_CONFIG_MAP.get(appCode).getDataSecurity() != null ? true : false; } public static boolean isApiCodeCircuitBreaker(String key) { @@ -131,7 +127,7 @@ public class AppConfigHandle { public static void initAllAppConfig(RedisTemplate redisTemplate) throws Exception { Set set = redisTemplate.opsForZSet().range(RedisKeyConfig.APP_CONFIG_SET_KEY, 0, -1); for (String appCode : set) { - AppConfigHandle.initAppConfig(redisTemplate, appCode); + AppConfigHandle.initAppConfig(redisTemplate, appCode, false); } } @@ -146,7 +142,39 @@ public class AppConfigHandle { } } - public static void initAppConfig(RedisTemplate redisTemplate, String appCode) { + private static void delAppConfigCache(String appCode) { + AppConfig appConfig = CACHE_APP_CONFIG_MAP.get(appCode); + if(appConfig != null) { + // app、api默认限流 + GLOBAL_API_CURRENT_LIMITING_CONFIG_MAP.remove(appCode); + GLOBAL_APP_CURRENT_LIMITING_CONFIG_MAP.remove(appCode); + for (SacService sacService : appConfig.getService()) { + if (sacService.getApiConfig() != null && sacService.getApiConfig().size() > 0) { + for (ApiConfig apiConfig : sacService.getApiConfig()) { + String key = appCode + ":" + apiConfig.getApiCode(); + APICODE_CONFIG_MAP.remove(key); + LOADBALANCING_MAP.remove(key); + APICODE_CONFIG_CURRENT_LIMITING_MAP.remove(key); + String keyCircuitBreaker = key + ":" + "CIRCUIT_BREAKER"; + CircuitBreaker circuitBreaker = APICODE_CONFIG_CIRCUIT_BREAKER_MAP.get(keyCircuitBreaker); + if(circuitBreaker != null) { + circuitBreaker.close(); + APICODE_CONFIG_CIRCUIT_BREAKER_MAP.remove(keyCircuitBreaker); + } + } + } + } + // 应用配置 + CACHE_APP_CONFIG_MAP.remove(appCode); + } + } + + public static void initAppConfig(RedisTemplate redisTemplate, String appCode, boolean isDelLocalCache) { + // 是否需要先删除 + if(isDelLocalCache) { + delAppConfigCache(appCode); + } + String appCodeKey = RedisKeyConfig.APP_CONFIG_PREFIX_KEY + ":" + appCode; String appCodeValue = redisTemplate.opsForValue().get(appCodeKey); if (StringUtils.isNotBlank(appCodeValue)) { @@ -157,7 +185,6 @@ public class AppConfigHandle { // app、api默认限流 if (appConfig.getApiCurrentLimitingConfig() != null) { GLOBAL_API_CURRENT_LIMITING_CONFIG_MAP.put(appCode, appConfig.getApiCurrentLimitingConfig()); - } if (appConfig.getAppCurrentLimitingConfig() != null) { diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/BodyHandlerImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/BodyHandlerImpl.java index d9817e0..f3b69bc 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/BodyHandlerImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/BodyHandlerImpl.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; +import com.sf.vertx.constans.SacErrorCode; + /* * Copyright 2014 Red Hat, Inc. * @@ -35,6 +37,7 @@ import io.vertx.core.impl.logging.Logger; import io.vertx.core.impl.logging.LoggerFactory; import io.vertx.ext.web.FileUpload; import io.vertx.ext.web.RoutingContext; +import io.vertx.ext.web.handler.HttpException; import io.vertx.ext.web.impl.FileUploadImpl; import io.vertx.ext.web.impl.RoutingContextInternal; @@ -81,8 +84,17 @@ public class BodyHandlerImpl implements BodyHandler { // 1、是否配置全局加解密.
// 2、apiCode 配置熔断 String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER"; - if (AppConfigHandle.isDataSecurity(appCode) - || AppConfigHandle.isApiCodeCircuitBreaker(keyCircuitBreaker)) { + boolean isLoadBody = false; + try { + // TODO 测试异常 + isLoadBody = AppConfigHandle.isDataSecurity(appCode) + || AppConfigHandle.isApiCodeCircuitBreaker(keyCircuitBreaker); + } catch (Exception e) { + e.printStackTrace(); + context.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); + return; + } + if (isLoadBody) { // =======源码流程 // final HttpServerRequest request = context.request(); // final HttpServerResponse response = context.response(); diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/ParameterCheckHandlerImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/ParameterCheckHandlerImpl.java index 1f1f2ca..6839fc6 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/ParameterCheckHandlerImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/ParameterCheckHandlerImpl.java @@ -2,6 +2,8 @@ package com.sf.vertx.handle; import org.apache.commons.lang3.StringUtils; +import com.sf.vertx.constans.SacErrorCode; + import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.HttpException; @@ -9,26 +11,34 @@ public class ParameterCheckHandlerImpl implements ParameterCheckHandler { @Override public void handle(RoutingContext rc) { - String appCode = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey()); - String apiCode = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey()); - String key = appCode + ":" + apiCode; - if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode) - || AppConfigHandle.getAppConfig(appCode) == null - || AppConfigHandle.getApicodeConfigMap(key) == null) { - rc.fail(new HttpException(10012)); + try { + // TODO 测试异常 + String appCode = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey()); + String apiCode = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey()); + String key = appCode + ":" + apiCode; + if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode) + || AppConfigHandle.getAppConfig(appCode) == null + || AppConfigHandle.getApicodeConfigMap(key) == null) { + rc.fail(new HttpException(10012)); + return; + } + + if(AppConfigHandle.isDisabledAppcode(apiCode)) { + rc.fail(new HttpException(10001)); + return; + } + + String uri = rc.request().uri(); + if(AppConfigHandle.isApicodeUri(key, uri) == false) { + rc.fail(new HttpException(10018)); + return; + } + } catch (Exception e) { + e.printStackTrace(); + rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); return; } - if(AppConfigHandle.isDisabledAppcode(apiCode)) { - rc.fail(new HttpException(10001)); - return; - } - - String uri = rc.request().uri(); - if(AppConfigHandle.isApicodeUri(key, uri) == false) { - rc.fail(new HttpException(10018)); - return; - } rc.next(); return; diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/RateLimitHandlerRedisImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/RateLimitHandlerRedisImpl.java index 4f7a9a2..47dd930 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/RateLimitHandlerRedisImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/RateLimitHandlerRedisImpl.java @@ -3,6 +3,7 @@ package com.sf.vertx.handle; import java.util.Map; import com.sf.vertx.api.pojo.Strategy; +import com.sf.vertx.constans.SacErrorCode; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.HttpException; @@ -25,29 +26,36 @@ public class RateLimitHandlerRedisImpl implements RateLimitHandler { @Override public void handle(RoutingContext rc) { - String appCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey()); - String apiCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey()); - Strategy apiCodeStrategy = AppConfigHandle.getApiCurrentLimiting(appCodeHeaderKey + ":" + apiCodeHeaderKey); + try { + // TODO 测试异常 + String appCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey()); + String apiCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey()); + Strategy apiCodeStrategy = AppConfigHandle.getApiCurrentLimiting(appCodeHeaderKey + ":" + apiCodeHeaderKey); - Strategy globalApiStrategy = AppConfigHandle.getGlobalApiCurrentLimitingConfig(appCodeHeaderKey); - Strategy globalAppStrategy = AppConfigHandle.getGlobalAppCurrentLimitingConfig(appCodeHeaderKey); + Strategy globalApiStrategy = AppConfigHandle.getGlobalApiCurrentLimitingConfig(appCodeHeaderKey); + Strategy globalAppStrategy = AppConfigHandle.getGlobalAppCurrentLimitingConfig(appCodeHeaderKey); - Strategy apiStrategy = apiCodeStrategy != null ? apiCodeStrategy - : globalApiStrategy != null ? globalApiStrategy : null; - Strategy appStrategy = globalAppStrategy != null ? globalAppStrategy : null; - - if (apiStrategy != null || appStrategy != null) { - Map retMap = rateLimiter.acquire(rc, apiStrategy, appStrategy); + Strategy apiStrategy = apiCodeStrategy != null ? apiCodeStrategy + : globalApiStrategy != null ? globalApiStrategy : null; + Strategy appStrategy = globalAppStrategy != null ? globalAppStrategy : null; - if (apiStrategy != null && retMap.get(1) == false) { - rc.fail(new HttpException(10015, apiStrategy.getDefaultResponse())); - return; - } - - if (appStrategy != null && retMap.get(2) == false) { - rc.fail(new HttpException(10017, appStrategy.getDefaultResponse())); - return; + if (apiStrategy != null || appStrategy != null) { + Map retMap = rateLimiter.acquire(rc, apiStrategy, appStrategy); + + if (apiStrategy != null && retMap.get(1) == false) { + rc.fail(new HttpException(10015, apiStrategy.getDefaultResponse())); + return; + } + + if (appStrategy != null && retMap.get(2) == false) { + rc.fail(new HttpException(10017, appStrategy.getDefaultResponse())); + return; + } } + } catch (Exception e) { + e.printStackTrace(); + rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); + return; } rc.next(); return; diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java index fd8ee54..9938310 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java @@ -3,7 +3,6 @@ package com.sf.vertx.handle; import org.apache.commons.lang3.StringUtils; import com.sf.vertx.constans.SacErrorCode; -import com.sf.vertx.utils.ProxyTool; import io.vertx.core.http.HttpHeaders; import io.vertx.core.json.JsonObject; diff --git a/sf-vertx/src/main/java/com/sf/vertx/service/impl/AppConfigServiceImpl.java b/sf-vertx/src/main/java/com/sf/vertx/service/impl/AppConfigServiceImpl.java index 7399fe8..b0a0277 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/service/impl/AppConfigServiceImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/service/impl/AppConfigServiceImpl.java @@ -33,7 +33,7 @@ public class AppConfigServiceImpl implements AppConfigService { redisTemplate.opsForValue().set(appCodeKey, JSONObject.toJSONString(appConfig)); // 初始化AppConfig本地缓存 - AppConfigHandle.initAppConfig(redisTemplate, appConfig.getAppCode()); + AppConfigHandle.initAppConfig(redisTemplate, appConfig.getAppCode(), true); } diff --git a/sf-vertx/src/main/java/io/vertx/core/http/impl/SharedClientHttpStreamEndpoint.java b/sf-vertx/src/main/java/io/vertx/core/http/impl/SharedClientHttpStreamEndpoint.java index 8874d7e..2ca566c 100644 --- a/sf-vertx/src/main/java/io/vertx/core/http/impl/SharedClientHttpStreamEndpoint.java +++ b/sf-vertx/src/main/java/io/vertx/core/http/impl/SharedClientHttpStreamEndpoint.java @@ -108,17 +108,16 @@ class SharedClientHttpStreamEndpoint extends ClientHttpEndpointBase { // Executed when the circuit is opened - log.info("Executed when the circuit is opened."); + log.info("CONNECTION_CIRCUIT_BREAKER Executed when the circuit is opened."); return v; }, ar -> { // Do something with the result - log.info("failed result."); if (ar.result() != null) { if (ar.result() instanceof ConnectException) { // TODO 配置重试策略 diff --git a/sf-vertx/src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java b/sf-vertx/src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java index 7f856f4..e9ea987 100644 --- a/sf-vertx/src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java +++ b/sf-vertx/src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java @@ -388,8 +388,6 @@ public class ReverseProxy implements HttpProxy { String appCode = proxyRequest.headers().get(AppConfigHandle.getAppCodeHeaderKey()); String apiCode = proxyRequest.headers().get(AppConfigHandle.getApiCodeHeaderKey()); String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER"; - - // 熔断 CircuitBreaker circuitBreaker = AppConfigHandle.getApiCodeCircuitBreaker(keyCircuitBreaker); boolean isDataSecurity = AppConfigHandle.isDataSecurity(appCode); if (isDataSecurity || circuitBreaker != null) {