添加异常捕获、

This commit is contained in:
ztzh_xieyun 2024-05-09 10:48:42 +08:00
parent 53a4ac7ab5
commit 13040c949e
8 changed files with 106 additions and 53 deletions

View File

@ -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<String, String> redisTemplate) throws Exception {
Set<String> 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<String, String> redisTemplate, String appCode) {
private static void delAppConfigCache(String appCode) {
AppConfig appConfig = CACHE_APP_CONFIG_MAP.get(appCode);
if(appConfig != null) {
// appapi默认限流
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<String, String> 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 {
// appapi默认限流
if (appConfig.getApiCurrentLimitingConfig() != null) {
GLOBAL_API_CURRENT_LIMITING_CONFIG_MAP.put(appCode, appConfig.getApiCurrentLimitingConfig());
}
if (appConfig.getAppCurrentLimitingConfig() != null) {

View File

@ -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是否配置全局加解密.<br/>
// 2apiCode 配置熔断
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();

View File

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

View File

@ -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<Integer, Boolean> 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<Integer, Boolean> 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;

View File

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

View File

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

View File

@ -108,17 +108,16 @@ class SharedClientHttpStreamEndpoint extends ClientHttpEndpointBase<Lease<HttpCl
promise.complete(null);
} else {
// handler.handle(Future.failedFuture(ar.cause()));
log.info("===============execute error");
log.info("CONNECTION_CIRCUIT_BREAKER ===============execute error");
promise.fail(ar.cause());
}
});
}, v -> {
// 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 配置重试策略

View File

@ -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) {