Commit e347e5b29e754113d2e27d1bef8d22f1e4fb3dfe

Authored by miaoguoxin
1 parent afc80a49

优化

Showing 20 changed files with 119 additions and 109 deletions
doc/xtrade-gateway说明.docx
No preview for this file type
src/main/java/com/diligrp/xtrade/gateway/api/ApiManager.java
... ... @@ -4,13 +4,8 @@ import com.diligrp.xtrade.gateway.config.property.AuthPathProperties;
4 4 import org.springframework.beans.factory.annotation.Autowired;
5 5 import org.springframework.http.server.PathContainer;
6 6 import org.springframework.stereotype.Component;
7   -import org.springframework.web.util.pattern.PathPattern;
8 7  
9   -import java.util.List;
10 8 import java.util.Optional;
11   -import java.util.function.Consumer;
12   -import java.util.function.Function;
13   -import java.util.function.Predicate;
14 9  
15 10 /**
16 11 * @Auther: miaoguoxin
... ...
src/main/java/com/diligrp/xtrade/gateway/application/TestAggregationApplication.java
... ... @@ -4,6 +4,8 @@ import com.diligrp.xtrade.gateway.common.annotation.DispatchMapping;
4 4 import com.diligrp.xtrade.gateway.domain.TestRequestDto;
5 5 import com.diligrp.xtrade.gateway.support.dispatch.DispatchContext;
6 6 import com.diligrp.xtrade.gateway.support.dispatch.RequestDispatcher;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.http.server.reactive.ServerHttpRequest;
7 9 import org.springframework.stereotype.Component;
8 10 import org.springframework.validation.annotation.Validated;
9 11  
... ... @@ -19,13 +21,16 @@ import javax.validation.groups.Default;
19 21 @DispatchMapping
20 22 public class TestAggregationApplication {
21 23  
  24 + @Autowired
  25 + private ServerHttpRequest serverHttpRequest;
  26 +
22 27 @DispatchMapping("/test")
23 28 public String test(DispatchContext<TestRequestDto> dispatchContext){
24 29 return "ffff";
25 30 }
26 31  
27 32 @DispatchMapping("test2")
28   - public void test2(@Validated(value = Default.class) DispatchContext<TestRequestDto> dispatchContext){
  33 + public void test2(@Validated DispatchContext<TestRequestDto> dispatchContext){
29 34  
30 35 }
31 36  
... ...
src/main/java/com/diligrp/xtrade/gateway/common/constant/GatewayAttrType.java
... ... @@ -16,9 +16,9 @@ public enum GatewayAttrType implements IEnumType {
16 16 /**
17 17 * 编码
18 18 */
19   - private int value;
  19 + private final int value;
20 20  
21   - private String desc;
  21 + private final String desc;
