代码优化

1.x
tanghc 6 years ago
parent a5eb49ab6e
commit 236726b023
  1. 2
      sop-admin/sop-admin-front/pages/service/serviceList.js
  2. 10
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java
  3. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java
  4. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java
  5. 28
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java
  6. 10
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java
  7. 22
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/result/ZuulResultExecutor.java
  8. 2
      sop-common/sop-service-common/pom.xml
  9. 5
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java
  10. 14
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/EasyopenServiceConfiguration.java
  11. 4
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingRequestCondition.java
  12. 1
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  13. 1
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/ServiceRouteInfo.java
  14. 6
      sop-test/src/test/java/com/gitee/sop/JSR303DemoTest.java

@ -24,7 +24,7 @@ lib.config({
cols: [[
{type: 'numbers'},
{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: 'serverPort', title: '端口号', width: 100},
{field: 'status', title: '服务状态', width: 100, templet: function (row) {

@ -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;

@ -34,9 +34,9 @@ public class JSR303DemoTest extends TestBase {
// 业务参数
Map<String, String> bizContent = new HashMap<>();
// 对应校验规则查看GoodsParam.java
bizContent.put("goods_name", "iphone6");
bizContent.put("goods_remark", "iphone6");
bizContent.put("goods_comment", "1");
// bizContent.put("goods_name", "iphone6");
// bizContent.put("goods_remark", "iphone6");
// bizContent.put("goods_comment", "1");
params.put("biz_content", JSON.toJSONString(bizContent));

Loading…
Cancel
Save