支持域名访问
This commit is contained in:
parent
90efb2bf18
commit
fbec1d3137
@ -36,6 +36,7 @@ public class SacErrorCode {
|
||||
_ERROR.put(10019, "请求不支持conetnt-type类型");
|
||||
_ERROR.put(10020, "uri返回mock数据");
|
||||
_ERROR.put(10021, "无法找到负载均衡路由节点");
|
||||
_ERROR.put(10022, "uri模糊匹配,不能使用加解密、熔断策略.");
|
||||
};
|
||||
|
||||
public static JsonObject returnErrorMsg(Integer errorCode) {
|
||||
|
@ -82,6 +82,7 @@ public class AppConfigHandler {
|
||||
private static ConcurrentHashMap<String, SacCurrentLimiting> APICODE_CONFIG_CURRENT_LIMITING_MAP = new ConcurrentHashMap<>();
|
||||
// 服务类型, apiCode限流配置 appCode:apiCode - 服务类型,SAC=SAC规范服务,OPEN=开放服务
|
||||
private static ConcurrentHashMap<String, String> APICODE_CONFIG_SERVICE_TYPE_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
// 负载均衡路由类型 appCode:apiCode - routerType
|
||||
// 执行流程 routerType= <br/>
|
||||
// 1、serviceNodel="NORMAL", serviceNodel="ROUTE" and RouteType = "WEIGHT_ROUTE"
|
||||
@ -95,9 +96,16 @@ public class AppConfigHandler {
|
||||
// apiCode熔断配置 appCode:apiCode - CircuitBreaker
|
||||
private static ConcurrentHashMap<String, CircuitBreaker> APICODE_CONFIG_CIRCUIT_BREAKER_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
// apicode uri = * - appConfig
|
||||
private static ConcurrentHashMap<String, AppConfig> APICODE_APPCONFIG_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
// 禁用appCode
|
||||
private static ConcurrentHashSet<String> DISABLED_APPCODE = new ConcurrentHashSet<String>();
|
||||
|
||||
public static AppConfig getAppConfigByDomain(String domain) {
|
||||
return APICODE_APPCONFIG_MAP.get(domain);
|
||||
}
|
||||
|
||||
public static Integer routerType(String key) {
|
||||
return APICODE_CONFIG_ROUTERTYPE_MAP.get(key) != null ? APICODE_CONFIG_ROUTERTYPE_MAP.get(key) : 1;
|
||||
}
|
||||
@ -168,6 +176,11 @@ public class AppConfigHandler {
|
||||
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
||||
CircuitBreaker circuitBreaker = AppConfigHandler.getApiCodeCircuitBreaker(keyCircuitBreaker);
|
||||
boolean isDataSecurity = AppConfigHandler.isDataSecurity(appCode);
|
||||
// 模糊匹配不解析body
|
||||
String uri = AppConfigHandler.getApicodeConfig(appCode + ":" + apiCode).getUri();
|
||||
if (StringUtils.equals(uri, "*")) {
|
||||
return false;
|
||||
}
|
||||
// 文件上传不走加解密
|
||||
return (isDataSecurity || circuitBreaker != null) && StringUtils.startsWith(contentType, "multipart") == false;
|
||||
}
|
||||
@ -224,7 +237,7 @@ public class AppConfigHandler {
|
||||
}
|
||||
|
||||
public static String mock(String key) {
|
||||
return APICODE_CONFIG_MAP.get(key).getMockResponse();
|
||||
return APICODE_CONFIG_MAP.get(key) != null ? APICODE_CONFIG_MAP.get(key).getMockResponse() : null;
|
||||
}
|
||||
|
||||
private static RateLimiterRegistry createRateLimiter(Strategy strategy) {
|
||||
@ -357,6 +370,11 @@ public class AppConfigHandler {
|
||||
APICODE_CONFIG_MAP.put(key, apiConfig);
|
||||
APICODE_CONFIG_SERVICE_TYPE_MAP.put(key, sacService.getServiceType());
|
||||
|
||||
// OPEN模式, 域名映射
|
||||
if(isServiceTypeOpen(key) && StringUtils.equals(apiConfig.getUri(), "*")) {
|
||||
APICODE_APPCONFIG_MAP.put(apiConfig.getApiCode(), appConfig);
|
||||
}
|
||||
|
||||
// 负载均衡模式
|
||||
APICODE_CONFIG_ROUTERTYPE_MAP.put(key, routerType);
|
||||
switch (routerType) {
|
||||
|
@ -5,6 +5,8 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sf.vertx.constans.SacErrorCode;
|
||||
|
||||
/*
|
||||
@ -83,7 +85,6 @@ public class BodyHandlerImpl implements BodyHandler {
|
||||
String contentType = context.request().headers().get(HttpHeaders.CONTENT_TYPE);
|
||||
boolean isLoadBody = false;
|
||||
try {
|
||||
// TODO 测试异常
|
||||
isLoadBody = AppConfigHandler.isAnalysisBody(appCode, apiCode, contentType);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -2,25 +2,39 @@ package com.sf.vertx.handle;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sf.vertx.api.pojo.AppConfig;
|
||||
import com.sf.vertx.constans.SacErrorCode;
|
||||
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.HttpException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ParameterCheckHandlerImpl implements ParameterCheckHandler {
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext rc) {
|
||||
try {
|
||||
log.info("rc.request().host():{}", rc.request().host());
|
||||
String appCode = rc.request().headers().get(AppConfigHandler.getAppCodeHeaderKey());
|
||||
String apiCode = rc.request().headers().get(AppConfigHandler.getApiCodeHeaderKey());
|
||||
String key = appCode + ":" + apiCode;
|
||||
if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode)
|
||||
|| AppConfigHandler.getAppConfig(appCode) == null
|
||||
|| AppConfigHandler.getApicodeConfig(key) == null) {
|
||||
// 判断OPEN模式, header不传递参数, 走域名映射
|
||||
AppConfig appConfig = AppConfigHandler.getAppConfigByDomain(rc.request().host());
|
||||
if(appConfig != null) {
|
||||
appCode = appConfig.getAppCode();
|
||||
apiCode = rc.request().host();
|
||||
rc.request().headers().add(AppConfigHandler.getAppCodeHeaderKey(), appCode);
|
||||
rc.request().headers().add(AppConfigHandler.getApiCodeHeaderKey(), apiCode);
|
||||
key = appCode + ":" + apiCode;
|
||||
} else {
|
||||
rc.fail(new HttpException(10012));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(AppConfigHandler.isDisabledAppcode(apiCode)) {
|
||||
rc.fail(new HttpException(10001));
|
||||
|
@ -20,6 +20,7 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler {
|
||||
Integer sc = SacErrorCode.DEFAULT_ERROR_CODE;
|
||||
try {
|
||||
Throwable failure = frc.failure();
|
||||
log.info("failure:{}", failure);
|
||||
if (failure instanceof HttpException) {
|
||||
HttpException httpException = (HttpException) failure;
|
||||
sc = httpException.getStatusCode();
|
||||
|
Loading…
x
Reference in New Issue
Block a user