mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
Merge branch 'develop' into registry-nacos
This commit is contained in:
33
doc/docs/files/10111_动态修改请求参数.md
Normal file
33
doc/docs/files/10111_动态修改请求参数.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# 动态修改请求参数
|
||||
|
||||
自1.14.0开始,zuul网关支持动态修改请求参数。即在网关修改客户端传递过来的参数,然后发送到微服务端。
|
||||
|
||||
```
|
||||
客户端参数{"name": "jim"} --> zuul中修改为{"name": "Lucy"} --> 微服务端将收到{"name": "Lucy"}
|
||||
```
|
||||
|
||||
使用场景:客户端请求参数经过加密,在网关解密后,再次发送明文参数给微服务端
|
||||
|
||||
- 如何使用
|
||||
|
||||
在网关springboot启动函数中添加如下代码
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
ApiConfig.getInstance().setZuulParameterFormatter(requestParams -> {
|
||||
// 获取biz_content
|
||||
JSONObject jsonObject = requestParams.getJSONObject(ParamNames.BIZ_CONTENT_NAME);
|
||||
// 修改biz_content中的值
|
||||
jsonObject.put("name", "name修改了111");
|
||||
jsonObject.put("remark", "remark修改了222");
|
||||
// 重新设置biz_content
|
||||
requestParams.put(ParamNames.BIZ_CONTENT_NAME, jsonObject);
|
||||
});
|
||||
SpringApplication.run(SopGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
其中requestParams是客户端传递过来的参数,直接修改其中的值即可。
|
||||
|
||||
更多参考:com.gitee.sop.gatewaycommon.zuul.filter.PreParameterFormatterFilter.java
|
@@ -5,18 +5,18 @@ import com.gitee.sop.gatewaycommon.gateway.result.GatewayResult;
|
||||
import com.gitee.sop.gatewaycommon.gateway.result.GatewayResultExecutor;
|
||||
import com.gitee.sop.gatewaycommon.limit.DefaultLimitManager;
|
||||
import com.gitee.sop.gatewaycommon.limit.LimitManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultIPBlacklistManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultIsvRoutePermissionManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultLimitConfigManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultRouteConfigManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultServiceErrorManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.EnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.IPBlacklistManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.IsvRoutePermissionManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.LimitConfigManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.RouteConfigManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.EnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||
import com.gitee.sop.gatewaycommon.result.DataNameBuilder;
|
||||
import com.gitee.sop.gatewaycommon.result.DefaultDataNameBuilder;
|
||||
@@ -34,6 +34,7 @@ import com.gitee.sop.gatewaycommon.validate.Signer;
|
||||
import com.gitee.sop.gatewaycommon.validate.Validator;
|
||||
import com.gitee.sop.gatewaycommon.zuul.configuration.ZuulErrorController;
|
||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParamBuilder;
|
||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterFormatter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.result.ZuulResultExecutor;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import lombok.Data;
|
||||
@@ -149,6 +150,8 @@ public class ApiConfig {
|
||||
*/
|
||||
private ZuulErrorController zuulErrorController = new ZuulErrorController();
|
||||
|
||||
private ZuulParameterFormatter zuulParameterFormatter;
|
||||
|
||||
// -------- fields ---------
|
||||
|
||||
/**
|
||||
|
@@ -12,7 +12,9 @@ import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory;
|
||||
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
|
||||
import com.gitee.sop.gatewaycommon.manager.RouteManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
@@ -21,6 +23,7 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -54,6 +57,12 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
|
||||
return jsonExceptionHandler;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
ParamBuilder<ServerWebExchange> paramBuilder() {
|
||||
return ApiConfig.getInstance().getGatewayParamBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理返回结果
|
||||
*/
|
||||
|
@@ -40,7 +40,7 @@ public class DefaultEnvGrayManager implements EnvGrayManager {
|
||||
if (serviceId == null || nameVersion == null) {
|
||||
return null;
|
||||
}
|
||||
boolean opened = instanceIdServiceIdMap.values().contains(serviceId);
|
||||
boolean opened = instanceIdServiceIdMap.containsValue(serviceId);
|
||||
// 没有开启灰度
|
||||
if (!opened) {
|
||||
return null;
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package com.gitee.sop.gatewaycommon.manager;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 参数格式化
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
public interface ParameterFormatter<T extends Map<String, Object>> {
|
||||
|
||||
/**
|
||||
* 参数格式化,即动态修改请求参数
|
||||
*
|
||||
* @param requestParams 原始请求参数,在此基础上追加或修改参数
|
||||
*/
|
||||
void format(T requestParams);
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
package com.gitee.sop.gatewaycommon.zuul.configuration;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
|
||||
import com.gitee.sop.gatewaycommon.bean.ApiContext;
|
||||
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
|
||||
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
|
||||
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
|
||||
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.ErrorFilter;
|
||||
@@ -10,6 +12,7 @@ import com.gitee.sop.gatewaycommon.zuul.filter.FormBodyWrapperFilterExt;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PostResultFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PreHttpServletRequestWrapperFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PreLimitFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PreParameterFormatterFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PreValidateFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.PreVersionDecisionFilter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.filter.Servlet30WrapperFilterExt;
|
||||
@@ -44,6 +47,12 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
|
||||
return ApiConfig.getInstance().getZuulParamBuilder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
ParameterFormatter<JSONObject> preParamFilter(){
|
||||
return ApiConfig.getInstance().getZuulParameterFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由存储
|
||||
* @return
|
||||
@@ -104,6 +113,11 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
|
||||
return new PreValidateFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
PreParameterFormatterFilter preParameterFormatterFilter() {
|
||||
return new PreParameterFormatterFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启限流
|
||||
*/
|
||||
@@ -120,6 +134,8 @@ public class BaseZuulConfiguration extends AbstractConfiguration {
|
||||
return new PreVersionDecisionFilter();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 错误处理扩展
|
||||
*/
|
||||
|
@@ -23,11 +23,14 @@ public abstract class BaseZuulFilter extends ZuulFilter {
|
||||
/** 签名验证过滤 */
|
||||
public static final int PRE_VALIDATE_FILTER_ORDER = -1000;
|
||||
|
||||
/** 参数格式化过滤器 */
|
||||
public static final int PRE_PARAMETER_FORMATTER_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 1;
|
||||
|
||||
/** 权限验证过滤 */
|
||||
public static final int PRE_ROUTE_PERMISSION_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 1;
|
||||
public static final int PRE_ROUTE_PERMISSION_FILTER_ORDER = PRE_VALIDATE_FILTER_ORDER + 100;
|
||||
|
||||
/** 限流过滤 */
|
||||
public static final int PRE_LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 1;
|
||||
public static final int PRE_LIMIT_FILTER_ORDER = PRE_ROUTE_PERMISSION_FILTER_ORDER + 100;
|
||||
|
||||
private Integer filterOrder;
|
||||
|
||||
|
@@ -0,0 +1,42 @@
|
||||
package com.gitee.sop.gatewaycommon.zuul.filter;
|
||||
|
||||
import com.gitee.sop.gatewaycommon.param.ApiParam;
|
||||
import com.gitee.sop.gatewaycommon.param.ParamNames;
|
||||
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
|
||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterFormatter;
|
||||
import com.gitee.sop.gatewaycommon.zuul.param.ZuulParameterUtil;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* 参数格式化过滤器,动态修改参数,此过滤器放在前面校验后面
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
public class PreParameterFormatterFilter extends BaseZuulFilter {
|
||||
|
||||
@Autowired(required = false)
|
||||
private ZuulParameterFormatter zuulParameterFormatter;
|
||||
|
||||
@Override
|
||||
protected FilterType getFilterType() {
|
||||
return FilterType.PRE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getFilterOrder() {
|
||||
return PRE_PARAMETER_FORMATTER_FILTER_ORDER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doRun(RequestContext requestContext) throws ZuulException {
|
||||
ApiParam apiParam = ZuulContext.getApiParam();
|
||||
// 校验成功后进行参数转换
|
||||
if (zuulParameterFormatter != null) {
|
||||
ZuulParameterUtil.format(apiParam, zuulParameterFormatter::format);
|
||||
requestContext.addZuulRequestHeader(ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package com.gitee.sop.gatewaycommon.zuul.param;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.gatewaycommon.manager.ParameterFormatter;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public interface ZuulParameterFormatter extends ParameterFormatter<JSONObject> {
|
||||
}
|
@@ -0,0 +1,183 @@
|
||||
package com.gitee.sop.gatewaycommon.zuul.param;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
||||
import com.gitee.sop.gatewaycommon.util.RequestUtil;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.http.HttpServletRequestWrapper;
|
||||
import com.netflix.zuul.http.ServletInputStreamWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.cloud.netflix.zuul.util.RequestContentDataExtractor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.FormHttpMessageConverter;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* zuul参数工具
|
||||
* @author tanghc
|
||||
*/
|
||||
@Slf4j
|
||||
public class ZuulParameterUtil {
|
||||
|
||||
private static FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
|
||||
|
||||
/**
|
||||
* 格式化参数
|
||||
* @param apiParam 请求的参数
|
||||
* @param consumer 修改参数
|
||||
* @param <T> 参数类型
|
||||
*/
|
||||
public static <T extends Map<String, Object>> void format(T apiParam, Consumer<T> consumer) {
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
consumer.accept(apiParam);
|
||||
HttpServletRequest request = requestContext.getRequest();
|
||||
String contentType = request.getContentType();
|
||||
if (StringUtils.containsIgnoreCase(contentType, MediaType.APPLICATION_JSON_VALUE)) {
|
||||
String json = (apiParam instanceof JSONObject) ?
|
||||
((JSONObject) apiParam).toJSONString()
|
||||
: JSON.toJSONString(apiParam);
|
||||
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
|
||||
requestContext.setRequest(new ChangeParamsHttpServletRequestWrapper(request, bytes));
|
||||
} else if(StringUtils.containsIgnoreCase(contentType, MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
|
||||
List<String> list = new ArrayList<>(apiParam.size());
|
||||
try {
|
||||
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof Collection) {
|
||||
Collection collection = (Collection) value;
|
||||
for (Object el : collection) {
|
||||
list.add(key + "=" + URLEncoder.encode(String.valueOf(el), SopConstants.UTF8));
|
||||
}
|
||||
} else {
|
||||
list.add(key + "=" + URLEncoder.encode(String.valueOf(value), SopConstants.UTF8));
|
||||
}
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("字符集不支持", e);
|
||||
}
|
||||
String paramsStr = StringUtils.join(list, "&");
|
||||
byte[] data = paramsStr.getBytes(StandardCharsets.UTF_8);
|
||||
requestContext.setRequest(new ChangeParamsHttpServletRequestWrapper(request, data));
|
||||
} else if(RequestUtil.isMultipart(request)) {
|
||||
FormHttpOutputMessage outputMessage = new FormHttpOutputMessage();
|
||||
try {
|
||||
// 转成MultipartRequest
|
||||
if (!(request instanceof MultipartHttpServletRequest)) {
|
||||
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getServletContext());
|
||||
request = commonsMultipartResolver.resolveMultipart(request);
|
||||
}
|
||||
// 重写新的值
|
||||
MultiValueMap<String, Object> builder = RequestContentDataExtractor.extract(request);
|
||||
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof List) {
|
||||
builder.put(entry.getKey(), (List)value);
|
||||
} else {
|
||||
builder.put(entry.getKey(), Collections.singletonList(String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
MediaType mediaType = MediaType.valueOf(request.getContentType());
|
||||
// 将字段以及上传文件重写写入到流中
|
||||
formHttpMessageConverter.write(builder, mediaType, outputMessage);
|
||||
// 获取新的上传文件流
|
||||
byte[] data = outputMessage.getInput();
|
||||
|
||||
requestContext.setRequest(new ChangeParamsHttpServletRequestWrapper(request, data));
|
||||
// 必须要重新指定content-type,因为此时的boundary已经发生改变
|
||||
requestContext.getZuulRequestHeaders().put("content-type", outputMessage.getHeaders().getContentType().toString());
|
||||
} catch (Exception e) {
|
||||
log.error("修改上传文件请求参数失败, apiParam:{}", apiParam, e);
|
||||
}
|
||||
} else if(HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {
|
||||
Map<String, List<String>> newParams = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : apiParam.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof List) {
|
||||
List<String> valueList = ((List<?>) value).stream().map(String::valueOf).collect(Collectors.toList());
|
||||
newParams.put(entry.getKey(), valueList);
|
||||
} else {
|
||||
newParams.put(entry.getKey(), Collections.singletonList(String.valueOf(value)));
|
||||
}
|
||||
}
|
||||
requestContext.setRequestQueryParams(newParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class FormHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
private ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getBody() throws IOException {
|
||||
return this.output;
|
||||
}
|
||||
|
||||
public byte[] getInput() throws IOException {
|
||||
this.output.flush();
|
||||
return this.output.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ChangeParamsHttpServletRequestWrapper extends HttpServletRequestWrapper {
|
||||
private byte[] data;
|
||||
|
||||
public ChangeParamsHttpServletRequestWrapper(HttpServletRequest request, byte[] data) {
|
||||
super(request);
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
return new ServletInputStreamWrapper(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getContentData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getContentLength() {
|
||||
return data.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLengthLong() {
|
||||
return data.length;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,24 +1,16 @@
|
||||
package com.gitee.sop.registryapi.bean;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
public class ServiceInfo {
|
||||
/** 服务名称 */
|
||||
private String serviceId;
|
||||
/** 实例列表 */
|
||||
private List<ServiceInstance> instances = Collections.emptyList();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "服务名称: " + serviceId + ", 实例数:" + instances.size();
|
||||
}
|
||||
private List<ServiceInstance> instances;
|
||||
}
|
||||
|
@@ -18,6 +18,8 @@ import org.springframework.util.CollectionUtils;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -51,7 +53,9 @@ public class RegistryServiceNacos implements RegistryService {
|
||||
ServiceInfo serviceInfo = new ServiceInfo();
|
||||
serviceInfo.setServiceId(serviceName);
|
||||
List<Instance> instanceList = namingService.getAllInstances(serviceName);
|
||||
if (!CollectionUtils.isEmpty(instanceList)) {
|
||||
if (CollectionUtils.isEmpty(instanceList)) {
|
||||
serviceInfo.setInstances(Collections.emptyList());
|
||||
} else {
|
||||
serviceInfo.setInstances(new ArrayList<>(instanceList.size()));
|
||||
for (Instance instance : instanceList) {
|
||||
ServiceInstance serviceInstance = new ServiceInstance();
|
||||
@@ -68,6 +72,7 @@ public class RegistryServiceNacos implements RegistryService {
|
||||
}
|
||||
serviceInfoList.add(serviceInfo);
|
||||
}
|
||||
serviceInfoList.sort(Comparator.comparingInt(o -> o.getInstances().size()));
|
||||
return serviceInfoList;
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.servercommon.bean.ServiceApiInfo;
|
||||
import com.gitee.sop.servercommon.bean.ZookeeperTool;
|
||||
import com.gitee.sop.servercommon.exception.ZookeeperPathNotExistException;
|
||||
import com.gitee.sop.servercommon.route.GatewayPredicateDefinition;
|
||||
import com.gitee.sop.servercommon.route.GatewayRouteDefinition;
|
||||
import com.gitee.sop.servercommon.route.ServiceRouteInfo;
|
||||
import lombok.Getter;
|
||||
@@ -17,6 +18,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -39,6 +41,15 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
||||
|
||||
private static final String DEFAULT_CONTEXT_PATH = "/";
|
||||
|
||||
/**
|
||||
* NameVersion=alipay.story.get1.0
|
||||
* see com.gitee.sop.gatewaycommon.routeDefinition.NameVersionRoutePredicateFactory
|
||||
*/
|
||||
private static String QUERY_PREDICATE_DEFINITION_TPL = "NameVersion=%s";
|
||||
|
||||
private static ServiceApiInfo.ApiMeta FIRST_API_META = new ServiceApiInfo.ApiMeta("_first.route_", "/", "v_000");
|
||||
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private ZookeeperTool zookeeperTool;
|
||||
@@ -94,6 +105,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
||||
protected ServiceRouteInfo buildServiceGatewayInfo(ServiceApiInfo serviceApiInfo) {
|
||||
List<ServiceApiInfo.ApiMeta> apis = serviceApiInfo.getApis();
|
||||
List<GatewayRouteDefinition> routeDefinitionList = new ArrayList<>(apis.size());
|
||||
routeDefinitionList.add(this.buildReadBodyRouteDefinition(serviceApiInfo));
|
||||
for (ServiceApiInfo.ApiMeta apiMeta : apis) {
|
||||
GatewayRouteDefinition gatewayRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, apiMeta);
|
||||
routeDefinitionList.add(gatewayRouteDefinition);
|
||||
@@ -136,6 +148,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
||||
BeanUtils.copyProperties(apiMeta, gatewayRouteDefinition);
|
||||
gatewayRouteDefinition.setId(routeId);
|
||||
gatewayRouteDefinition.setFilters(Collections.emptyList());
|
||||
gatewayRouteDefinition.setPredicates(this.buildPredicates(apiMeta));
|
||||
String uri = this.buildUri(serviceApiInfo, apiMeta);
|
||||
String path = this.buildServletPath(serviceApiInfo, apiMeta);
|
||||
gatewayRouteDefinition.setUri(uri);
|
||||
@@ -143,6 +156,30 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
||||
return gatewayRouteDefinition;
|
||||
}
|
||||
|
||||
protected List<GatewayPredicateDefinition> buildPredicates(ServiceApiInfo.ApiMeta apiMeta) {
|
||||
GatewayPredicateDefinition gatewayPredicateDefinition = new GatewayPredicateDefinition();
|
||||
gatewayPredicateDefinition.setName("ReadBody");
|
||||
return Arrays.asList(gatewayPredicateDefinition, this.buildNameVersionPredicateDefinition(apiMeta));
|
||||
}
|
||||
|
||||
protected GatewayPredicateDefinition buildNameVersionPredicateDefinition(ServiceApiInfo.ApiMeta apiMeta) {
|
||||
return new GatewayPredicateDefinition(String.format(QUERY_PREDICATE_DEFINITION_TPL, apiMeta.fetchNameVersion()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加com.gitee.sop.gatewaycommon.routeDefinition.ReadBodyRoutePredicateFactory,解决form表单获取不到问题
|
||||
*
|
||||
* @return 返回路由定义
|
||||
*/
|
||||
protected GatewayRouteDefinition buildReadBodyRouteDefinition(ServiceApiInfo serviceApiInfo) {
|
||||
GatewayRouteDefinition readBodyRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, FIRST_API_META);
|
||||
readBodyRouteDefinition.setOrder(Integer.MIN_VALUE);
|
||||
|
||||
readBodyRouteDefinition.setPredicates(this.buildPredicates(FIRST_API_META));
|
||||
|
||||
return readBodyRouteDefinition;
|
||||
}
|
||||
|
||||
protected String buildUri(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) {
|
||||
return PROTOCOL_LOAD_BALANCE + serviceApiInfo.getServiceId();
|
||||
}
|
||||
|
@@ -29,6 +29,10 @@ public abstract class SwaggerSupport {
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return getDocket();
|
||||
}
|
||||
|
||||
protected Docket getDocket() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
|
@@ -67,12 +67,8 @@ public class OpenUtil {
|
||||
for (Map.Entry<String, String[]> entry : entrySet) {
|
||||
String name = entry.getKey();
|
||||
String[] values = entry.getValue();
|
||||
if (values.length == 1) {
|
||||
if (values.length >= 1) {
|
||||
retMap.put(name, values[0]);
|
||||
} else if (values.length > 1) {
|
||||
retMap.put(name, values);
|
||||
} else {
|
||||
retMap.put(name, "");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -79,22 +79,11 @@
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.9.2</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-models</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-models</artifactId>
|
||||
<version>1.5.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.9.2</version>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>swagger-bootstrap-ui</artifactId>
|
||||
<version>1.9.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
@@ -22,10 +22,11 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
|
||||
@Override
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
super.addResourceHandlers(registry);
|
||||
registry.addResourceHandler("swagger-ui.html")
|
||||
.addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**")
|
||||
.addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
// 支持swagger-bootstrap-ui首页
|
||||
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
// 支持默认swagger
|
||||
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
|
||||
|
||||
@@ -43,9 +44,10 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
|
||||
|
||||
@Override
|
||||
protected boolean swaggerAccessProtected() {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -127,13 +127,26 @@ public class AlipayController {
|
||||
|
||||
// http://localhost:2222/alipay.story.get/
|
||||
@ApiMapping(value = "alipay.story.get")
|
||||
public Story getStory() {
|
||||
public Story getStory(Story param) {
|
||||
Story story = new Story();
|
||||
story.setId(1);
|
||||
story.setName("海底小纵队(alipay.story.get1.0)");
|
||||
story.setName("海底小纵队(alipay.story.get1.0), param:" + param);
|
||||
return story;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param param 对应biz_content中的内容,并自动JSR-303校验
|
||||
* @return
|
||||
*/
|
||||
@ApiMapping(value = "alipay.story.get", version = "1.2")
|
||||
public Story getStory11(Story param) {
|
||||
Story story2 = new Story();
|
||||
story2.setId(1);
|
||||
story2.setName("海底小纵队(alipay.story.get1.2), param:" + param);
|
||||
return story2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数绑定
|
||||
* @param story 对应biz_content中的内容,并自动JSR-303校验
|
||||
@@ -153,19 +166,6 @@ public class AlipayController {
|
||||
return storyVO;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param story 对应biz_content中的内容,并自动JSR-303校验
|
||||
* @return
|
||||
*/
|
||||
@ApiMapping(value = "alipay.story.get", version = "1.2")
|
||||
public Story getStory11(Story story) {
|
||||
Story story2 = new Story();
|
||||
story2.setId(1);
|
||||
story2.setName("海底小纵队(alipay.story.get1.2)");
|
||||
return story2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示文档表格树
|
||||
* @param story
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.gitee.sop.sdk.client;
|
||||
|
||||
import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestMethod;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.CookieJar;
|
||||
@@ -118,7 +119,7 @@ public class OpenHttp {
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException {
|
||||
public String request(String url, Map<String, String> form, Map<String, String> header, RequestMethod method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
@@ -134,7 +135,7 @@ public class OpenHttp {
|
||||
}
|
||||
}
|
||||
|
||||
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, HTTPMethod method) {
|
||||
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, RequestMethod method) {
|
||||
switch (method) {
|
||||
case GET:
|
||||
return new Request.Builder()
|
||||
@@ -241,22 +242,4 @@ public class OpenHttp {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public enum HTTPMethod {
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
HEAD,
|
||||
DELETE;
|
||||
|
||||
private HTTPMethod() {
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return this.name();
|
||||
}
|
||||
|
||||
public static HTTPMethod fromValue(String v) {
|
||||
return valueOf(v.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -100,11 +100,9 @@ public class SdkTest extends TestCase {
|
||||
if (response.isSuccess()) {
|
||||
List<DemoFileUploadResponse.FileMeta> responseFiles = response.getFiles();
|
||||
System.out.println("您上传的文件信息:");
|
||||
responseFiles.stream().forEach(file->{
|
||||
System.out.println(file);
|
||||
});
|
||||
responseFiles.forEach(System.out::println);
|
||||
} else {
|
||||
System.out.println("errorCode:" + response.getCode() + ",errorMsg:" + response.getMsg());
|
||||
System.out.println(JSON.toJSONString(response));
|
||||
}
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
|
Reference in New Issue
Block a user