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