mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
代码优化
This commit is contained in:
@@ -105,13 +105,17 @@ public class BaseGatewayConfiguration {
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void after() {
|
||||
protected final void after() {
|
||||
if (RouteRepositoryContext.getRouteRepository() == null) {
|
||||
throw new IllegalArgumentException("RouteRepositoryContext.setRouteRepository()方法未使用");
|
||||
}
|
||||
initMessage();
|
||||
gatewayZookeeperApiMetaManager.refresh();
|
||||
doAfter();
|
||||
}
|
||||
|
||||
protected void doAfter() {
|
||||
initMessage();
|
||||
gatewayZookeeperApiMetaManager.refresh();
|
||||
|
||||
}
|
||||
|
||||
protected void initMessage() {
|
||||
|
@@ -26,7 +26,7 @@ import java.util.Map;
|
||||
public class GatewayResultExecutor extends BaseExecutorAdapter<ServerWebExchange, GatewayResult> {
|
||||
|
||||
@Override
|
||||
public int getBizHeaderCode(ServerWebExchange exchange) {
|
||||
public int getResponseStatus(ServerWebExchange exchange) {
|
||||
int responseStatus = HttpStatus.OK.value();
|
||||
List<String> errorCodeList = exchange.getResponse().getHeaders().get(SopConstants.X_BIZ_ERROR_CODE);
|
||||
if (!CollectionUtils.isEmpty(errorCodeList)) {
|
||||
|
@@ -7,7 +7,8 @@ import com.gitee.sop.gatewaycommon.bean.TargetRoute;
|
||||
*/
|
||||
public class RouteRepositoryContext {
|
||||
|
||||
private RouteRepositoryContext(){}
|
||||
private RouteRepositoryContext() {
|
||||
}
|
||||
|
||||
private static RouteRepository<? extends TargetRoute> routeRepository;
|
||||
|
||||
@@ -15,7 +16,7 @@ public class RouteRepositoryContext {
|
||||
return routeRepository;
|
||||
}
|
||||
|
||||
public static <T extends TargetRoute> void setRouteRepository(RouteRepository<T> routeRepository) {
|
||||
public static <T extends TargetRoute> void setRouteRepository(RouteRepository<T> routeRepository) {
|
||||
RouteRepositoryContext.routeRepository = routeRepository;
|
||||
}
|
||||
|
||||
|
@@ -38,7 +38,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
public abstract int getBizHeaderCode(T t);
|
||||
public abstract int getResponseStatus(T t);
|
||||
|
||||
/**
|
||||
* 返回Api参数
|
||||
@@ -54,7 +54,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
||||
return serviceResult;
|
||||
}
|
||||
serviceResult = wrapResult(serviceResult);
|
||||
int responseStatus = this.getBizHeaderCode(request);
|
||||
int responseStatus = this.getResponseStatus(request);
|
||||
JSONObject jsonObjectService;
|
||||
if (responseStatus == HttpStatus.OK.value()) {
|
||||
// 200正常返回
|
||||
@@ -88,6 +88,9 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
||||
return defaultSetting;
|
||||
}
|
||||
Map<String, ?> params = this.getApiParam(request);
|
||||
if (params == null) {
|
||||
return true;
|
||||
}
|
||||
Object name = params.get(ParamNames.API_NAME);
|
||||
Object version = params.get(ParamNames.VERSION_NAME);
|
||||
if(name == null) {
|
||||
@@ -119,17 +122,22 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
|
||||
|
||||
public String merge(T exchange, JSONObject jsonObjectService) {
|
||||
JSONObject ret = new JSONObject();
|
||||
String name = "error";
|
||||
String sign = "";
|
||||
Map<String, ?> params = this.getApiParam(exchange);
|
||||
Object name = params.get(ParamNames.API_NAME);
|
||||
if (name == null) {
|
||||
name = "error";
|
||||
}
|
||||
Object sign = params.get(ParamNames.SIGN_NAME);
|
||||
if (sign == null) {
|
||||
sign = "";
|
||||
if (params != null) {
|
||||
Object method = params.get(ParamNames.API_NAME);
|
||||
if (method != null) {
|
||||
name = String.valueOf(method);
|
||||
}
|
||||
Object _sign = params.get(ParamNames.SIGN_NAME);
|
||||
if (_sign != null) {
|
||||
sign = String.valueOf(_sign);
|
||||
}
|
||||
}
|
||||
|
||||
// 点换成下划线
|
||||
String method = String.valueOf(name).replace(DOT, UNDERLINE);
|
||||
String method = name.replace(DOT, UNDERLINE);
|
||||
ret.put(method + DATA_SUFFIX, jsonObjectService);
|
||||
ret.put(ParamNames.SIGN_NAME, sign);
|
||||
return ret.toJSONString();
|
||||
|
@@ -121,13 +121,17 @@ public class BaseZuulConfiguration {
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void after() {
|
||||
public final void after() {
|
||||
if (RouteRepositoryContext.getRouteRepository() == null) {
|
||||
throw new IllegalArgumentException("RouteRepositoryContext.setRouteRepository()方法未使用");
|
||||
}
|
||||
initMessage();
|
||||
apiMetaManager.refresh();
|
||||
doAfter();
|
||||
}
|
||||
|
||||
protected void doAfter() {
|
||||
initMessage();
|
||||
apiMetaManager.refresh();
|
||||
|
||||
}
|
||||
|
||||
protected void initMessage() {
|
||||
|
@@ -12,10 +12,7 @@ import com.netflix.util.Pair;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -27,24 +24,21 @@ import java.util.Optional;
|
||||
public class ZuulResultExecutor extends BaseExecutorAdapter<RequestContext, String> {
|
||||
|
||||
@Override
|
||||
public int getBizHeaderCode(RequestContext requestContext) {
|
||||
// 微服务端返回的head
|
||||
int code = HttpStatus.OK.value();
|
||||
public int getResponseStatus(RequestContext requestContext) {
|
||||
List<Pair<String, String>> bizHeaders = requestContext.getZuulResponseHeaders();
|
||||
Optional<Pair<String, String>> first = bizHeaders.stream()
|
||||
Optional<String> first = bizHeaders.stream()
|
||||
.filter(header -> {
|
||||
return SopConstants.X_BIZ_ERROR_CODE.equals(header.first());
|
||||
}).map(header -> {
|
||||
return header.second();
|
||||
}).findFirst();
|
||||
|
||||
Pair<String, String> header = first.orElseGet(() -> {
|
||||
return new Pair<String, String>(HttpStatus.OK.name(), String.valueOf(HttpStatus.OK.value()));
|
||||
String status = first.orElseGet(() -> {
|
||||
int respStatus = requestContext.getResponseStatusCode();
|
||||
return String.valueOf(respStatus);
|
||||
});
|
||||
|
||||
String bizErrorCode = header.second();
|
||||
if (bizErrorCode != null) {
|
||||
code = Integer.valueOf(bizErrorCode);
|
||||
}
|
||||
return code;
|
||||
return Integer.valueOf(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -31,7 +31,7 @@
|
||||
<dependency>
|
||||
<groupId>net.oschina.durcframework</groupId>
|
||||
<artifactId>easyopen</artifactId>
|
||||
<version>1.16.0</version>
|
||||
<version>1.16.1</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@@ -61,12 +61,13 @@ public class BaseServiceConfiguration extends WebMvcConfigurationSupport {
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void after() {
|
||||
public final void after() {
|
||||
initMessage();
|
||||
doAfter();
|
||||
}
|
||||
|
||||
protected void doAfter() {
|
||||
initMessage();
|
||||
|
||||
}
|
||||
|
||||
protected void initMessage() {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.gitee.sop.servercommon.configuration;
|
||||
|
||||
import com.gitee.easyopen.ApiContext;
|
||||
import com.gitee.easyopen.annotation.Api;
|
||||
import com.gitee.easyopen.util.ReflectionUtil;
|
||||
import com.gitee.sop.servercommon.bean.ServiceApiInfo;
|
||||
@@ -25,6 +26,10 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public class EasyopenServiceConfiguration extends BaseServiceConfiguration {
|
||||
|
||||
static {
|
||||
ApiContext.getApiConfig().setIgnoreValidate(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequestMappingEvent getRequestMappingEvent(ApiMetaManager apiMetaManager, Environment environment) {
|
||||
return new EasyopenRequestMappingEvent(apiMetaManager, environment);
|
||||
@@ -56,6 +61,8 @@ public class EasyopenServiceConfiguration extends BaseServiceConfiguration {
|
||||
apiMeta.setName(api.name());
|
||||
apiMeta.setVersion(api.version());
|
||||
apiMeta.setIgnoreValidate(BooleanUtils.toInteger(api.ignoreValidate()));
|
||||
// 对结果不合并
|
||||
apiMeta.setMergeResult(BooleanUtils.toInteger(false));
|
||||
// /api/goods.get/
|
||||
String servletPath = this.buildPath(api);
|
||||
apiMeta.setPath(servletPath);
|
||||
@@ -88,11 +95,4 @@ public class EasyopenServiceConfiguration extends BaseServiceConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void after() {
|
||||
super.after();
|
||||
// 取消验证
|
||||
// todo:需要在easyopen端修改
|
||||
//ApiContext.getApiConfig().setIgnoreValidate(true);
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package com.gitee.sop.servercommon.mapping;
|
||||
|
||||
import com.gitee.sop.servercommon.bean.ServiceConfig;
|
||||
import com.gitee.sop.servercommon.bean.ParamNames;
|
||||
import com.gitee.sop.servercommon.bean.ServiceConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.mvc.condition.RequestCondition;
|
||||
@@ -32,6 +32,7 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
|
||||
/**
|
||||
* 如果版本号跟当前对象中的版本号匹配,则表是命中的对应的方法
|
||||
* 否则返回null,表示不匹配
|
||||
*
|
||||
* @param request
|
||||
* @return 返回ApiMappingRequestCondition
|
||||
*/
|
||||
@@ -53,6 +54,7 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
|
||||
/**
|
||||
* 对两个RequestCondition对象进行比较,这里主要是如果存在两个注册的一样的Mapping,
|
||||
* 那么就会对这两个Mapping进行排序,以判断哪个Mapping更适合处理当前request请求
|
||||
*
|
||||
* @param other
|
||||
* @param request
|
||||
* @return 返回-1,0,1
|
||||
|
@@ -11,6 +11,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
/**
|
||||
* 解析request参数中的业务参数,隐射到方法参数上
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package com.gitee.sop.servercommon.route;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.gitee.easyopen.doc.annotation.ApiDocField;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
Reference in New Issue
Block a user