添加异常捕获、
This commit is contained in:
parent
53a4ac7ab5
commit
13040c949e
@ -67,10 +67,6 @@ public class AppConfigHandle {
|
|||||||
return DISABLED_APPCODE.contains(appCode);
|
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) {
|
public static Strategy getGlobalAppCurrentLimitingConfig(String appCode) {
|
||||||
return GLOBAL_APP_CURRENT_LIMITING_CONFIG_MAP.get(appCode);
|
return GLOBAL_APP_CURRENT_LIMITING_CONFIG_MAP.get(appCode);
|
||||||
}
|
}
|
||||||
@ -84,7 +80,7 @@ public class AppConfigHandle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isDataSecurity(String appCode) {
|
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) {
|
public static boolean isApiCodeCircuitBreaker(String key) {
|
||||||
@ -131,7 +127,7 @@ public class AppConfigHandle {
|
|||||||
public static void initAllAppConfig(RedisTemplate<String, String> redisTemplate) throws Exception {
|
public static void initAllAppConfig(RedisTemplate<String, String> redisTemplate) throws Exception {
|
||||||
Set<String> set = redisTemplate.opsForZSet().range(RedisKeyConfig.APP_CONFIG_SET_KEY, 0, -1);
|
Set<String> set = redisTemplate.opsForZSet().range(RedisKeyConfig.APP_CONFIG_SET_KEY, 0, -1);
|
||||||
for (String appCode : set) {
|
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) {
|
||||||
|
// 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<String, String> redisTemplate, String appCode, boolean isDelLocalCache) {
|
||||||
|
// 是否需要先删除
|
||||||
|
if(isDelLocalCache) {
|
||||||
|
delAppConfigCache(appCode);
|
||||||
|
}
|
||||||
|
|
||||||
String appCodeKey = RedisKeyConfig.APP_CONFIG_PREFIX_KEY + ":" + appCode;
|
String appCodeKey = RedisKeyConfig.APP_CONFIG_PREFIX_KEY + ":" + appCode;
|
||||||
String appCodeValue = redisTemplate.opsForValue().get(appCodeKey);
|
String appCodeValue = redisTemplate.opsForValue().get(appCodeKey);
|
||||||
if (StringUtils.isNotBlank(appCodeValue)) {
|
if (StringUtils.isNotBlank(appCodeValue)) {
|
||||||
@ -157,7 +185,6 @@ public class AppConfigHandle {
|
|||||||
// app、api默认限流
|
// app、api默认限流
|
||||||
if (appConfig.getApiCurrentLimitingConfig() != null) {
|
if (appConfig.getApiCurrentLimitingConfig() != null) {
|
||||||
GLOBAL_API_CURRENT_LIMITING_CONFIG_MAP.put(appCode, appConfig.getApiCurrentLimitingConfig());
|
GLOBAL_API_CURRENT_LIMITING_CONFIG_MAP.put(appCode, appConfig.getApiCurrentLimitingConfig());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (appConfig.getAppCurrentLimitingConfig() != null) {
|
if (appConfig.getAppCurrentLimitingConfig() != null) {
|
||||||
|
@ -5,6 +5,8 @@ import java.util.List;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import com.sf.vertx.constans.SacErrorCode;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2014 Red Hat, Inc.
|
* 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.core.impl.logging.LoggerFactory;
|
||||||
import io.vertx.ext.web.FileUpload;
|
import io.vertx.ext.web.FileUpload;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
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.FileUploadImpl;
|
||||||
import io.vertx.ext.web.impl.RoutingContextInternal;
|
import io.vertx.ext.web.impl.RoutingContextInternal;
|
||||||
|
|
||||||
@ -81,8 +84,17 @@ public class BodyHandlerImpl implements BodyHandler {
|
|||||||
// 1、是否配置全局加解密.<br/>
|
// 1、是否配置全局加解密.<br/>
|
||||||
// 2、apiCode 配置熔断
|
// 2、apiCode 配置熔断
|
||||||
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
||||||
if (AppConfigHandle.isDataSecurity(appCode)
|
boolean isLoadBody = false;
|
||||||
|| AppConfigHandle.isApiCodeCircuitBreaker(keyCircuitBreaker)) {
|
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 HttpServerRequest request = context.request();
|
||||||
// final HttpServerResponse response = context.response();
|
// final HttpServerResponse response = context.response();
|
||||||
|
@ -2,6 +2,8 @@ package com.sf.vertx.handle;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import com.sf.vertx.constans.SacErrorCode;
|
||||||
|
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.handler.HttpException;
|
import io.vertx.ext.web.handler.HttpException;
|
||||||
|
|
||||||
@ -9,26 +11,34 @@ public class ParameterCheckHandlerImpl implements ParameterCheckHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(RoutingContext rc) {
|
public void handle(RoutingContext rc) {
|
||||||
String appCode = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey());
|
try {
|
||||||
String apiCode = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey());
|
// TODO 测试异常
|
||||||
String key = appCode + ":" + apiCode;
|
String appCode = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey());
|
||||||
if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode)
|
String apiCode = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey());
|
||||||
|| AppConfigHandle.getAppConfig(appCode) == null
|
String key = appCode + ":" + apiCode;
|
||||||
|| AppConfigHandle.getApicodeConfigMap(key) == null) {
|
if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode)
|
||||||
rc.fail(new HttpException(10012));
|
|| 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;
|
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();
|
rc.next();
|
||||||
return;
|
return;
|
||||||
|
@ -3,6 +3,7 @@ package com.sf.vertx.handle;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.sf.vertx.api.pojo.Strategy;
|
import com.sf.vertx.api.pojo.Strategy;
|
||||||
|
import com.sf.vertx.constans.SacErrorCode;
|
||||||
|
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.handler.HttpException;
|
import io.vertx.ext.web.handler.HttpException;
|
||||||
@ -25,29 +26,36 @@ public class RateLimitHandlerRedisImpl implements RateLimitHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(RoutingContext rc) {
|
public void handle(RoutingContext rc) {
|
||||||
String appCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getAppCodeHeaderKey());
|
try {
|
||||||
String apiCodeHeaderKey = rc.request().headers().get(AppConfigHandle.getApiCodeHeaderKey());
|
// TODO 测试异常
|
||||||
Strategy apiCodeStrategy = AppConfigHandle.getApiCurrentLimiting(appCodeHeaderKey + ":" + apiCodeHeaderKey);
|
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 globalApiStrategy = AppConfigHandle.getGlobalApiCurrentLimitingConfig(appCodeHeaderKey);
|
||||||
Strategy globalAppStrategy = AppConfigHandle.getGlobalAppCurrentLimitingConfig(appCodeHeaderKey);
|
Strategy globalAppStrategy = AppConfigHandle.getGlobalAppCurrentLimitingConfig(appCodeHeaderKey);
|
||||||
|
|
||||||
Strategy apiStrategy = apiCodeStrategy != null ? apiCodeStrategy
|
Strategy apiStrategy = apiCodeStrategy != null ? apiCodeStrategy
|
||||||
: globalApiStrategy != null ? globalApiStrategy : null;
|
: globalApiStrategy != null ? globalApiStrategy : null;
|
||||||
Strategy appStrategy = globalAppStrategy != null ? globalAppStrategy : null;
|
Strategy appStrategy = globalAppStrategy != null ? globalAppStrategy : null;
|
||||||
|
|
||||||
if (apiStrategy != null || appStrategy != null) {
|
if (apiStrategy != null || appStrategy != null) {
|
||||||
Map<Integer, Boolean> retMap = rateLimiter.acquire(rc, apiStrategy, appStrategy);
|
Map<Integer, Boolean> retMap = rateLimiter.acquire(rc, apiStrategy, appStrategy);
|
||||||
|
|
||||||
if (apiStrategy != null && retMap.get(1) == false) {
|
if (apiStrategy != null && retMap.get(1) == false) {
|
||||||
rc.fail(new HttpException(10015, apiStrategy.getDefaultResponse()));
|
rc.fail(new HttpException(10015, apiStrategy.getDefaultResponse()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (appStrategy != null && retMap.get(2) == false) {
|
if (appStrategy != null && retMap.get(2) == false) {
|
||||||
rc.fail(new HttpException(10017, appStrategy.getDefaultResponse()));
|
rc.fail(new HttpException(10017, appStrategy.getDefaultResponse()));
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
rc.next();
|
rc.next();
|
||||||
return;
|
return;
|
||||||
|
@ -3,7 +3,6 @@ package com.sf.vertx.handle;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import com.sf.vertx.constans.SacErrorCode;
|
import com.sf.vertx.constans.SacErrorCode;
|
||||||
import com.sf.vertx.utils.ProxyTool;
|
|
||||||
|
|
||||||
import io.vertx.core.http.HttpHeaders;
|
import io.vertx.core.http.HttpHeaders;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
|
@ -33,7 +33,7 @@ public class AppConfigServiceImpl implements AppConfigService {
|
|||||||
redisTemplate.opsForValue().set(appCodeKey, JSONObject.toJSONString(appConfig));
|
redisTemplate.opsForValue().set(appCodeKey, JSONObject.toJSONString(appConfig));
|
||||||
|
|
||||||
// 初始化AppConfig本地缓存
|
// 初始化AppConfig本地缓存
|
||||||
AppConfigHandle.initAppConfig(redisTemplate, appConfig.getAppCode());
|
AppConfigHandle.initAppConfig(redisTemplate, appConfig.getAppCode(), true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,17 +108,16 @@ class SharedClientHttpStreamEndpoint extends ClientHttpEndpointBase<Lease<HttpCl
|
|||||||
promise.complete(null);
|
promise.complete(null);
|
||||||
} else {
|
} else {
|
||||||
// handler.handle(Future.failedFuture(ar.cause()));
|
// handler.handle(Future.failedFuture(ar.cause()));
|
||||||
log.info("===============execute error");
|
log.info("CONNECTION_CIRCUIT_BREAKER ===============execute error");
|
||||||
promise.fail(ar.cause());
|
promise.fail(ar.cause());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, v -> {
|
}, v -> {
|
||||||
// Executed when the circuit is opened
|
// 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;
|
return v;
|
||||||
}, ar -> {
|
}, ar -> {
|
||||||
// Do something with the result
|
// Do something with the result
|
||||||
log.info("failed result.");
|
|
||||||
if (ar.result() != null) {
|
if (ar.result() != null) {
|
||||||
if (ar.result() instanceof ConnectException) {
|
if (ar.result() instanceof ConnectException) {
|
||||||
// TODO 配置重试策略
|
// TODO 配置重试策略
|
||||||
|
@ -388,8 +388,6 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
String appCode = proxyRequest.headers().get(AppConfigHandle.getAppCodeHeaderKey());
|
String appCode = proxyRequest.headers().get(AppConfigHandle.getAppCodeHeaderKey());
|
||||||
String apiCode = proxyRequest.headers().get(AppConfigHandle.getApiCodeHeaderKey());
|
String apiCode = proxyRequest.headers().get(AppConfigHandle.getApiCodeHeaderKey());
|
||||||
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
||||||
|
|
||||||
// 熔断
|
|
||||||
CircuitBreaker circuitBreaker = AppConfigHandle.getApiCodeCircuitBreaker(keyCircuitBreaker);
|
CircuitBreaker circuitBreaker = AppConfigHandle.getApiCodeCircuitBreaker(keyCircuitBreaker);
|
||||||
boolean isDataSecurity = AppConfigHandle.isDataSecurity(appCode);
|
boolean isDataSecurity = AppConfigHandle.isDataSecurity(appCode);
|
||||||
if (isDataSecurity || circuitBreaker != null) {
|
if (isDataSecurity || circuitBreaker != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user