22 22  
23 23  
24 24 GatewayAttrType(int value, String desc) {
... ...
src/main/java/com/diligrp/xtrade/gateway/common/constant/GatewayConst.java
1 1 package com.diligrp.xtrade.gateway.common.constant;
2 2  
  3 +import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
  4 +
3 5 import java.util.concurrent.atomic.AtomicBoolean;
4 6  
5 7 /**
... ... @@ -15,7 +17,11 @@ public class GatewayConst {
15 17 /**缓存Api配置key*/
16 18 public static final String CACHE_API_OBJECT_KEY = "cachedApiConfigObject";
17 19  
18   - public static final int CACHE_BODY_FILTER_ORDER = -10;
19   -
20   - public static final int AUTH_FILTER_ORDER = -9;
  20 + /* 自定义的过滤器顺序都从100开始(除ResponseBodyRead),避免顺序问题*/
  21 + /**缓存请求体过滤器*/
  22 + public static final int CACHE_BODY_FILTER_ORDER = 100;
  23 + /** 权限过滤器 */
  24 + public static final int AUTH_FILTER_ORDER = 101;
  25 + /**分发器,需要保证在最后执行,并且在10000之前*/
  26 + public static final int DISPATCH_FACTORY_FILTER_ORDER = 150;
21 27 }
... ...
src/main/java/com/diligrp/xtrade/gateway/common/utils/PathUtils.java
... ... @@ -14,7 +14,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
14 14 * @Date: 2020/4/15 10:37
15 15 */
16 16 public class PathUtils {
17   - private static PathPatternParser pathPatternParser = new PathPatternParser();
  17 + private static final PathPatternParser PATH_PATTERN_PARSER = new PathPatternParser();
18 18  
19 19 /**
20 20 * 多个路径拼接成一个标准uri,ex: /test + gateway = /test/gateway
... ... @@ -95,7 +95,7 @@ public class PathUtils {
95 95 pathPatterns.clear();
96 96 }
97 97 for (String path : paths) {
98   - PathPattern pathPattern = pathPatternParser.parse(path);
  98 + PathPattern pathPattern = PATH_PATTERN_PARSER.parse(path);
99 99 pathPatterns.add(pathPattern);
100 100 }
101 101 return pathPatterns;
... ...
src/main/java/com/diligrp/xtrade/gateway/common/utils/ValidateUtils.java
... ... @@ -31,8 +31,8 @@ public class ValidateUtils {
31 31 validate(params, new Class[]{});
32 32 }
33 33  
34   - public static void validate(Object params, Class... groups) {
35   - Set<Class> groupSet = new HashSet<>();
  34 + public static void validate(Object params, Class<?>... groups) {
  35 + Set<Class<?>> groupSet = new HashSet<>();
36 36 groupSet.add(Default.class);
37 37 if (groups != null && groups.length > 0) {
38 38 groupSet.addAll(Arrays.asList(groups));
... ...
src/main/java/com/diligrp/xtrade/gateway/controller/TestController.java
1 1 package com.diligrp.xtrade.gateway.controller;
2 2  
  3 +import com.diligrp.xtrade.gateway.domain.TestRequestDto;
3 4 import com.diligrp.xtrade.shared.domain.Message;
  5 +import com.diligrp.xtrade.shared.util.DateUtils;
4 6 import com.google.common.collect.Lists;
5 7 import org.springframework.beans.factory.annotation.Autowired;
6 8 import org.springframework.beans.factory.annotation.Value;
... ... @@ -8,12 +10,18 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
8 10 import org.springframework.cloud.gateway.config.GatewayProperties;
9 11 import org.springframework.cloud.gateway.route.RouteDefinition;
10 12 import org.springframework.http.server.PathContainer;
  13 +import org.springframework.validation.annotation.Validated;
11 14 import org.springframework.web.bind.annotation.GetMapping;
  15 +import org.springframework.web.bind.annotation.PathVariable;
12 16 import org.springframework.web.bind.annotation.RequestMapping;
13 17 import org.springframework.web.bind.annotation.RestController;
14 18 import org.springframework.web.util.pattern.PathPattern;
15 19 import org.springframework.web.util.pattern.PathPatternParser;
16 20  
  21 +import javax.validation.constraints.Min;
  22 +import javax.validation.constraints.NotBlank;
  23 +import javax.validation.groups.Default;
  24 +import java.time.LocalDateTime;
17 25 import java.util.ArrayList;
18 26 import java.util.List;
19 27 import java.util.Optional;
... ... @@ -26,22 +34,21 @@ import java.util.Optional;
26 34 @RestController
27 35 @RequestMapping("/test")
28 36 @RefreshScope
  37 +@Validated
29 38 public class TestController {
30   - private static PathPatternParser pathPatternParser = new PathPatternParser();
31 39 @Autowired
32 40 private GatewayProperties gatewayProperties;
33 41 @Value("${ribbon.ServerListRefreshInterval}")
34 42 private String interval;
35 43  
36   - @GetMapping("/gateway")
37   - public Message<List<RouteDefinition>> test() {
  44 + @GetMapping("/gateway/{id}")
  45 + public Message test(@Min(value = 5,message = "id不能小于5") @PathVariable Integer id) {
38 46 List<RouteDefinition> routes = gatewayProperties.getRoutes();
39   - return Message.builder().success(routes);
40   - //throw new RuntimeException("gfdg");
  47 + return Message.builder().success();
41 48 }
42 49  
43 50 @GetMapping("/test_dynamic")
44   - public Message<String> test1() {
45   - return Message.builder().success(interval);
  51 + public Message<String> test1( TestRequestDto testRequestDto) {
  52 + throw new RuntimeException("fffff");
46 53 }
47 54 }
... ...
src/main/java/com/diligrp/xtrade/gateway/domain/TestRequestDto.java
1 1 package com.diligrp.xtrade.gateway.domain;
2 2  
  3 +import com.fasterxml.jackson.annotation.JsonFormat;
3 4 import lombok.Data;
4 5  
5 6 import javax.validation.constraints.NotBlank;
  7 +import javax.validation.groups.Default;
  8 +import java.time.LocalDateTime;
6 9  
7 10 /**
8 11 * @Auther: miaoguoxin
... ... @@ -12,5 +15,6 @@ import javax.validation.constraints.NotBlank;
12 15 public class TestRequestDto {
13 16 @NotBlank(message = "{request.username.not.blank}")
14 17 private String username;
15   -
  18 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  19 + private LocalDateTime localDateTime;
16 20 }
... ...
src/main/java/com/diligrp/xtrade/gateway/filters/factory/DispatchGatewayFilterFactory.java
1 1 package com.diligrp.xtrade.gateway.filters.factory;
2 2  
  3 +import com.diligrp.xtrade.gateway.common.constant.GatewayConst;
3 4 import com.diligrp.xtrade.gateway.common.utils.ResponseUtils;
4 5 import com.diligrp.xtrade.gateway.support.dispatch.RequestDispatcher;
5 6 import com.diligrp.xtrade.shared.domain.Message;
... ... @@ -7,12 +8,15 @@ import lombok.Data;
7 8 import org.reactivestreams.Publisher;
8 9 import org.springframework.beans.factory.annotation.Autowired;
9 10 import org.springframework.cloud.gateway.filter.GatewayFilter;
  11 +import org.springframework.cloud.gateway.filter.GatewayFilterChain;
10 12 import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
  13 +import org.springframework.core.Ordered;
11 14 import org.springframework.core.io.buffer.DataBuffer;
12 15 import org.springframework.http.HttpHeaders;
13 16 import org.springframework.http.MediaType;
14 17 import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
15 18 import org.springframework.stereotype.Component;
  19 +import org.springframework.web.server.ServerWebExchange;
16 20 import reactor.core.publisher.Mono;
17 21  
18 22 import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY;
... ... @@ -34,30 +38,35 @@ public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;D
34 38  
35 39 @Override
36 40 public GatewayFilter apply(Config config) {
37   - return (exchange, chain) -> {
  41 + return new DispatchGatewayFilter(requestDispatcher);
  42 + }
  43 +
  44 + private static class DispatchGatewayFilter implements GatewayFilter,Ordered{
  45 + private final RequestDispatcher requestDispatcher;
  46 +
  47 + public DispatchGatewayFilter(RequestDispatcher requestDispatcher) {
  48 + this.requestDispatcher = requestDispatcher;
  49 + }
  50 +
  51 + @Override
  52 + public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
38 53 String rawPath = exchange.getRequest().getURI().getRawPath();
39 54 String paramsJson = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY);
40   - Message message = requestDispatcher.executeMethod(rawPath, paramsJson,exchange);
  55 + Message message = requestDispatcher.executeMethod(rawPath, paramsJson, exchange);
41 56 //throw new RuntimeException("gggg");
42   - return ResponseUtils.writeResponse(
43   - new ServerHttpResponseDecorator(exchange.getResponse()) {
44   - @Override
45   - public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
46   - //这里需要设置响应content-Type,不然后面的过滤器拿不到这个值会报错
47   - exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE);
48   - String originalResponseContentType = exchange
49   - .getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);
50   - HttpHeaders httpHeaders = exchange.getResponse().getHeaders();
51   - httpHeaders.add(HttpHeaders.CONTENT_TYPE, originalResponseContentType);
52   - return super.writeWith(body);
53   - }
54   - }, message);
55   - };
  57 + exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE);
  58 + return ResponseUtils.writeResponse(exchange.getResponse(),message);
  59 + }
  60 +
  61 + @Override
  62 + public int getOrder() {
  63 + return GatewayConst.DISPATCH_FACTORY_FILTER_ORDER;
  64 + }
56 65 }
57 66  
  67 +
58 68 @Data
59 69 public static class Config {
60   - private String keyResolverName;
61 70 }
62 71  
63 72 }
... ...
src/main/java/com/diligrp/xtrade/gateway/filters/global/AuthGlobalFilter.java
... ... @@ -2,31 +2,24 @@ package com.diligrp.xtrade.gateway.filters.global;
2 2  
3 3 import com.diligrp.xtrade.gateway.api.ApiManager;
4 4 import com.diligrp.xtrade.gateway.common.utils.ResponseUtils;
5   -import com.diligrp.xtrade.shared.domain.Message;
6   -import com.diligrp.xtrade.shared.util.JsonUtils;
7 5 import lombok.extern.slf4j.Slf4j;
8 6 import org.springframework.beans.factory.annotation.Autowired;
9 7 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
10 8 import org.springframework.cloud.gateway.filter.GlobalFilter;
11 9 import org.springframework.core.Ordered;
12   -import org.springframework.core.io.buffer.PooledDataBuffer;
13   -import org.springframework.http.HttpStatus;
14 10 import org.springframework.http.MediaType;
15   -import org.springframework.http.server.reactive.ServerHttpRequest;
16 11 import org.springframework.http.server.reactive.ServerHttpResponse;
17 12 import org.springframework.stereotype.Component;
18 13 import org.springframework.web.server.ServerWebExchange;
19   -import reactor.core.Disposable;
20   -import reactor.core.publisher.Flux;
21 14 import reactor.core.publisher.Mono;
22 15  
23   -import java.nio.charset.Charset;
24   -import java.nio.charset.StandardCharsets;
25   -import java.util.Map;
  16 +import java.net.URI;
  17 +import java.util.LinkedHashSet;
  18 +import java.util.Optional;
26 19  
27 20 import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.AUTH_FILTER_ORDER;
28   -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY;
29   -import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR;
  21 +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
  22 +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR;
30 23  
31 24 /**
32 25 * @Auther: miaoguoxin
... ... @@ -38,17 +31,27 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.C
38 31 public class AuthGlobalFilter implements GlobalFilter, Ordered {
39 32 @Autowired
40 33 private ApiManager apiManager;
  34 +
41 35 @Override
42 36 public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
43   - String rawPath = exchange.getRequest().getURI().getRawPath();
  37 + String originalUri = getOriginalUri(exchange);
44 38 ServerHttpResponse response = exchange.getResponse();
45 39 //排除在外的Uri跳过权限
46   - if (apiManager.isExcludePathMatch(rawPath)){
47   - return chain.filter(exchange);
  40 + if (apiManager.isExcludePathMatch(originalUri)) {
  41 + // return chain.filter(exchange);
  42 + throw new RuntimeException("异常测试");
48 43 }
  44 + exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE);
49 45 return ResponseUtils.writeForbidden(response);
50 46 }
51 47  
  48 + private static String getOriginalUri(ServerWebExchange exchange) {
  49 + LinkedHashSet<URI> uris = exchange.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
  50 + return Optional.ofNullable(uris)
  51 + .flatMap(us -> us.stream().findFirst().map(URI::getRawPath))
  52 + .orElse(exchange.getRequest().getURI().getRawPath());
  53 + }
  54 +
52 55 @Override
53 56 public int getOrder() {
54 57 return AUTH_FILTER_ORDER;
... ...
src/main/java/com/diligrp/xtrade/gateway/filters/global/ResponseReadBodyGlobalFilter.java
... ... @@ -77,7 +77,7 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered {
77 77 HttpHeaders httpHeaders = new HttpHeaders();
78 78 httpHeaders.setContentType(originalResponseContentType);
79 79  
80   - ClientResponse clientResponse = this.prepareClientResponse( exchange.getResponse().getStatusCode(),body, httpHeaders);
  80 + ClientResponse clientResponse = this.prepareClientResponse(exchange.getResponse().getStatusCode(),body, httpHeaders);
81 81 Mono<String> bodyMono = clientResponse.bodyToMono(String.class);
82 82 return bodyMono.flatMap(respBody -> {
83 83 //打印返回响应日志
... ... @@ -100,9 +100,8 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered {
100 100  
101 101 private ClientResponse prepareClientResponse(HttpStatus httpStatus, Publisher<? extends DataBuffer> body,
102 102 HttpHeaders httpHeaders) {
103   - ClientResponse.Builder builder;
104   - builder = ClientResponse.create(httpStatus, messageReaders);
105   - return builder.headers(headers -> headers.putAll(httpHeaders))
  103 + return ClientResponse.create(httpStatus, messageReaders)
  104 + .headers(headers -> headers.putAll(httpHeaders))
106 105 .body(Flux.from(body)).build();
107 106 }
108 107  
... ...
src/main/java/com/diligrp/xtrade/gateway/filters/web/ReactiveContextWebFilter.java
1 1 package com.diligrp.xtrade.gateway.filters.web;
2 2  
3 3 import com.diligrp.xtrade.gateway.support.context.ReactiveExchangeContextHolder;
  4 +import lombok.extern.slf4j.Slf4j;
4 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
5 6 import org.springframework.http.server.reactive.ServerHttpRequest;
6 7 import org.springframework.stereotype.Component;
... ... @@ -10,6 +11,8 @@ import org.springframework.web.server.WebFilterChain;
10 11 import reactor.core.publisher.Mono;
11 12 import reactor.core.publisher.SignalType;
12 13  
  14 +import java.util.function.Consumer;
  15 +
13 16 /**
14 17 * @Auther: miaoguoxin
15 18 * @Date: 2020/4/16 09:17
... ... @@ -17,9 +20,11 @@ import reactor.core.publisher.SignalType;
17 20 */
18 21 @Component
19 22 @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
  23 +@Slf4j
20 24 public class ReactiveContextWebFilter implements WebFilter {
21 25 @Override
22 26 public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  27 + long l = System.currentTimeMillis();
23 28 ServerHttpRequest request = exchange.getRequest();
24 29 String path = request.getURI().getRawPath();
25 30 if (path.contains("favicon.ico")) {
... ... @@ -27,7 +32,8 @@ public class ReactiveContextWebFilter implements WebFilter {
27 32 }
28 33 ReactiveExchangeContextHolder.put(exchange);
29 34 return chain.filter(exchange).doFinally(signalType -> {
30   - ReactiveExchangeContextHolder.remove();
  35 + ReactiveExchangeContextHolder.remove();
  36 + log.info("消耗时间:{}",System.currentTimeMillis()-l);
31 37 });
32 38 }
33 39 }
... ...
src/main/java/com/diligrp/xtrade/gateway/route/GatewayResourcesLoader.java
... ... @@ -16,7 +16,7 @@ public class GatewayResourcesLoader implements ApplicationRunner {
16 16 private DynamicRouteLoaderIntf dynamicRouteLoader;
17 17  
18 18 @Override
19   - public void run(ApplicationArguments args) throws Exception {
  19 + public void run(ApplicationArguments args) {
20 20 dynamicRouteLoader.init();
21 21 }
22 22 }
... ...
src/main/java/com/diligrp/xtrade/gateway/route/impl/CloudRouteResourceLoader.java
... ... @@ -34,7 +34,7 @@ public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf {
34 34 /**标记当前gatewayproties的hash值,用于判断配置是否更新过*/
35 35 private static int gatewayPropertiesHash;
36 36 /**防止重复初始化的标记*/
37   - private static AtomicBoolean hasInit = new AtomicBoolean(false);
  37 + private static final AtomicBoolean HAS_INIT = new AtomicBoolean(false);
38 38  
39 39 private final static String GROUP = "DEFAULT_GROUP";
40 40 private final static String ROUTE_CONFIG_NAME = "route.properties";
... ... @@ -57,7 +57,7 @@ public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf {
57 57  
58 58 @Override
59 59 public void init() {
60   - if (hasInit.compareAndSet(false, true)) {
  60 + if (HAS_INIT.compareAndSet(false, true)) {
61 61 this.loadRoutes();
62 62 //用来判断routes是否改变过,避免无效刷新
63 63 gatewayPropertiesHash = gatewayProperties.getRoutes().hashCode();
... ...
src/main/java/com/diligrp/xtrade/gateway/support/context/ReactiveRequestBeanProcessor.java renamed to src/main/java/com/diligrp/xtrade/gateway/support/context/ReactiveWebContextBeanProcessor.java
... ... @@ -13,10 +13,9 @@ import java.io.Serializable;
13 13 /**
14 14 * @Auther: miaoguoxin
15 15 * @Date: 2020/4/16 09:13
16   - * @Description:
17 16 */
18 17 @Component
19   -public class ReactiveRequestBeanProcessor implements BeanFactoryPostProcessor {
  18 +public class ReactiveWebContextBeanProcessor implements BeanFactoryPostProcessor {
20 19  
21 20 @Override
22 21 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
... ...
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/RequestDispatcher.java
1 1 package com.diligrp.xtrade.gateway.support.dispatch;
2 2  
3   -import com.diligrp.xtrade.gateway.application.TestAggregationApplication;
4 3 import com.diligrp.xtrade.gateway.common.utils.PathUtils;
5 4 import com.diligrp.xtrade.gateway.common.utils.ValidateUtils;
6   -import com.diligrp.xtrade.gateway.domain.TestRequestDto;
7 5 import com.diligrp.xtrade.gateway.exception.GatewayParamNotValidException;
8 6 import com.diligrp.xtrade.gateway.exception.GatewayServiceException;
9 7 import com.diligrp.xtrade.gateway.filters.factory.DispatchGatewayFilterFactory;
10   -import com.diligrp.xtrade.gateway.route.DynamicRouteLoaderIntf;
11   -import com.diligrp.xtrade.gateway.route.impl.MysqlRouteResourceLoader;
12 8 import com.diligrp.xtrade.shared.domain.Message;
13   -import com.diligrp.xtrade.shared.type.SystemCode;
  9 +import com.diligrp.xtrade.shared.type.ErrorCode;
14 10 import com.diligrp.xtrade.shared.util.JsonUtils;
15 11 import com.google.common.base.Strings;
16 12 import lombok.extern.slf4j.Slf4j;
17   -import org.bouncycastle.util.Arrays;
18 13 import org.springframework.beans.factory.annotation.Autowired;
19 14 import org.springframework.cloud.gateway.support.NotFoundException;
20 15 import org.springframework.context.ApplicationContext;
... ... @@ -22,13 +17,10 @@ import org.springframework.stereotype.Component;
22 17 import org.springframework.validation.annotation.Validated;
23 18 import org.springframework.web.server.ResponseStatusException;
24 19 import org.springframework.web.server.ServerWebExchange;
25   -import org.springframework.web.server.ServerWebInputException;
26 20 import org.springframework.web.util.pattern.PathPattern;
27 21  
28 22 import java.lang.reflect.InvocationTargetException;
29 23 import java.lang.reflect.Method;
30   -import java.lang.reflect.ParameterizedType;
31   -import java.lang.reflect.Type;
32 24 import java.util.List;
33 25 import java.util.Map;
34 26 import java.util.Optional;
... ... @@ -38,7 +30,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
38 30  
39 31 /**
40 32 * @Auther: miaoguoxin
41   - * @Date: 2020/4/15 09:29
  33 + * @date: 2020/4/15 09:29
42 34 * @Description: 映射分发器, 用于聚合服务执行对应方法
43 35 * {@link DispatchGatewayFilterFactory}
44 36 */
... ... @@ -52,7 +44,7 @@ public class RequestDispatcher {
52 44  
53 45 private static final List<PathPattern> PATH_PATTERNS = new CopyOnWriteArrayList<>();
54 46  
55   - private static AtomicBoolean hasInit = new AtomicBoolean(false);
  47 + private static final AtomicBoolean HAS_INIT = new AtomicBoolean(false);
56 48  
57 49 @Autowired
58 50 private ApplicationContext applicationContext;
... ... @@ -63,9 +55,9 @@ public class RequestDispatcher {
63 55 * @author miaoguoxin
64 56 * @date 2020/4/15
65 57 */
66   - public Message executeMethod(String uri, String paramsJson, ServerWebExchange exchange) {
  58 + public Message<Object> executeMethod(String uri, String paramsJson, ServerWebExchange exchange) {
67 59 try {
68   - if (hasInit.compareAndSet(false, true)) {
  60 + if (HAS_INIT.compareAndSet(false, true)) {
69 61 String[] paths = MAPPING_INFO_MAP.keySet().toArray(new String[0]);
70 62 PATH_PATTERNS.addAll(PathUtils.assemblePath(paths, PATH_PATTERNS));
71 63 }
... ... @@ -75,7 +67,7 @@ public class RequestDispatcher {
75 67 }
76 68 //分发逻辑
77 69 Object resultData = this.beginDispatch(matchUri, paramsJson,exchange);
78   - return Message.builder().success(resultData);
  70 + return Message.success(resultData);
79 71 } catch (Throwable e) {
80 72 return handleException(e);
81 73 }
... ... @@ -83,8 +75,6 @@ public class RequestDispatcher {
83 75  
84 76 /**
85 77 * 注册mapping信息
86   - * @param
87   - * @return
88 78 * @author miaoguoxin
89 79 * @date 2020/4/16
90 80 */
... ... @@ -117,7 +107,7 @@ public class RequestDispatcher {
117 107 }
118 108 }
119 109  
120   - DispatchContext dispatchContext = new DispatchContext(
  110 + DispatchContext<Object> dispatchContext = new DispatchContext<>(
121 111 exchange.getRequest(),
122 112 exchange.getResponse(),
123 113 paramData);
... ... @@ -132,25 +122,18 @@ public class RequestDispatcher {
132 122 }
133 123 }
134 124  
135   - private static Message handleException(Throwable ex) {
  125 + private static Message<Object> handleException(Throwable ex) {
136 126 if (ex instanceof ResponseStatusException) {
137 127 int statusCode = ((ResponseStatusException) ex).getStatus().value();
138 128 log.error("aggregation dispatch {}:{}", statusCode, ex.getMessage());
139   - return Message.builder()
140   - .code(statusCode)
141   - .message(ex.getMessage())
142   - .build();
  129 + return Message.failure(statusCode,ex.getMessage());
143 130 } else if (ex instanceof GatewayParamNotValidException) {
144   - return Message.builder()
145   - .code(SystemCode.ILLEGAL_PARAMS.getCode())
146   - .message(ex.getMessage())
147   - .build();
  131 + return Message.failure(
  132 + ErrorCode.ILLEGAL_PARAMS.getCode(),ex.getMessage());
148 133 } else {
149 134 log.error("aggregation dispatch error:{}", ex);
150   - return Message.builder()
151   - .code(SystemCode.SERVER_ERROR.getCode())
152   - .message(SystemCode.SERVER_ERROR.getName())
153   - .build();
  135 + return Message.failure(
  136 + ErrorCode.UNKNOWN_ERROR.getCode(),ErrorCode.UNKNOWN_ERROR.getName());
154 137 }
155 138 }
156 139  
... ...
src/main/resources/bootstrap-dev.yml
... ... @@ -2,12 +2,12 @@ spring:
2 2 cloud:
3 3 nacos:
4 4 discovery:
5   - username: nacos
6   - password: microtest
7   - server-addr: apitest.51shiban.com:80/n
8   - namespace: c86d8673-4d0a-469f-8bb8-1434c57c236c
  5 + username: xtrade
  6 + password: abcd1234
  7 + server-addr: 10.28.1.79:8848
  8 + namespace: 35211f71-92a9-4f3e-975e-37b1132e05c8
9 9 config:
10   - username: nacos
11   - password: microtest
12   - server-addr: apitest.51shiban.com:80/n
13   - namespace: c86d8673-4d0a-469f-8bb8-1434c57c236c
  10 + username: xtrade
  11 + password: abcd1234
  12 + server-addr: 10.28.1.79:8848
  13 + namespace: 35211f71-92a9-4f3e-975e-37b1132e05c8
... ...
src/main/resources/bootstrap.yml
... ... @@ -21,7 +21,5 @@ mybatis:
21 21 mapper-locations: classpath:/mapper/*.xml
22 22 config-location: classpath:/mybatis-config.xml
23 23 xtrade:
24   - json:
25   - format: false
26 24 gateway-loader: cloud
27 25 aggregation-scan-packages: com.diligrp.xtrade.gateway.application
... ...
src/test/java/com/diligrp/xtrade/gateway/XtradeGatewayApplicationTests.java
... ... @@ -6,8 +6,4 @@ import org.springframework.boot.test.context.SpringBootTest;
6 6 @SpringBootTest
7 7 class XtradeGatewayApplicationTests {
8 8  
9   - @Test
10   - void contextLoads() {
11   - }
12   -
13 9 }
... ...