diff --git a/changelog.md b/changelog.md index e81dc56c..cddbe075 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # changelog +## 3.0.1 + +- 增强国际化消息(现SpringCouldGateway支持英文国际化) +- 优化限流配置页 + ## 3.0.0 - 重构spring cloud gateway网关 diff --git a/sop-auth/pom.xml b/sop-auth/pom.xml index 113cfff8..908e6bb1 100644 --- a/sop-auth/pom.xml +++ b/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 3a464160..87f7582c 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT pom diff --git a/sop-common/sop-bridge-gateway/pom.xml b/sop-common/sop-bridge-gateway/pom.xml index aa776b53..2595ecfc 100644 --- a/sop-common/sop-bridge-gateway/pom.xml +++ b/sop-common/sop-bridge-gateway/pom.xml @@ -5,11 +5,11 @@ sop-common com.gitee.sop - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT ../pom.xml 4.0.0 - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT sop-bridge-gateway @@ -17,7 +17,7 @@ com.gitee.sop sop-gateway-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT diff --git a/sop-common/sop-bridge-zuul/pom.xml b/sop-common/sop-bridge-zuul/pom.xml index 723ce3d0..a0171904 100644 --- a/sop-common/sop-bridge-zuul/pom.xml +++ b/sop-common/sop-bridge-zuul/pom.xml @@ -5,11 +5,11 @@ sop-common com.gitee.sop - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT ../pom.xml 4.0.0 - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT sop-bridge-zuul @@ -17,7 +17,7 @@ com.gitee.sop sop-gateway-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 040b57d8..1ecfe1d0 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT ../pom.xml sop-gateway-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ServiceBeanInitializer.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ServiceBeanInitializer.java new file mode 100644 index 00000000..2fd9cb32 --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ServiceBeanInitializer.java @@ -0,0 +1,11 @@ +package com.gitee.sop.gatewaycommon.bean; + +import com.gitee.sop.gatewaycommon.manager.ChannelMsgProcessor; + +/** + * @author tanghc + */ +public interface ServiceBeanInitializer extends ChannelMsgProcessor { + + void load(String serviceId); +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java index ba322249..44c54282 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java @@ -8,6 +8,7 @@ import com.gitee.sop.gatewaycommon.result.ResultExecutorForZuul; import com.gitee.sop.gatewaycommon.zuul.result.ZuulResultExecutor; import com.netflix.zuul.context.RequestContext; +import java.util.Locale; import java.util.Optional; /** @@ -32,9 +33,10 @@ public class EasyopenResultExecutor implements ResultExecutorForZuul { } @Override - public String buildErrorResult(RequestContext request, Throwable ex) { + public String buildErrorResult(RequestContext requestContext, Throwable ex) { ApiResult apiResult = new ApiResult(); - Error error = ZuulResultExecutor.getError(ex); + Locale locale = requestContext.getRequest().getLocale(); + Error error = ZuulResultExecutor.getError(locale, ex); apiResult.setCode(error.getSub_code()); apiResult.setMsg(error.getSub_msg()); return JSON.toJSONString(apiResult); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/exception/ApiException.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/exception/ApiException.java index af0da78b..3edf40da 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/exception/ApiException.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/exception/ApiException.java @@ -2,25 +2,43 @@ package com.gitee.sop.gatewaycommon.exception; import com.gitee.sop.gatewaycommon.message.Error; +import com.gitee.sop.gatewaycommon.message.ErrorFactory; +import com.gitee.sop.gatewaycommon.message.ErrorMeta; + +import java.util.Locale; /** * @author tanghc */ public class ApiException extends RuntimeException { - private transient final Error error; + private transient Error error; - public ApiException(Throwable cause, Error error) { - super(cause); - this.error = error; + private transient ErrorMeta errorMeta; + private transient Object[] params; + + public ApiException(ErrorMeta errorMeta, Object... params) { + this.errorMeta = errorMeta; + this.params = params; } - public ApiException(Error error) { - super(error.toString()); - this.error = error; + public ApiException(Throwable cause, ErrorMeta errorMeta, Object... params) { + super(cause); + this.errorMeta = errorMeta; + this.params = params; } - public Error getError() { + public Error getError(Locale locale) { + if (error == null) { + error = ErrorFactory.getError(this.errorMeta, locale, params); + } return error; } + + @Override + public String getMessage() { + String message = super.getMessage(); + return message == null ? errorMeta.toString() : message; + } + } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java index 4c7e2a1e..740b6553 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LimitFilter.java @@ -7,7 +7,8 @@ import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; import com.gitee.sop.gatewaycommon.limit.LimitManager; import com.gitee.sop.gatewaycommon.limit.LimitType; import com.gitee.sop.gatewaycommon.manager.LimitConfigManager; -import com.gitee.sop.gatewaycommon.message.ErrorImpl; +import com.gitee.sop.gatewaycommon.message.ErrorEnum; +import com.gitee.sop.gatewaycommon.message.ErrorMeta; import com.gitee.sop.gatewaycommon.param.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; @@ -17,16 +18,18 @@ 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; /** + * spring cloud gateway限流过滤器 * @author tanghc */ @Slf4j public class LimitFilter implements GlobalFilter, Ordered { + private static final ErrorMeta LIMIT_ERROR_META = ErrorEnum.ISV_REQUEST_LIMIT.getErrorMeta(); + @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { ApiConfig apiConfig = ApiConfig.getInstance(); @@ -43,13 +46,14 @@ public class LimitFilter implements GlobalFilter, Ordered { if (configLimitDto.getLimitStatus() == ConfigLimitDto.LIMIT_STATUS_CLOSE) { return chain.filter(exchange); } - byte limitType = configLimitDto.getLimitType().byteValue(); + Byte limitType = configLimitDto.getLimitType(); LimitManager limitManager = ApiConfig.getInstance().getLimitManager(); // 如果是漏桶策略 if (limitType == LimitType.LEAKY_BUCKET.getType()) { boolean acquire = limitManager.acquire(configLimitDto); + // 被限流,返回错误信息 if (!acquire) { - throw new ApiException(new ErrorImpl(configLimitDto.getLimitCode(), configLimitDto.getLimitMsg())); + throw new ApiException(LIMIT_ERROR_META); } } else if (limitType == LimitType.TOKEN_BUCKET.getType()) { limitManager.acquireToken(configLimitDto); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java index 3c69cdda..f39fa3a1 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java @@ -16,6 +16,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.server.ServerWebExchange; import java.util.List; +import java.util.Locale; import java.util.Map; @@ -53,14 +54,20 @@ public class GatewayResultExecutor extends BaseExecutorAdapter beanInitializerMap = applicationContext.getBeansOfType(BeanInitializer.class); + beanInitializerMap.values().forEach(BeanInitializer::load); } protected void doAfter() { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java index 0af913b8..77bd78fc 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultLimitConfigManager.java @@ -25,6 +25,11 @@ public class DefaultLimitConfigManager implements LimitConfigManager { protected static Map> idKeyMap = new HashMap<>(); + @Override + public void load(String serviceId) { + + } + @Override public void update(ConfigLimitDto configLimitDto) { Long id = configLimitDto.getId(); @@ -119,8 +124,4 @@ public class DefaultLimitConfigManager implements LimitConfigManager { return limitCache.get(limitKey); } - @Override - public void load() { - - } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultRouteConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultRouteConfigManager.java index 0ee215cc..60bf6e52 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultRouteConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultRouteConfigManager.java @@ -24,7 +24,7 @@ public class DefaultRouteConfigManager implements RouteConfigManager { } @Override - public void load() { + public void load(String serviceId) { } @@ -33,6 +33,14 @@ public class DefaultRouteConfigManager implements RouteConfigManager { this.doUpdate(routeConfig.getRouteId(), routeConfig); } + protected void save(RouteConfig routeConfig) { + Byte status = routeConfig.getStatus(); + if (status == null) { + routeConfig.setStatus(RouteStatus.ENABLE.getStatus()); + } + routeConfigMap.put(routeConfig.getRouteId(), routeConfig); + } + protected void doUpdate(String routeId, Object res) { RouteConfig routeConfig = routeConfigMap.get(routeId); if (routeConfig == null) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/LimitConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/LimitConfigManager.java index 6f224db6..9f348b57 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/LimitConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/LimitConfigManager.java @@ -1,12 +1,12 @@ package com.gitee.sop.gatewaycommon.manager; -import com.gitee.sop.gatewaycommon.bean.BeanInitializer; import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto; +import com.gitee.sop.gatewaycommon.bean.ServiceBeanInitializer; /** * @author tanghc */ -public interface LimitConfigManager extends BeanInitializer { +public interface LimitConfigManager extends ServiceBeanInitializer { /** * 更新限流配置 * @param configLimitDto 路由配置 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteConfigManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteConfigManager.java index b7c022ce..229f3b15 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteConfigManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteConfigManager.java @@ -1,13 +1,13 @@ package com.gitee.sop.gatewaycommon.manager; -import com.gitee.sop.gatewaycommon.bean.BeanInitializer; import com.gitee.sop.gatewaycommon.bean.RouteConfig; +import com.gitee.sop.gatewaycommon.bean.ServiceBeanInitializer; /** * 路由配置管理 * @author tanghc */ -public interface RouteConfigManager extends BeanInitializer { +public interface RouteConfigManager extends ServiceBeanInitializer { /** * 更新路由配置 * @param routeConfig 路由配置 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java index ec4eb090..d4289007 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java @@ -1,7 +1,6 @@ package com.gitee.sop.gatewaycommon.manager; import com.gitee.sop.gatewaycommon.bean.TargetRoute; -import com.gitee.sop.gatewaycommon.message.ErrorEnum; /** * @author tanghc @@ -22,17 +21,13 @@ public class RouteRepositoryContext { } /** - * 检查路由是否存在,不存在报错 + * 根据路由id查询路由 + * * @param routeId 路由id - * @param errorEnum 报错信息 - * @return 返回TargetRoute + * @return 返回路由信息,没有返回null */ - public static TargetRoute checkExist(String routeId, ErrorEnum errorEnum) { - TargetRoute targetRoute = routeRepository.get(routeId); - if (targetRoute == null) { - throw errorEnum.getErrorMeta().getException(); - } - return targetRoute; + public static TargetRoute getTargetRoute(String routeId) { + return routeRepository.get(routeId); } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorEnum.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorEnum.java index a4740940..9676d547 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorEnum.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorEnum.java @@ -9,10 +9,11 @@ public enum ErrorEnum { SUCCESS(Codes.CODE_SUCCESS, ""), /** 服务暂不可用 */ - ISP_UNKNOWN_ERROR(Codes.CODE_UNKNOW, "isp.unknown-error"), + ISP_UNKNOWN_ERROR(Codes.CODE_UNKNOWN, "isp.unknown-error"), /** 服务不可用,路由被禁用 */ - ISP_API_DISABLED(Codes.CODE_UNKNOW, "isp.service-not-available"), - + ISP_API_DISABLED(Codes.CODE_UNKNOWN, "isp.service-not-available"), + /** 限流处理 */ + ISV_REQUEST_LIMIT(Codes.CODE_UNKNOWN, "isv.service-busy"), /** 无效的访问令牌 */ AOP_INVALID_AUTH_TOKEN(Codes.CODE_AUTH, "aop.invalid-auth-token"), @@ -119,7 +120,7 @@ public enum ErrorEnum { public static final String CODE_INVALID = "40002"; public static final String CODE_BIZ = "40004"; public static final String CODE_ISV_PERM = "40006"; - public static final String CODE_UNKNOW = "20000"; + public static final String CODE_UNKNOWN = "20000"; } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorFactory.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorFactory.java index 6e15592c..98d33f98 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorFactory.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorFactory.java @@ -67,6 +67,9 @@ public class ErrorFactory { * @return 如果没有配置国际化消息,则直接返回errorMeta中的信息 */ public static Error getError(ErrorMeta errorMeta, Locale locale, Object... params) { + if (locale == null) { + locale = Locale.SIMPLIFIED_CHINESE; + } String key = errorMeta.getModulePrefix() + errorMeta.getCode() + errorMeta.getSubCode() + locale.toString(); Error error = errorCache.get(key); if (error == null) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorImpl.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorImpl.java index e1eb6838..30823cf5 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorImpl.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorImpl.java @@ -7,8 +7,6 @@ import lombok.Data; */ @Data public class ErrorImpl implements Error { - public static final String ISP_SERVICE_NOT_AVAILABLE = "isp.service-not-available"; - public static final String SERVICE_NOT_AVAILABLE = "service not available"; private String code; private String msg; @@ -16,13 +14,6 @@ public class ErrorImpl implements Error { private String sub_msg; private String solution; - public ErrorImpl() { - } - - public ErrorImpl(String sub_code, String sub_msg) { - this(ISP_SERVICE_NOT_AVAILABLE, SERVICE_NOT_AVAILABLE, sub_code, sub_msg, null); - } - public ErrorImpl(String code, String msg, String sub_code, String sub_msg, String solution) { this.code = code; this.msg = msg; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorMeta.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorMeta.java index b9cb1ffe..6b3aff65 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorMeta.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/message/ErrorMeta.java @@ -1,11 +1,8 @@ package com.gitee.sop.gatewaycommon.message; -import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.exception.ApiException; -import com.netflix.zuul.context.RequestContext; import lombok.Getter; -import javax.servlet.http.HttpServletRequest; import java.util.Locale; /** @@ -28,8 +25,8 @@ public class ErrorMeta { this.subCode = subCode; } - public Error getError() { - return ErrorFactory.getError(this, ZH_CN); + public Error getError(Locale locale) { + return ErrorFactory.getError(this, locale); } /** @@ -39,31 +36,17 @@ public class ErrorMeta { * @return 返回exception */ public ApiException getException(Object... params) { - Locale locale = getLocale(); if (params != null && params.length == 1) { Object param = params[0]; if (param instanceof Throwable) { - Error error = ErrorFactory.getError(this, ZH_CN); - return new ApiException((Throwable) param, error); - } - if (param instanceof Locale) { - locale = (Locale) param; + return new ApiException((Throwable) param, this); } } - Error error = ErrorFactory.getError(this, locale, params); - return new ApiException(error); + return new ApiException(this, params); } - protected Locale getLocale() { - if (ApiConfig.getInstance().isUseGateway()) { - return ZH_CN; - } - RequestContext currentContext = RequestContext.getCurrentContext(); - if (currentContext == null) { - return ZH_CN; - } - HttpServletRequest request = currentContext.getRequest(); - return request == null ? ZH_CN : request.getLocale(); + @Override + public String toString() { + return modulePrefix + code + "_" + subCode; } - } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java index 7361b758..c620c0a6 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java @@ -27,6 +27,7 @@ import org.springframework.http.HttpStatus; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import java.util.Locale; import java.util.Map; import java.util.Optional; @@ -72,6 +73,13 @@ public abstract class BaseExecutorAdapter implements ResultExecutor */ public abstract ApiParam getApiParam(T t); + /** + * 获取locale + * @param t request + * @return 返回locale + */ + protected abstract Locale getLocale(T t); + @Override public String mergeResult(T request, String serviceResult) { boolean isMergeResult = this.isMergeResult(request); @@ -85,17 +93,17 @@ public abstract class BaseExecutorAdapter implements ResultExecutor // 200正常返回 responseData = JSON.parseObject(serviceResult); responseData.put(GATEWAY_CODE_NAME, SUCCESS_META.getCode()); - responseData.put(GATEWAY_MSG_NAME, SUCCESS_META.getError().getMsg()); + responseData.put(GATEWAY_MSG_NAME, SUCCESS_META.getError(getLocale(request)).getMsg()); } else if (responseStatus == SopConstants.BIZ_ERROR_STATUS) { // 如果是业务出错 this.storeError(request, ErrorType.BIZ); responseData = JSON.parseObject(serviceResult); responseData.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode()); - responseData.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg()); + responseData.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError(getLocale(request)).getMsg()); } else if (responseStatus == HttpStatus.NOT_FOUND.value()) { responseData = JSON.parseObject(serviceResult); responseData.put(GATEWAY_CODE_NAME, ISV_MISSING_METHOD_META.getCode()); - responseData.put(GATEWAY_MSG_NAME, ISV_MISSING_METHOD_META.getError().getCode()); + responseData.put(GATEWAY_MSG_NAME, ISV_MISSING_METHOD_META.getError(getLocale(request)).getCode()); } else { ApiParam params = this.getApiParam(request); log.error("微服务端报错,params:{}, 微服务返回结果:{}", params, serviceResult); @@ -104,7 +112,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor // {"path":"/book/getBook3","error":"Internal Server Error","message":"id不能为空","timestamp":"2019-02-13T07:41:00.495+0000","status":500} responseData = new JSONObject(); responseData.put(GATEWAY_CODE_NAME, ISP_UNKNOW_ERROR_META.getCode()); - responseData.put(GATEWAY_MSG_NAME, ISP_UNKNOW_ERROR_META.getError().getMsg()); + responseData.put(GATEWAY_MSG_NAME, ISP_UNKNOW_ERROR_META.getError(getLocale(request)).getMsg()); } return this.merge(request, responseData); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java index bfca1c63..7c5a984e 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java @@ -115,7 +115,10 @@ public class ApiValidator implements Validator { } String routeId = param.fetchNameVersion(); // 检查路由是否存在 - TargetRoute targetRoute = RouteRepositoryContext.checkExist(routeId, ErrorEnum.ISV_INVALID_METHOD); + TargetRoute targetRoute = RouteRepositoryContext.getTargetRoute(routeId); + if (targetRoute == null) { + throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); + } // 检查路由是否启用 RouteConfig routeConfig = routeConfigManager.get(routeId); if (!routeConfig.enable()) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreLimitFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreLimitFilter.java index 319a5e6a..d00922ee 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreLimitFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreLimitFilter.java @@ -6,7 +6,8 @@ import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.limit.LimitManager; import com.gitee.sop.gatewaycommon.limit.LimitType; import com.gitee.sop.gatewaycommon.manager.LimitConfigManager; -import com.gitee.sop.gatewaycommon.message.ErrorImpl; +import com.gitee.sop.gatewaycommon.message.ErrorEnum; +import com.gitee.sop.gatewaycommon.message.ErrorMeta; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.util.RequestUtil; import com.gitee.sop.gatewaycommon.zuul.ZuulContext; @@ -16,17 +17,18 @@ import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.List; /** - * 限流拦截器 + * zuul限流过滤器 * * @author tanghc */ public class PreLimitFilter extends BaseZuulFilter { + private static final ErrorMeta LIMIT_ERROR_META = ErrorEnum.ISV_REQUEST_LIMIT.getErrorMeta(); + @Autowired private LimitManager limitManager; @@ -63,8 +65,9 @@ public class PreLimitFilter extends BaseZuulFilter { // 如果是漏桶策略 if (limitType == LimitType.LEAKY_BUCKET.getType()) { boolean acquire = limitManager.acquire(configLimitDto); + // 被限流,返回错误信息 if (!acquire) { - throw new ApiException(new ErrorImpl(configLimitDto.getLimitCode(), configLimitDto.getLimitMsg())); + throw new ApiException(LIMIT_ERROR_META); } } else if (limitType == LimitType.TOKEN_BUCKET.getType()) { limitManager.acquireToken(configLimitDto); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/result/ZuulResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/result/ZuulResultExecutor.java index ad4de22d..297767aa 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/result/ZuulResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/result/ZuulResultExecutor.java @@ -16,6 +16,7 @@ import com.netflix.zuul.exception.ZuulException; import lombok.extern.slf4j.Slf4j; import java.util.List; +import java.util.Locale; /** * @author tanghc @@ -68,27 +69,33 @@ public class ZuulResultExecutor extends BaseExecutorAdapter com.gitee.sop sop-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT ../pom.xml sop-service-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT jar sop-service-common diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index ffd1adc7..b3528fe3 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 8140f57a..366d699f 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-service-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 7b7c6ba6..21281c57 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT com.gitee.sop diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 2b18e01e..2b7a097c 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -34,7 +34,7 @@ com.gitee.sop sop-bridge-gateway - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbLimitConfigManager.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbLimitConfigManager.java index 20645d62..ed5393d2 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbLimitConfigManager.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbLimitConfigManager.java @@ -7,6 +7,7 @@ import com.gitee.sop.gatewaycommon.bean.ConfigLimitDto; import com.gitee.sop.gatewaycommon.manager.DefaultLimitConfigManager; import com.gitee.sop.gatewaycommon.util.MyBeanUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; @@ -26,8 +27,11 @@ public class DbLimitConfigManager extends DefaultLimitConfigManager { Environment environment; @Override - public void load() { + public void load(String serviceId) { Query query = new Query(); + if (StringUtils.isNotBlank(serviceId)) { + query.eq("service_id", serviceId); + } configLimitMapper.list(query) .forEach(this::putVal); @@ -45,7 +49,7 @@ public class DbLimitConfigManager extends DefaultLimitConfigManager { switch (channelMsg.getOperation()) { case "reload": log.info("重新加载限流配置信息,configLimitDto:{}", configLimitDto); - load(); + load(configLimitDto.getServiceId()); break; case "update": log.info("更新限流配置信息,configLimitDto:{}", configLimitDto); diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRouteConfigManager.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRouteConfigManager.java index de798b1f..9f87fdd9 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRouteConfigManager.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRouteConfigManager.java @@ -1,20 +1,18 @@ package com.gitee.sop.gateway.manager; -import com.gitee.fastmybatis.core.query.Query; import com.gitee.sop.gateway.mapper.ConfigRouteBaseMapper; import com.gitee.sop.gateway.mapper.ConfigRouteLimitMapper; +import com.gitee.sop.gateway.mapper.ConfigRouteMapper; import com.gitee.sop.gatewaycommon.bean.ChannelMsg; import com.gitee.sop.gatewaycommon.bean.RouteConfig; -import com.gitee.sop.gatewaycommon.bean.RouteDefinition; -import com.gitee.sop.gatewaycommon.bean.TargetRoute; import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager; -import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; -import java.util.Collection; +import java.util.List; /** * @author tanghc @@ -29,38 +27,17 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager { @Autowired ConfigRouteLimitMapper configRouteLimitMapper; + @Autowired + ConfigRouteMapper configRouteMapper; + @Autowired Environment environment; @Override - public void load() { - loadAllRoute(); - - Query query = new Query(); - configRouteBaseMapper.list(query) - .forEach(configRouteBase -> { - String key = configRouteBase.getRouteId(); - putVal(key, configRouteBase); - }); - } - - protected void loadAllRoute() { - Collection targetRoutes = RouteRepositoryContext.getRouteRepository().getAll(); - targetRoutes.forEach(targetRoute -> { - RouteDefinition routeDefinition = targetRoute.getRouteDefinition(); - initRouteConfig(routeDefinition); - }); - } - - protected void initRouteConfig(RouteDefinition routeDefinition) { - String routeId = routeDefinition.getId(); - RouteConfig routeConfig = newRouteConfig(); - routeConfig.setRouteId(routeId); - routeConfigMap.put(routeId, routeConfig); - } - - protected void putVal(String routeId, Object object) { - this.doUpdate(routeId, object); + public void load(String serviceId) { + List routeConfigs = StringUtils.isBlank(serviceId) ? configRouteMapper.listAllRouteConfig() + : configRouteMapper.listRouteConfig(serviceId); + routeConfigs.forEach(this::save); } @Override @@ -69,7 +46,7 @@ public class DbRouteConfigManager extends DefaultRouteConfigManager { switch (channelMsg.getOperation()) { case "reload": log.info("重新加载路由配置信息,routeConfigDto:{}", routeConfig); - load(); + load(null); break; case "update": log.info("更新路由配置信息,routeConfigDto:{}", routeConfig); diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRoutesProcessor.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRoutesProcessor.java index 7dc34021..ca0a21be 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRoutesProcessor.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/manager/DbRoutesProcessor.java @@ -4,15 +4,19 @@ import com.alibaba.fastjson.JSON; import com.gitee.fastmybatis.core.query.Query; import com.gitee.sop.gateway.entity.ConfigServiceRoute; import com.gitee.sop.gateway.mapper.ConfigServiceRouteMapper; +import com.gitee.sop.gatewaycommon.bean.BeanInitializer; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; +import com.gitee.sop.gatewaycommon.bean.ServiceBeanInitializer; import com.gitee.sop.gatewaycommon.bean.ServiceRouteInfo; import com.gitee.sop.gatewaycommon.route.RoutesProcessor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -25,6 +29,9 @@ public class DbRoutesProcessor implements RoutesProcessor { @Autowired private ConfigServiceRouteMapper configServiceRouteMapper; + @Autowired + private ApplicationContext applicationContext; + @Override public void removeAllRoutes(String serviceId) { // 删除serviceId下所有的路由 @@ -65,6 +72,13 @@ public class DbRoutesProcessor implements RoutesProcessor { if (CollectionUtils.isNotEmpty(configServiceRoutes)) { // 批量保存 configServiceRouteMapper.saveBatch(configServiceRoutes); + // 后续处理操作 + this.initServiceBeanInitializer(serviceId); } } + + private void initServiceBeanInitializer(String serviceId) { + Map serviceBeanInitializerMap = applicationContext.getBeansOfType(ServiceBeanInitializer.class); + serviceBeanInitializerMap.values().forEach(serviceBeanInitializer -> serviceBeanInitializer.load(serviceId)); + } } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteBaseMapper.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteBaseMapper.java index dda4c5b3..0a7c0dde 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteBaseMapper.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteBaseMapper.java @@ -8,4 +8,5 @@ import com.gitee.sop.gateway.entity.ConfigRouteBase; * @author tanghc */ public interface ConfigRouteBaseMapper extends CrudMapper { + } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteMapper.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteMapper.java new file mode 100644 index 00000000..a7d3a6ea --- /dev/null +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/mapper/ConfigRouteMapper.java @@ -0,0 +1,27 @@ +package com.gitee.sop.gateway.mapper; + +import com.gitee.sop.gatewaycommon.bean.RouteConfig; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author tanghc + */ +@Mapper +public interface ConfigRouteMapper { + + @Select("SELECT t.id AS routeId, t2.status " + + "FROM config_service_route t " + + "LEFT JOIN config_route_base t2 ON t.id=t2.route_id " + + "WHERE t.service_id=#{serviceId}") + List listRouteConfig(@Param("serviceId") String serviceId); + + @Select("SELECT t.id AS routeId, t2.status " + + "FROM config_service_route t " + + "LEFT JOIN config_route_base t2 ON t.id=t2.route_id ") + List listAllRouteConfig(); + +} diff --git a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java index 985a2c15..37a6c0b1 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -289,11 +290,14 @@ public class AllInOneTest extends TestBase { public void run() { try { countDownLatch.await(); // 等在这里,执行countDownLatch.countDown();集体触发 + Map header = new HashMap<>(4); + header.put("Accept-Language", "en-US"); // 业务方法 Client.RequestBuilder requestBuilder = new Client.RequestBuilder() .method("alipay.story.get") .version("1.2") .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + //.header(header) .httpMethod(HttpTool.HTTPMethod.GET); client.execute(requestBuilder); @@ -321,6 +325,29 @@ public class AllInOneTest extends TestBase { client.execute(requestBuilder); } + /** + * 国际化测试,返回英文错误 + */ + public void testLanguage() { + // Accept-Language + Map header = new HashMap<>(4); + header.put("Accept-Language", "en-US"); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.story.get__") + .version("1.0") + .header(header) + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.GET) + .callback((requestInfo, responseData) -> { + System.out.println(responseData); + String node = requestInfo.getDataNode(); + JSONObject jsonObject = JSON.parseObject(responseData).getJSONObject(node); + Assert.assertEquals("Nonexistent method name", jsonObject.getString("sub_msg")); + }); + + client.execute(requestBuilder); + } + class BizContent extends HashMap { public BizContent add(String key, Object value) { this.put(key, value); diff --git a/sop-website/pom.xml b/sop-website/pom.xml index fcbaaef5..d2f16c5c 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -35,7 +35,7 @@ com.gitee.sop sop-gateway-common - 3.0.0-SNAPSHOT + 3.0.1-SNAPSHOT