Commit 36d196a458eb29841067bd57ccc269f6e1229d0a
1 parent
48f40cbd
模块化
Showing
64 changed files
with
869 additions
and
387 deletions
gateway-business/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
5 | + <parent> | ||
6 | + <artifactId>xtrade-gateway-service</artifactId> | ||
7 | + <groupId>com.diligrp</groupId> | ||
8 | + <version>1.0.0</version> | ||
9 | + </parent> | ||
10 | + <modelVersion>4.0.0</modelVersion> | ||
11 | + | ||
12 | + <artifactId>gateway-business</artifactId> | ||
13 | + | ||
14 | + <dependencies> | ||
15 | + <dependency> | ||
16 | + <artifactId>gateway-core</artifactId> | ||
17 | + <groupId>com.diligrp</groupId> | ||
18 | + </dependency> | ||
19 | + | ||
20 | + </dependencies> | ||
21 | +</project> | ||
0 | \ No newline at end of file | 22 | \ No newline at end of file |
src/main/java/com/diligrp/xtrade/gateway/XtradeGatewayApplication.java renamed to gateway-business/src/main/java/com/diligrp/xtrade/business/XtradeGatewayApplication.java
1 | -package com.diligrp.xtrade.gateway; | 1 | +package com.diligrp.xtrade.business; |
2 | 2 | ||
3 | import org.mybatis.spring.annotation.MapperScan; | 3 | import org.mybatis.spring.annotation.MapperScan; |
4 | import org.springframework.boot.SpringApplication; | 4 | import org.springframework.boot.SpringApplication; |
@@ -6,10 +6,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | @@ -6,10 +6,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
7 | import org.springframework.cloud.context.config.annotation.RefreshScope; | 7 | import org.springframework.cloud.context.config.annotation.RefreshScope; |
8 | 8 | ||
9 | -@SpringBootApplication | ||
10 | -@EnableDiscoveryClient | 9 | + |
10 | + | ||
11 | @RefreshScope | 11 | @RefreshScope |
12 | -@MapperScan(basePackages = {"com.diligrp.xtrade.gateway.repository.dao"}) | 12 | +@EnableDiscoveryClient |
13 | +@MapperScan(basePackages = {"com.diligrp.xtrade.core.repository"}) | ||
14 | +@SpringBootApplication(scanBasePackages = {"com.diligrp.xtrade.core","com.diligrp.xtrade.business"}) | ||
13 | public class XtradeGatewayApplication { | 15 | public class XtradeGatewayApplication { |
14 | 16 | ||
15 | public static void main(String[] args) { | 17 | public static void main(String[] args) { |
@@ -17,3 +19,5 @@ public class XtradeGatewayApplication { | @@ -17,3 +19,5 @@ public class XtradeGatewayApplication { | ||
17 | } | 19 | } |
18 | 20 | ||
19 | } | 21 | } |
22 | + | ||
23 | + |
src/main/java/com/diligrp/xtrade/gateway/application/TestAggregationApplication.java renamed to gateway-business/src/main/java/com/diligrp/xtrade/business/application/TestAggregationApplication.java
1 | -package com.diligrp.xtrade.gateway.application; | 1 | +package com.diligrp.xtrade.business.application; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.common.annotation.DispatchMapping; | ||
4 | -import com.diligrp.xtrade.gateway.domain.TestRequestDto; | ||
5 | -import com.diligrp.xtrade.gateway.support.dispatch.DispatchContext; | ||
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; | 3 | +import com.diligrp.xtrade.business.domain.TestRequestDto; |
4 | +import com.diligrp.xtrade.core.common.annotation.DispatchMapping; | ||
5 | +import com.diligrp.xtrade.core.support.dispatch.DispatchContext; | ||
6 | +import com.diligrp.xtrade.core.support.dispatch.RequestDispatcher; | ||
9 | import org.springframework.stereotype.Component; | 7 | import org.springframework.stereotype.Component; |
10 | import org.springframework.validation.annotation.Validated; | 8 | import org.springframework.validation.annotation.Validated; |
11 | 9 | ||
12 | -import javax.servlet.http.HttpServletRequest; | ||
13 | - | ||
14 | /** | 10 | /** |
15 | * @Auther: miaoguoxin | 11 | * @Auther: miaoguoxin |
16 | * @Date: 2020/4/15 09:40 | 12 | * @Date: 2020/4/15 09:40 |
@@ -21,11 +17,6 @@ import javax.servlet.http.HttpServletRequest; | @@ -21,11 +17,6 @@ import javax.servlet.http.HttpServletRequest; | ||
21 | @DispatchMapping | 17 | @DispatchMapping |
22 | public class TestAggregationApplication { | 18 | public class TestAggregationApplication { |
23 | 19 | ||
24 | - @Autowired | ||
25 | - private ServerHttpRequest serverHttpRequest; | ||
26 | - @Autowired | ||
27 | - private HttpServletRequest httpServletRequest; | ||
28 | - | ||
29 | @DispatchMapping("/test") | 20 | @DispatchMapping("/test") |
30 | public String test(DispatchContext<TestRequestDto> dispatchContext) { | 21 | public String test(DispatchContext<TestRequestDto> dispatchContext) { |
31 | return "ffff"; | 22 | return "ffff"; |
src/main/java/com/diligrp/xtrade/gateway/controller/TestController.java renamed to gateway-business/src/main/java/com/diligrp/xtrade/business/controller/TestController.java
1 | -package com.diligrp.xtrade.gateway.controller; | 1 | +package com.diligrp.xtrade.business.controller; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.domain.TestRequestDto; | 3 | +import com.diligrp.xtrade.business.domain.TestRequestDto; |
4 | import com.diligrp.xtrade.shared.domain.Message; | 4 | import com.diligrp.xtrade.shared.domain.Message; |
5 | import org.springframework.beans.factory.annotation.Autowired; | 5 | import org.springframework.beans.factory.annotation.Autowired; |
6 | import org.springframework.beans.factory.annotation.Value; | 6 | import org.springframework.beans.factory.annotation.Value; |
7 | import org.springframework.cloud.context.config.annotation.RefreshScope; | 7 | import org.springframework.cloud.context.config.annotation.RefreshScope; |
8 | import org.springframework.cloud.gateway.config.GatewayProperties; | 8 | import org.springframework.cloud.gateway.config.GatewayProperties; |
9 | -import org.springframework.cloud.gateway.route.RouteDefinition; | ||
10 | import org.springframework.validation.annotation.Validated; | 9 | import org.springframework.validation.annotation.Validated; |
11 | import org.springframework.web.bind.annotation.GetMapping; | 10 | import org.springframework.web.bind.annotation.GetMapping; |
12 | -import org.springframework.web.bind.annotation.PathVariable; | ||
13 | import org.springframework.web.bind.annotation.RequestMapping; | 11 | import org.springframework.web.bind.annotation.RequestMapping; |
14 | import org.springframework.web.bind.annotation.RestController; | 12 | import org.springframework.web.bind.annotation.RestController; |
15 | 13 | ||
16 | -import javax.validation.constraints.Min; | ||
17 | -import java.util.List; | ||
18 | - | ||
19 | /** | 14 | /** |
20 | * @Auther: miaoguoxin | 15 | * @Auther: miaoguoxin |
21 | * @Date: 2020/4/10 16:01 | 16 | * @Date: 2020/4/10 16:01 |
src/main/java/com/diligrp/xtrade/gateway/domain/TestRequestDto.java renamed to gateway-business/src/main/java/com/diligrp/xtrade/business/domain/TestRequestDto.java
1 | -package com.diligrp.xtrade.gateway.domain; | 1 | +package com.diligrp.xtrade.business.domain; |
2 | 2 | ||
3 | import com.fasterxml.jackson.annotation.JsonFormat; | 3 | import com.fasterxml.jackson.annotation.JsonFormat; |
4 | -import lombok.Data; | ||
5 | 4 | ||
6 | import javax.validation.constraints.NotBlank; | 5 | import javax.validation.constraints.NotBlank; |
7 | import java.time.LocalDateTime; | 6 | import java.time.LocalDateTime; |
@@ -10,10 +9,25 @@ import java.time.LocalDateTime; | @@ -10,10 +9,25 @@ import java.time.LocalDateTime; | ||
10 | * @Auther: miaoguoxin | 9 | * @Auther: miaoguoxin |
11 | * @Date: 2020/4/15 15:56 | 10 | * @Date: 2020/4/15 15:56 |
12 | */ | 11 | */ |
13 | -@Data | ||
14 | public class TestRequestDto { | 12 | public class TestRequestDto { |
15 | @NotBlank(message = "{request.username.not.blank}") | 13 | @NotBlank(message = "{request.username.not.blank}") |
16 | private String username; | 14 | private String username; |
17 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | 15 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
18 | private LocalDateTime localDateTime; | 16 | private LocalDateTime localDateTime; |
17 | + | ||
18 | + public String getUsername() { | ||
19 | + return username; | ||
20 | + } | ||
21 | + | ||
22 | + public void setUsername(String username) { | ||
23 | + this.username = username; | ||
24 | + } | ||
25 | + | ||
26 | + public LocalDateTime getLocalDateTime() { | ||
27 | + return localDateTime; | ||
28 | + } | ||
29 | + | ||
30 | + public void setLocalDateTime(LocalDateTime localDateTime) { | ||
31 | + this.localDateTime = localDateTime; | ||
32 | + } | ||
19 | } | 33 | } |
src/main/resources/ValidationMessages.properties renamed to gateway-business/src/main/resources/ValidationMessages.properties
src/main/resources/ValidationMessages_zh_CN.properties renamed to gateway-business/src/main/resources/ValidationMessages_zh_CN.properties
src/main/resources/banner2.txt renamed to gateway-business/src/main/resources/banner2.txt
gateway-business/src/main/resources/bootstrap-dev.yml
0 → 100644
1 | +spring: | ||
2 | + cloud: | ||
3 | + nacos: | ||
4 | + discovery: | ||
5 | + username: nacos | ||
6 | + password: microtest | ||
7 | + server-addr: apitest.51shiban.com:80/n | ||
8 | + namespace: c86d8673-4d0a-469f-8bb8-1434c57c236c | ||
9 | + config: | ||
10 | + username: nacos | ||
11 | + password: microtest | ||
12 | + server-addr: apitest.51shiban.com:80/n | ||
13 | + namespace: c86d8673-4d0a-469f-8bb8-1434c57c236c |
src/main/resources/bootstrap-gateway-tempalte.yml renamed to gateway-business/src/main/resources/bootstrap-gateway-tempalte.yml
src/main/resources/bootstrap.yml renamed to gateway-business/src/main/resources/bootstrap.yml
@@ -22,4 +22,4 @@ mybatis: | @@ -22,4 +22,4 @@ mybatis: | ||
22 | config-location: classpath:/mybatis-config.xml | 22 | config-location: classpath:/mybatis-config.xml |
23 | xtrade: | 23 | xtrade: |
24 | gateway-loader: cloud | 24 | gateway-loader: cloud |
25 | - aggregation-scan-packages: com.diligrp.xtrade.gateway.application | 25 | + aggregation-scan-packages: com.diligrp.xtrade.business.application |
src/main/resources/ehcache.xml renamed to gateway-business/src/main/resources/ehcache.xml
src/main/resources/mybatis-config.xml renamed to gateway-business/src/main/resources/mybatis-config.xml
gateway-core/pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
5 | + <parent> | ||
6 | + <artifactId>xtrade-gateway-service</artifactId> | ||
7 | + <groupId>com.diligrp</groupId> | ||
8 | + <version>1.0.0</version> | ||
9 | + </parent> | ||
10 | + <modelVersion>4.0.0</modelVersion> | ||
11 | + | ||
12 | + <artifactId>gateway-core</artifactId> | ||
13 | + | ||
14 | + <dependencies> | ||
15 | + <dependency> | ||
16 | + <groupId>com.diligrp</groupId> | ||
17 | + <artifactId>xtrade-shared-spring-boot-starter</artifactId> | ||
18 | + </dependency> | ||
19 | + <dependency> | ||
20 | + <groupId>org.springframework.cloud</groupId> | ||
21 | + <artifactId>spring-cloud-starter-gateway</artifactId> | ||
22 | + </dependency> | ||
23 | + | ||
24 | + <dependency> | ||
25 | + <groupId>com.alibaba.cloud</groupId> | ||
26 | + <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> | ||
27 | + </dependency> | ||
28 | + <dependency> | ||
29 | + <groupId>com.alibaba.cloud</groupId> | ||
30 | + <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
31 | + </dependency> | ||
32 | + <dependency> | ||
33 | + <groupId>com.alibaba.nacos</groupId> | ||
34 | + <artifactId>nacos-api</artifactId> | ||
35 | + </dependency> | ||
36 | + <dependency> | ||
37 | + <groupId>com.alibaba.nacos</groupId> | ||
38 | + <artifactId>nacos-client</artifactId> | ||
39 | + </dependency> | ||
40 | + <dependency> | ||
41 | + <groupId>org.springframework.boot</groupId> | ||
42 | + <artifactId>spring-boot-starter-test</artifactId> | ||
43 | + <scope>test</scope> | ||
44 | + </dependency> | ||
45 | + <dependency> | ||
46 | + <groupId>org.reflections</groupId> | ||
47 | + <artifactId>reflections</artifactId> | ||
48 | + </dependency> | ||
49 | + <dependency> | ||
50 | + <groupId>org.mybatis.spring.boot</groupId> | ||
51 | + <artifactId>mybatis-spring-boot-starter</artifactId> | ||
52 | + </dependency> | ||
53 | + <dependency> | ||
54 | + <groupId>org.springframework.boot</groupId> | ||
55 | + <artifactId>spring-boot-starter-data-redis</artifactId> | ||
56 | + </dependency> | ||
57 | + <dependency> | ||
58 | + <groupId>org.apache.commons</groupId> | ||
59 | + <artifactId>commons-pool2</artifactId> | ||
60 | + </dependency> | ||
61 | + </dependencies> | ||
62 | +</project> | ||
0 | \ No newline at end of file | 63 | \ No newline at end of file |
gateway-core/src/main/java/com/diligrp/xtrade/core/api/Api.java
0 → 100644
1 | +package com.diligrp.xtrade.core.api; | ||
2 | + | ||
3 | + | ||
4 | +import java.io.Serializable; | ||
5 | + | ||
6 | +/** | ||
7 | + * @Auther: miaoguoxin | ||
8 | + * @Date: 2019/3/31 16:24 | ||
9 | + * @Description: 只用于网关api缓存的对象 | ||
10 | + */ | ||
11 | +public class Api implements Serializable { | ||
12 | + /**服务名称*/ | ||
13 | + private String serviceId; | ||
14 | + | ||
15 | + private String patternUrl; | ||
16 | + //并发量 | ||
17 | + private int replenishRate = 2; | ||
18 | + //容量 | ||
19 | + private int burstCapacity = 5; | ||
20 | + | ||
21 | + private Integer state; | ||
22 | + | ||
23 | + public String getServiceId() { | ||
24 | + return serviceId; | ||
25 | + } | ||
26 | + | ||
27 | + public void setServiceId(String serviceId) { | ||
28 | + this.serviceId = serviceId; | ||
29 | + } | ||
30 | + | ||
31 | + public String getPatternUrl() { | ||
32 | + return patternUrl; | ||
33 | + } | ||
34 | + | ||
35 | + public void setPatternUrl(String patternUrl) { | ||
36 | + this.patternUrl = patternUrl; | ||
37 | + } | ||
38 | + | ||
39 | + public int getReplenishRate() { | ||
40 | + return replenishRate; | ||
41 | + } | ||
42 | + | ||
43 | + public void setReplenishRate(int replenishRate) { | ||
44 | + this.replenishRate = replenishRate; | ||
45 | + } | ||
46 | + | ||
47 | + public int getBurstCapacity() { | ||
48 | + return burstCapacity; | ||
49 | + } | ||
50 | + | ||
51 | + public void setBurstCapacity(int burstCapacity) { | ||
52 | + this.burstCapacity = burstCapacity; | ||
53 | + } | ||
54 | + | ||
55 | + public Integer getState() { | ||
56 | + return state; | ||
57 | + } | ||
58 | + | ||
59 | + public void setState(Integer state) { | ||
60 | + this.state = state; | ||
61 | + } | ||
62 | +} |
src/main/java/com/diligrp/xtrade/gateway/api/ApiManager.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/api/ApiManager.java
1 | -package com.diligrp.xtrade.gateway.api; | 1 | +package com.diligrp.xtrade.core.api; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.config.property.AuthPathProperties; | 3 | +import com.diligrp.xtrade.core.config.property.AuthPathProperties; |
4 | import org.springframework.beans.factory.annotation.Autowired; | 4 | import org.springframework.beans.factory.annotation.Autowired; |
5 | import org.springframework.http.server.PathContainer; | 5 | import org.springframework.http.server.PathContainer; |
6 | import org.springframework.stereotype.Component; | 6 | import org.springframework.stereotype.Component; |
src/main/java/com/diligrp/xtrade/gateway/common/annotation/DispatchMapping.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/annotation/DispatchMapping.java
src/main/java/com/diligrp/xtrade/gateway/common/constant/GatewayAttrType.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/constant/GatewayAttrType.java
src/main/java/com/diligrp/xtrade/gateway/common/constant/GatewayConst.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/constant/GatewayConst.java
1 | -package com.diligrp.xtrade.gateway.common.constant; | 1 | +package com.diligrp.xtrade.core.common.constant; |
2 | 2 | ||
3 | import java.util.concurrent.atomic.AtomicBoolean; | 3 | import java.util.concurrent.atomic.AtomicBoolean; |
4 | 4 | ||
@@ -10,10 +10,13 @@ import java.util.concurrent.atomic.AtomicBoolean; | @@ -10,10 +10,13 @@ import java.util.concurrent.atomic.AtomicBoolean; | ||
10 | public class GatewayConst { | 10 | public class GatewayConst { |
11 | /**用于标记聚合服务mapping是否已经初始化(防止重复加载)*/ | 11 | /**用于标记聚合服务mapping是否已经初始化(防止重复加载)*/ |
12 | public static final AtomicBoolean HAS_INIT_MAPPING = new AtomicBoolean(false); | 12 | public static final AtomicBoolean HAS_INIT_MAPPING = new AtomicBoolean(false); |
13 | + /*webExchange中保存的属性*/ | ||
13 | /**请求体缓存Key*/ | 14 | /**请求体缓存Key*/ |
14 | - public static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObj"; | ||
15 | - /**缓存Api配置key*/ | ||
16 | - public static final String CACHE_API_OBJECT_KEY = "cachedApiConfigObject"; | 15 | + public static final String CACHED_REQUEST_BODY_STR_ATTR = "cachedRequestBodyStr"; |
16 | + /**网关异常记录*/ | ||
17 | + public static final String GATEWAY_EXCEPTION_ATTR = "gatewayException"; | ||
18 | + /**响应体缓存记录*/ | ||
19 | + public static final String CACHED_RESPONSE_BODY_STR_ATTR = "cachedResponseBodyStr"; | ||
17 | 20 | ||
18 | /* 自定义的过滤器顺序都从100开始(除ResponseBodyRead),避免顺序问题*/ | 21 | /* 自定义的过滤器顺序都从100开始(除ResponseBodyRead),避免顺序问题*/ |
19 | /**缓存请求体过滤器*/ | 22 | /**缓存请求体过滤器*/ |
src/main/java/com/diligrp/xtrade/gateway/common/utils/CacheUtils.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/utils/CacheUtils.java
src/main/java/com/diligrp/xtrade/gateway/common/utils/PathUtils.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/utils/PathUtils.java
1 | -package com.diligrp.xtrade.gateway.common.utils; | 1 | +package com.diligrp.xtrade.core.common.utils; |
2 | 2 | ||
3 | import org.springframework.http.server.PathContainer; | 3 | import org.springframework.http.server.PathContainer; |
4 | import org.springframework.util.CollectionUtils; | 4 | import org.springframework.util.CollectionUtils; |
src/main/java/com/diligrp/xtrade/gateway/common/utils/ResponseUtils.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/utils/ResponseUtils.java
1 | -package com.diligrp.xtrade.gateway.common.utils; | 1 | +package com.diligrp.xtrade.core.common.utils; |
2 | 2 | ||
3 | import com.diligrp.xtrade.shared.domain.Message; | 3 | import com.diligrp.xtrade.shared.domain.Message; |
4 | import com.diligrp.xtrade.shared.util.JsonUtils; | 4 | import com.diligrp.xtrade.shared.util.JsonUtils; |
src/main/java/com/diligrp/xtrade/gateway/common/utils/ValidateUtils.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/common/utils/ValidateUtils.java
1 | -package com.diligrp.xtrade.gateway.common.utils; | 1 | +package com.diligrp.xtrade.core.common.utils; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.exception.GatewayParamNotValidException; | 3 | +import com.diligrp.xtrade.core.exception.GatewayParamNotValidException; |
4 | import org.springframework.beans.factory.annotation.Autowired; | 4 | import org.springframework.beans.factory.annotation.Autowired; |
5 | import org.springframework.stereotype.Component; | 5 | import org.springframework.stereotype.Component; |
6 | 6 |
src/main/java/com/diligrp/xtrade/gateway/config/CacheManagerConfiguration.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/config/CacheManagerConfiguration.java
src/main/java/com/diligrp/xtrade/gateway/config/ExceptionConfiguration.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/config/ExceptionConfiguration.java
1 | -package com.diligrp.xtrade.gateway.config; | 1 | +package com.diligrp.xtrade.core.config; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.exception.GlobalGatewayErrorHandler; | 3 | +import com.diligrp.xtrade.core.exception.GlobalGatewayErrorHandler; |
4 | import org.springframework.beans.factory.ObjectProvider; | 4 | import org.springframework.beans.factory.ObjectProvider; |
5 | import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; | 5 | import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; |
6 | import org.springframework.context.annotation.Bean; | 6 | import org.springframework.context.annotation.Bean; |
src/main/java/com/diligrp/xtrade/gateway/config/GatewayResourceLoaderConfiguration.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/config/GatewayResourceLoaderConfiguration.java
1 | -package com.diligrp.xtrade.gateway.config; | 1 | +package com.diligrp.xtrade.core.config; |
2 | 2 | ||
3 | import com.alibaba.cloud.nacos.NacosConfigProperties; | 3 | import com.alibaba.cloud.nacos.NacosConfigProperties; |
4 | import com.alibaba.nacos.api.NacosFactory; | 4 | import com.alibaba.nacos.api.NacosFactory; |
5 | import com.alibaba.nacos.api.PropertyKeyConst; | 5 | import com.alibaba.nacos.api.PropertyKeyConst; |
6 | import com.alibaba.nacos.api.config.ConfigService; | 6 | import com.alibaba.nacos.api.config.ConfigService; |
7 | import com.alibaba.nacos.api.exception.NacosException; | 7 | import com.alibaba.nacos.api.exception.NacosException; |
8 | -import com.diligrp.xtrade.gateway.exception.GatewayServiceException; | ||
9 | -import com.diligrp.xtrade.gateway.route.DynamicRouteLoaderIntf; | ||
10 | -import com.diligrp.xtrade.gateway.route.impl.CloudRouteResourceLoader; | ||
11 | -import com.diligrp.xtrade.gateway.route.impl.MysqlRouteResourceLoader; | 8 | +import com.diligrp.xtrade.core.exception.GatewayRouteResourceException; |
9 | +import com.diligrp.xtrade.core.route.DynamicRouteLoaderIntf; | ||
10 | +import com.diligrp.xtrade.core.route.impl.CloudRouteResourceLoader; | ||
11 | +import com.diligrp.xtrade.core.route.impl.MysqlRouteResourceLoader; | ||
12 | import org.apache.logging.log4j.util.Strings; | 12 | import org.apache.logging.log4j.util.Strings; |
13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
14 | import org.springframework.context.annotation.Bean; | 14 | import org.springframework.context.annotation.Bean; |
@@ -40,7 +40,7 @@ public class GatewayResourceLoaderConfiguration { | @@ -40,7 +40,7 @@ public class GatewayResourceLoaderConfiguration { | ||
40 | try { | 40 | try { |
41 | configService = NacosFactory.createConfigService(properties); | 41 | configService = NacosFactory.createConfigService(properties); |
42 | } catch (NacosException e) { | 42 | } catch (NacosException e) { |
43 | - throw new GatewayServiceException("init nacos configService failed", e); | 43 | + throw new GatewayRouteResourceException("init nacos configService failed", e); |
44 | } | 44 | } |
45 | return configService; | 45 | return configService; |
46 | } | 46 | } |
src/main/java/com/diligrp/xtrade/gateway/config/property/AuthPathProperties.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/config/property/AuthPathProperties.java
1 | -package com.diligrp.xtrade.gateway.config.property; | 1 | +package com.diligrp.xtrade.core.config.property; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.common.utils.PathUtils; | ||
4 | -import lombok.Data; | 3 | +import com.diligrp.xtrade.core.common.utils.PathUtils; |
5 | import org.springframework.boot.context.properties.ConfigurationProperties; | 4 | import org.springframework.boot.context.properties.ConfigurationProperties; |
6 | import org.springframework.stereotype.Component; | 5 | import org.springframework.stereotype.Component; |
7 | import org.springframework.web.util.pattern.PathPattern; | 6 | import org.springframework.web.util.pattern.PathPattern; |
@@ -14,7 +13,6 @@ import java.util.List; | @@ -14,7 +13,6 @@ import java.util.List; | ||
14 | */ | 13 | */ |
15 | @Component | 14 | @Component |
16 | @ConfigurationProperties(prefix = "xtrade.auth") | 15 | @ConfigurationProperties(prefix = "xtrade.auth") |
17 | -@Data | ||
18 | public class AuthPathProperties { | 16 | public class AuthPathProperties { |
19 | /**不需要校验权限的path*/ | 17 | /**不需要校验权限的path*/ |
20 | private String[] excludePaths; | 18 | private String[] excludePaths; |
@@ -33,4 +31,11 @@ public class AuthPathProperties { | @@ -33,4 +31,11 @@ public class AuthPathProperties { | ||
33 | } | 31 | } |
34 | 32 | ||
35 | 33 | ||
34 | + public String[] getExcludePaths() { | ||
35 | + return excludePaths; | ||
36 | + } | ||
37 | + | ||
38 | + public List<PathPattern> getExcludePathPatterns() { | ||
39 | + return excludePathPatterns; | ||
40 | + } | ||
36 | } | 41 | } |
src/main/java/com/diligrp/xtrade/gateway/config/property/DispatchProperties.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/config/property/DispatchProperties.java
1 | -package com.diligrp.xtrade.gateway.config.property; | 1 | +package com.diligrp.xtrade.core.config.property; |
2 | 2 | ||
3 | -import lombok.Data; | ||
4 | import org.springframework.boot.context.properties.ConfigurationProperties; | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; |
5 | import org.springframework.stereotype.Component; | 4 | import org.springframework.stereotype.Component; |
6 | 5 | ||
@@ -10,7 +9,6 @@ import org.springframework.stereotype.Component; | @@ -10,7 +9,6 @@ import org.springframework.stereotype.Component; | ||
10 | */ | 9 | */ |
11 | @Component | 10 | @Component |
12 | @ConfigurationProperties(prefix = "xtrade") | 11 | @ConfigurationProperties(prefix = "xtrade") |
13 | -@Data | ||
14 | public class DispatchProperties { | 12 | public class DispatchProperties { |
15 | /** 扫描包路径 */ | 13 | /** 扫描包路径 */ |
16 | private String[] aggregationScanPackages; | 14 | private String[] aggregationScanPackages; |
@@ -18,4 +16,12 @@ public class DispatchProperties { | @@ -18,4 +16,12 @@ public class DispatchProperties { | ||
18 | public DispatchProperties(String[] aggregationScanPackages) { | 16 | public DispatchProperties(String[] aggregationScanPackages) { |
19 | this.aggregationScanPackages = aggregationScanPackages; | 17 | this.aggregationScanPackages = aggregationScanPackages; |
20 | } | 18 | } |
19 | + | ||
20 | + public String[] getAggregationScanPackages() { | ||
21 | + return aggregationScanPackages; | ||
22 | + } | ||
23 | + | ||
24 | + public void setAggregationScanPackages(String[] aggregationScanPackages) { | ||
25 | + this.aggregationScanPackages = aggregationScanPackages; | ||
26 | + } | ||
21 | } | 27 | } |
gateway-core/src/main/java/com/diligrp/xtrade/core/exception/GatewayDispatchException.java
0 → 100644
1 | +package com.diligrp.xtrade.core.exception; | ||
2 | + | ||
3 | +import com.diligrp.xtrade.core.support.dispatch.RequestDispatcher; | ||
4 | + | ||
5 | +/** | ||
6 | + * @Auther: miaoguoxin | ||
7 | + * @Date: 2020/4/18 19:43 | ||
8 | + * @Description: 用于操作聚合服务分发 | ||
9 | + * {@link RequestDispatcher} | ||
10 | + */ | ||
11 | +public class GatewayDispatchException extends RuntimeException { | ||
12 | + public GatewayDispatchException(String message) { | ||
13 | + super(message); | ||
14 | + } | ||
15 | + | ||
16 | + public GatewayDispatchException(String message, Throwable cause) { | ||
17 | + super(message, cause); | ||
18 | + } | ||
19 | + | ||
20 | + public GatewayDispatchException(Throwable cause) { | ||
21 | + super(cause); | ||
22 | + } | ||
23 | +} |
src/main/java/com/diligrp/xtrade/gateway/exception/GatewayParamNotValidException.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/exception/GatewayParamNotValidException.java
src/main/java/com/diligrp/xtrade/gateway/exception/GatewayServiceException.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/exception/GatewayRouteResourceException.java
1 | -package com.diligrp.xtrade.gateway.exception; | 1 | +package com.diligrp.xtrade.core.exception; |
2 | 2 | ||
3 | -import lombok.Getter; | ||
4 | 3 | ||
5 | /** | 4 | /** |
6 | * @Auther: miaoguoxin | 5 | * @Auther: miaoguoxin |
7 | * @Date: 2020/4/10 11:28 | 6 | * @Date: 2020/4/10 11:28 |
8 | * @Description: | 7 | * @Description: |
9 | */ | 8 | */ |
10 | -@Getter | ||
11 | -public class GatewayServiceException extends RuntimeException { | 9 | +public class GatewayRouteResourceException extends RuntimeException { |
12 | private Object[] args; | 10 | private Object[] args; |
13 | 11 | ||
14 | - public GatewayServiceException() { | 12 | + public GatewayRouteResourceException() { |
15 | } | 13 | } |
16 | 14 | ||
17 | - public GatewayServiceException(String message) { | 15 | + public GatewayRouteResourceException(String message) { |
18 | super(message); | 16 | super(message); |
19 | } | 17 | } |
20 | 18 | ||
21 | - public GatewayServiceException(String message, Object[] args) { | 19 | + public GatewayRouteResourceException(String message, Object[] args) { |
22 | super(message); | 20 | super(message); |
23 | this.args = args; | 21 | this.args = args; |
24 | } | 22 | } |
25 | 23 | ||
26 | - public GatewayServiceException(String message, Throwable cause, Object[] args) { | 24 | + public GatewayRouteResourceException(String message, Throwable cause, Object[] args) { |
27 | super(message, cause); | 25 | super(message, cause); |
28 | this.args = args; | 26 | this.args = args; |
29 | } | 27 | } |
30 | 28 | ||
31 | - public GatewayServiceException(Throwable cause, Object[] args) { | 29 | + public GatewayRouteResourceException(Throwable cause, Object[] args) { |
32 | super(cause); | 30 | super(cause); |
33 | this.args = args; | 31 | this.args = args; |
34 | } | 32 | } |
35 | 33 | ||
36 | - public GatewayServiceException(String message, Throwable cause) { | 34 | + public GatewayRouteResourceException(String message, Throwable cause) { |
37 | super(message, cause); | 35 | super(message, cause); |
38 | } | 36 | } |
39 | 37 | ||
40 | - public GatewayServiceException(Throwable cause) { | 38 | + public GatewayRouteResourceException(Throwable cause) { |
41 | super(cause); | 39 | super(cause); |
42 | } | 40 | } |
43 | 41 | ||
44 | 42 | ||
43 | + public Object[] getArgs() { | ||
44 | + return args; | ||
45 | + } | ||
45 | } | 46 | } |
src/main/java/com/diligrp/xtrade/gateway/exception/GlobalGatewayErrorHandler.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/exception/GlobalGatewayErrorHandler.java
1 | -package com.diligrp.xtrade.gateway.exception; | 1 | +package com.diligrp.xtrade.core.exception; |
2 | 2 | ||
3 | -import com.alibaba.fastjson.JSON; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | import com.diligrp.xtrade.shared.domain.Message; | 4 | import com.diligrp.xtrade.shared.domain.Message; |
5 | -import lombok.extern.slf4j.Slf4j; | 5 | +import com.diligrp.xtrade.shared.type.ErrorCode; |
6 | +import com.diligrp.xtrade.shared.util.JsonUtils; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
6 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
7 | import org.springframework.boot.web.reactive.error.ErrorAttributes; | 10 | import org.springframework.boot.web.reactive.error.ErrorAttributes; |
8 | import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; | 11 | import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; |
9 | -import org.springframework.cloud.gateway.support.NotFoundException; | ||
10 | import org.springframework.http.HttpStatus; | 12 | import org.springframework.http.HttpStatus; |
11 | import org.springframework.http.MediaType; | 13 | import org.springframework.http.MediaType; |
12 | import org.springframework.http.codec.HttpMessageReader; | 14 | import org.springframework.http.codec.HttpMessageReader; |
@@ -24,17 +26,17 @@ import org.springframework.web.server.ServerWebExchange; | @@ -24,17 +26,17 @@ import org.springframework.web.server.ServerWebExchange; | ||
24 | import reactor.core.publisher.Mono; | 26 | import reactor.core.publisher.Mono; |
25 | 27 | ||
26 | import java.util.Collections; | 28 | import java.util.Collections; |
27 | -import java.util.HashMap; | ||
28 | import java.util.List; | 29 | import java.util.List; |
29 | import java.util.Map; | 30 | import java.util.Map; |
30 | 31 | ||
32 | + | ||
31 | /** | 33 | /** |
32 | * @Auther: miaoguoxin | 34 | * @Auther: miaoguoxin |
33 | * @Date: 2020/04/13 10:55 | 35 | * @Date: 2020/04/13 10:55 |
34 | * @Description: 处理网关本身抛出的异常 | 36 | * @Description: 处理网关本身抛出的异常 |
35 | */ | 37 | */ |
36 | -@Slf4j | ||
37 | public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { | 38 | public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { |
39 | + private static final Logger log = LoggerFactory.getLogger(GlobalGatewayErrorHandler.class); | ||
38 | @Autowired | 40 | @Autowired |
39 | private ErrorAttributes errorAttributes; | 41 | private ErrorAttributes errorAttributes; |
40 | /** | 42 | /** |
@@ -89,51 +91,51 @@ public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { | @@ -89,51 +91,51 @@ public class GlobalGatewayErrorHandler implements ErrorWebExceptionHandler { | ||
89 | if (exchange.getResponse().isCommitted()) { | 91 | if (exchange.getResponse().isCommitted()) { |
90 | return Mono.error(ex); | 92 | return Mono.error(ex); |
91 | } | 93 | } |
92 | - errorAttributes.storeErrorInformation(ex,exchange); | 94 | + errorAttributes.storeErrorInformation(ex, exchange); |
93 | ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders); | 95 | ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders); |
94 | - return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest) | 96 | + return RouterFunctions |
97 | + .route(RequestPredicates.all(), request1 -> renderErrorResponse(request1, exchange)) | ||
98 | + .route(newRequest) | ||
95 | .switchIfEmpty(Mono.error(ex)) | 99 | .switchIfEmpty(Mono.error(ex)) |
96 | .flatMap((handler) -> handler.handle(newRequest)) | 100 | .flatMap((handler) -> handler.handle(newRequest)) |
97 | .flatMap((response) -> write(exchange, response)); | 101 | .flatMap((response) -> write(exchange, response)); |
98 | } | 102 | } |
99 | 103 | ||
100 | - private HttpStatus getHttpStatus(Throwable ex) { | ||
101 | - HttpStatus httpStatus; | ||
102 | - if (ex instanceof NotFoundException) { | ||
103 | - httpStatus = HttpStatus.NOT_FOUND; | ||
104 | - } else if (ex instanceof ResponseStatusException) { | ||
105 | - ResponseStatusException responseStatusException = (ResponseStatusException) ex; | ||
106 | - httpStatus = responseStatusException.getStatus(); | ||
107 | - } else { | ||
108 | - httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; | ||
109 | - } | ||
110 | - return httpStatus; | ||
111 | - } | ||
112 | - | ||
113 | - private Map<String, Object> getHttpResult(HttpStatus httpStatus) { | ||
114 | - Message<String> apiResult= Message.failure(httpStatus.value(),httpStatus.toString()); | ||
115 | - Map<String,Object> result=new HashMap<>(); | ||
116 | - result.put("httpStatus",httpStatus); | ||
117 | - result.put("body", JSON.toJSONString(apiResult)); | ||
118 | - return result; | ||
119 | - } | ||
120 | 104 | ||
121 | /** | 105 | /** |
122 | * 参考DefaultErrorWebExceptionHandler | 106 | * 参考DefaultErrorWebExceptionHandler |
123 | */ | 107 | */ |
124 | - private Mono<ServerResponse> renderErrorResponse(ServerRequest request) { | ||
125 | - Map<String, Object> errorAttrs = errorAttributes.getErrorAttributes(request, true); | ||
126 | - String msg = String.format("%s %s", | ||
127 | - errorAttrs.get("path"), errorAttrs.get("error")); | ||
128 | - Message<Object> message = Message.failure((int)errorAttrs.get("status"),msg); | ||
129 | - if (message.getCode().equals(500)){ | ||
130 | - log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", errorAttrs.get("path"),errorAttributes.getError(request)); | ||
131 | - } | 108 | + private Mono<ServerResponse> renderErrorResponse(ServerRequest request, ServerWebExchange exchange) { |
109 | + Throwable error = errorAttributes.getError(request); | ||
110 | + Message<Object> message = handleException(error); | ||
111 | + exchange.getAttributes().putIfAbsent(GatewayConst.GATEWAY_EXCEPTION_ATTR, error); | ||
112 | + exchange.getAttributes().putIfAbsent(GatewayConst.CACHED_RESPONSE_BODY_STR_ATTR, JsonUtils.toJsonString(message)); | ||
132 | return ServerResponse.status(HttpStatus.OK) | 113 | return ServerResponse.status(HttpStatus.OK) |
133 | .contentType(MediaType.APPLICATION_JSON) | 114 | .contentType(MediaType.APPLICATION_JSON) |
134 | .body(BodyInserters.fromValue(message)); | 115 | .body(BodyInserters.fromValue(message)); |
135 | } | 116 | } |
136 | 117 | ||
118 | + private static Message<Object> handleException(Throwable ex) { | ||
119 | + if (ex instanceof GatewayDispatchException) { | ||
120 | + GatewayDispatchException dispatchEx = (GatewayDispatchException) ex; | ||
121 | + if (dispatchEx.getCause() != null) { | ||
122 | + return handleException(dispatchEx.getCause()); | ||
123 | + } else { | ||
124 | + return Message.failure( | ||
125 | + ErrorCode.UNKNOWN_ERROR.getCode(), ErrorCode.UNKNOWN_ERROR.getName()); | ||
126 | + } | ||
127 | + } else if (ex instanceof ResponseStatusException) { | ||
128 | + int statusCode = ((ResponseStatusException) ex).getStatus().value(); | ||
129 | + return Message.failure(statusCode, ex.getMessage()); | ||
130 | + } else if (ex instanceof GatewayParamNotValidException) { | ||
131 | + return Message.failure( | ||
132 | + ErrorCode.ILLEGAL_PARAMS.getCode(), ex.getMessage()); | ||
133 | + } else { | ||
134 | + return Message.failure( | ||
135 | + ErrorCode.UNKNOWN_ERROR.getCode(), ErrorCode.UNKNOWN_ERROR.getName()); | ||
136 | + } | ||
137 | + } | ||
138 | + | ||
137 | /** | 139 | /** |
138 | * 参考AbstractErrorWebExceptionHandler | 140 | * 参考AbstractErrorWebExceptionHandler |
139 | */ | 141 | */ |
src/main/java/com/diligrp/xtrade/gateway/filters/factory/DispatchGatewayFilterFactory.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/factory/DispatchGatewayFilterFactory.java
1 | -package com.diligrp.xtrade.gateway.filters.factory; | 1 | +package com.diligrp.xtrade.core.filters.factory; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.common.constant.GatewayConst; | ||
4 | -import com.diligrp.xtrade.gateway.common.utils.ResponseUtils; | ||
5 | -import com.diligrp.xtrade.gateway.support.dispatch.RequestDispatcher; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | +import com.diligrp.xtrade.core.common.utils.ResponseUtils; | ||
5 | +import com.diligrp.xtrade.core.support.dispatch.RequestDispatcher; | ||
6 | import com.diligrp.xtrade.shared.domain.Message; | 6 | import com.diligrp.xtrade.shared.domain.Message; |
7 | -import lombok.Data; | ||
8 | import org.springframework.beans.factory.annotation.Autowired; | 7 | import org.springframework.beans.factory.annotation.Autowired; |
9 | import org.springframework.cloud.gateway.filter.GatewayFilter; | 8 | import org.springframework.cloud.gateway.filter.GatewayFilter; |
10 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 9 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
@@ -15,7 +14,8 @@ import org.springframework.stereotype.Component; | @@ -15,7 +14,8 @@ import org.springframework.stereotype.Component; | ||
15 | import org.springframework.web.server.ServerWebExchange; | 14 | import org.springframework.web.server.ServerWebExchange; |
16 | import reactor.core.publisher.Mono; | 15 | import reactor.core.publisher.Mono; |
17 | 16 | ||
18 | -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY; | 17 | +import java.util.concurrent.ConcurrentLinkedQueue; |
18 | + | ||
19 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; | 19 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; |
20 | 20 | ||
21 | /** | 21 | /** |
@@ -25,6 +25,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O | @@ -25,6 +25,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O | ||
25 | */ | 25 | */ |
26 | @Component | 26 | @Component |
27 | public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<DispatchGatewayFilterFactory.Config> { | 27 | public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<DispatchGatewayFilterFactory.Config> { |
28 | + | ||
28 | @Autowired | 29 | @Autowired |
29 | private RequestDispatcher requestDispatcher; | 30 | private RequestDispatcher requestDispatcher; |
30 | 31 | ||
@@ -47,8 +48,8 @@ public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<D | @@ -47,8 +48,8 @@ public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<D | ||
47 | @Override | 48 | @Override |
48 | public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | 49 | public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
49 | String rawPath = exchange.getRequest().getURI().getRawPath(); | 50 | String rawPath = exchange.getRequest().getURI().getRawPath(); |
50 | - String paramsJson = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY); | ||
51 | - Message message = requestDispatcher.executeMethod(rawPath, paramsJson, exchange); | 51 | + String paramsJson = exchange.getAttribute(GatewayConst.CACHED_REQUEST_BODY_STR_ATTR); |
52 | + Message<Object> message = requestDispatcher.executeMethod(rawPath, paramsJson, exchange); | ||
52 | //throw new RuntimeException("gggg"); | 53 | //throw new RuntimeException("gggg"); |
53 | exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE); | 54 | exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE); |
54 | return ResponseUtils.writeResponse(exchange.getResponse(),message); | 55 | return ResponseUtils.writeResponse(exchange.getResponse(),message); |
@@ -61,7 +62,6 @@ public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<D | @@ -61,7 +62,6 @@ public class DispatchGatewayFilterFactory extends AbstractGatewayFilterFactory<D | ||
61 | } | 62 | } |
62 | 63 | ||
63 | 64 | ||
64 | - @Data | ||
65 | public static class Config { | 65 | public static class Config { |
66 | } | 66 | } |
67 | 67 |
src/main/java/com/diligrp/xtrade/gateway/filters/global/AuthGlobalFilter.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/global/AuthGlobalFilter.java
1 | -package com.diligrp.xtrade.gateway.filters.global; | 1 | +package com.diligrp.xtrade.core.filters.global; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.api.ApiManager; | ||
4 | -import com.diligrp.xtrade.gateway.common.utils.ResponseUtils; | ||
5 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.api.ApiManager; |
4 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; | ||
5 | +import com.diligrp.xtrade.core.common.utils.ResponseUtils; | ||
6 | import org.springframework.beans.factory.annotation.Autowired; | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 7 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
8 | import org.springframework.cloud.gateway.filter.GlobalFilter; | 8 | import org.springframework.cloud.gateway.filter.GlobalFilter; |
9 | +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||
9 | import org.springframework.core.Ordered; | 10 | import org.springframework.core.Ordered; |
10 | import org.springframework.http.MediaType; | 11 | import org.springframework.http.MediaType; |
11 | import org.springframework.http.server.reactive.ServerHttpResponse; | 12 | import org.springframework.http.server.reactive.ServerHttpResponse; |
@@ -17,9 +18,6 @@ import java.net.URI; | @@ -17,9 +18,6 @@ import java.net.URI; | ||
17 | import java.util.LinkedHashSet; | 18 | import java.util.LinkedHashSet; |
18 | import java.util.Optional; | 19 | import java.util.Optional; |
19 | 20 | ||
20 | -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.AUTH_FILTER_ORDER; | ||
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; | ||
23 | 21 | ||
24 | /** | 22 | /** |
25 | * @Auther: miaoguoxin | 23 | * @Auther: miaoguoxin |
@@ -27,7 +25,6 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O | @@ -27,7 +25,6 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.O | ||
27 | * @Description: 用于权限操作 | 25 | * @Description: 用于权限操作 |
28 | */ | 26 | */ |
29 | @Component | 27 | @Component |
30 | -@Slf4j | ||
31 | public class AuthGlobalFilter implements GlobalFilter, Ordered { | 28 | public class AuthGlobalFilter implements GlobalFilter, Ordered { |
32 | @Autowired | 29 | @Autowired |
33 | private ApiManager apiManager; | 30 | private ApiManager apiManager; |
@@ -38,15 +35,15 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { | @@ -38,15 +35,15 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { | ||
38 | ServerHttpResponse response = exchange.getResponse(); | 35 | ServerHttpResponse response = exchange.getResponse(); |
39 | //排除在外的Uri跳过权限 | 36 | //排除在外的Uri跳过权限 |
40 | if (apiManager.isExcludePathMatch(originalUri)) { | 37 | if (apiManager.isExcludePathMatch(originalUri)) { |
41 | - // return chain.filter(exchange); | ||
42 | - throw new RuntimeException("异常测试"); | 38 | + return chain.filter(exchange); |
39 | + // throw new RuntimeException("异常测试"); | ||
43 | } | 40 | } |
44 | - exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE); | 41 | + exchange.getAttributes().put(ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, MediaType.APPLICATION_JSON_VALUE); |
45 | return ResponseUtils.writeForbidden(response); | 42 | return ResponseUtils.writeForbidden(response); |
46 | } | 43 | } |
47 | 44 | ||
48 | private static String getOriginalUri(ServerWebExchange exchange) { | 45 | private static String getOriginalUri(ServerWebExchange exchange) { |
49 | - LinkedHashSet<URI> uris = exchange.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); | 46 | + LinkedHashSet<URI> uris = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR); |
50 | return Optional.ofNullable(uris) | 47 | return Optional.ofNullable(uris) |
51 | .flatMap(us -> us.stream().findFirst().map(URI::getRawPath)) | 48 | .flatMap(us -> us.stream().findFirst().map(URI::getRawPath)) |
52 | .orElse(exchange.getRequest().getURI().getRawPath()); | 49 | .orElse(exchange.getRequest().getURI().getRawPath()); |
@@ -54,7 +51,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { | @@ -54,7 +51,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { | ||
54 | 51 | ||
55 | @Override | 52 | @Override |
56 | public int getOrder() { | 53 | public int getOrder() { |
57 | - return AUTH_FILTER_ORDER; | 54 | + return GatewayConst.AUTH_FILTER_ORDER; |
58 | } | 55 | } |
59 | 56 | ||
60 | } | 57 | } |
src/main/java/com/diligrp/xtrade/gateway/filters/global/CacheRequestBodyGlobalFilter.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/global/CacheRequestBodyGlobalFilter.java
1 | -package com.diligrp.xtrade.gateway.filters.global; | 1 | +package com.diligrp.xtrade.core.filters.global; |
2 | 2 | ||
3 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 4 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
5 | import org.springframework.cloud.gateway.filter.GlobalFilter; | 5 | import org.springframework.cloud.gateway.filter.GlobalFilter; |
6 | import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage; | 6 | import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage; |
@@ -24,8 +24,6 @@ import reactor.core.publisher.Mono; | @@ -24,8 +24,6 @@ import reactor.core.publisher.Mono; | ||
24 | 24 | ||
25 | import java.util.List; | 25 | import java.util.List; |
26 | 26 | ||
27 | -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_BODY_FILTER_ORDER; | ||
28 | -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY; | ||
29 | 27 | ||
30 | /** | 28 | /** |
31 | * @Auther: miaoguoxin | 29 | * @Auther: miaoguoxin |
@@ -34,7 +32,6 @@ import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQU | @@ -34,7 +32,6 @@ import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQU | ||
34 | * {@link ModifyRequestBodyGatewayFilterFactory} | 32 | * {@link ModifyRequestBodyGatewayFilterFactory} |
35 | */ | 33 | */ |
36 | @Component | 34 | @Component |
37 | -@Slf4j | ||
38 | public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { | 35 | public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { |
39 | private static final List<HttpMessageReader<?>> messageReaders = HandlerStrategies | 36 | private static final List<HttpMessageReader<?>> messageReaders = HandlerStrategies |
40 | .withDefaults().messageReaders(); | 37 | .withDefaults().messageReaders(); |
@@ -45,7 +42,7 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { | @@ -45,7 +42,7 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { | ||
45 | messageReaders); | 42 | messageReaders); |
46 | Mono<String> modifiedBody = serverRequest.bodyToMono(String.class) | 43 | Mono<String> modifiedBody = serverRequest.bodyToMono(String.class) |
47 | .flatMap(body -> { | 44 | .flatMap(body -> { |
48 | - exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, body); | 45 | + exchange.getAttributes().put(GatewayConst.CACHED_REQUEST_BODY_STR_ATTR, body); |
49 | return Mono.just(body); | 46 | return Mono.just(body); |
50 | }); | 47 | }); |
51 | 48 | ||
@@ -91,7 +88,7 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { | @@ -91,7 +88,7 @@ public class CacheRequestBodyGlobalFilter implements GlobalFilter, Ordered { | ||
91 | 88 | ||
92 | @Override | 89 | @Override |
93 | public int getOrder() { | 90 | public int getOrder() { |
94 | - return CACHE_BODY_FILTER_ORDER; | 91 | + return GatewayConst.CACHE_BODY_FILTER_ORDER; |
95 | } | 92 | } |
96 | 93 | ||
97 | } | 94 | } |
src/main/java/com/diligrp/xtrade/gateway/filters/global/ResponseReadBodyGlobalFilter.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/global/ResponseReadBodyGlobalFilter.java
1 | -package com.diligrp.xtrade.gateway.filters.global ; | 1 | +package com.diligrp.xtrade.core.filters.global; |
2 | 2 | ||
3 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | +import com.diligrp.xtrade.core.support.dispatch.MappingRegister; | ||
4 | import org.reactivestreams.Publisher; | 5 | import org.reactivestreams.Publisher; |
6 | +import org.slf4j.Logger; | ||
7 | +import org.slf4j.LoggerFactory; | ||
5 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 8 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
6 | import org.springframework.cloud.gateway.filter.GlobalFilter; | 9 | import org.springframework.cloud.gateway.filter.GlobalFilter; |
7 | import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; | 10 | import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; |
@@ -27,16 +30,18 @@ import java.util.List; | @@ -27,16 +30,18 @@ import java.util.List; | ||
27 | import java.util.Objects; | 30 | import java.util.Objects; |
28 | import java.util.function.BiFunction; | 31 | import java.util.function.BiFunction; |
29 | 32 | ||
33 | +import static com.diligrp.xtrade.core.common.constant.GatewayConst.CACHED_RESPONSE_BODY_STR_ATTR; | ||
30 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; | 34 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; |
31 | 35 | ||
32 | -/* | 36 | +/** |
33 | * @auther: miaoguoxin | 37 | * @auther: miaoguoxin |
34 | * @date: 2018/12/26 21:26 | 38 | * @date: 2018/12/26 21:26 |
35 | * @description: 读取响应体过滤器 | 39 | * @description: 读取响应体过滤器 |
36 | */ | 40 | */ |
37 | @Component | 41 | @Component |
38 | -@Slf4j | ||
39 | public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { | 42 | public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { |
43 | + private static final Logger log = LoggerFactory.getLogger(MappingRegister.class); | ||
44 | + | ||
40 | private final List<HttpMessageReader<?>> messageReaders; | 45 | private final List<HttpMessageReader<?>> messageReaders; |
41 | 46 | ||
42 | public ResponseReadBodyGlobalFilter() { | 47 | public ResponseReadBodyGlobalFilter() { |
@@ -78,11 +83,7 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { | @@ -78,11 +83,7 @@ public class ResponseReadBodyGlobalFilter implements GlobalFilter, Ordered { | ||
78 | //打印返回响应日志 | 83 | //打印返回响应日志 |
79 | log.debug("[Trace:{}]-gateway response:ct=[{}], status=[{}],headers=[{}],body=[{}]", | 84 | log.debug("[Trace:{}]-gateway response:ct=[{}], status=[{}],headers=[{}],body=[{}]", |
80 | trace, System.currentTimeMillis() - startTime, resp.getStatusCode(), resp.getHeaders(), respBody); | 85 | trace, System.currentTimeMillis() - startTime, resp.getStatusCode(), resp.getHeaders(), respBody); |
81 | -// String errRespString = this.getErrRespString(resp.getStatusCode()); | ||
82 | -// if (!Strings.isNullOrEmpty(errRespString)) { | ||
83 | -// resp.setStatusCode(HttpStatus.OK); | ||
84 | -// respBody = errRespString; | ||
85 | -// } | 86 | + exchange.getAttributes().putIfAbsent(CACHED_RESPONSE_BODY_STR_ATTR,respBody); |
86 | HttpHeaders headers = resp.getHeaders(); | 87 | HttpHeaders headers = resp.getHeaders(); |
87 | //特别声明:响应体改变必须设置contentLength,且长度要保持一致,(经过测试,如果过短则会截断,过长则会导致超时。) | 88 | //特别声明:响应体改变必须设置contentLength,且长度要保持一致,(经过测试,如果过短则会截断,过长则会导致超时。) |
88 | headers.setContentLength(respBody.getBytes(StandardCharsets.UTF_8).length); | 89 | headers.setContentLength(respBody.getBytes(StandardCharsets.UTF_8).length); |
src/main/java/com/diligrp/xtrade/gateway/filters/web/ReactiveContextWebFilter.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/web/ReactiveContextWebFilter.java
1 | -package com.diligrp.xtrade.gateway.filters.web; | 1 | +package com.diligrp.xtrade.core.filters.web; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.support.context.ReactiveExchangeContextHolder; | ||
4 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | +import com.diligrp.xtrade.core.support.ApiMetricsCollector; | ||
5 | +import com.diligrp.xtrade.core.support.ApiMetricsInfo; | ||
6 | +import com.diligrp.xtrade.core.support.context.ReactiveExchangeContextHolder; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
6 | import org.springframework.http.server.reactive.ServerHttpRequest; | 10 | import org.springframework.http.server.reactive.ServerHttpRequest; |
7 | import org.springframework.stereotype.Component; | 11 | import org.springframework.stereotype.Component; |
@@ -10,6 +14,7 @@ import org.springframework.web.server.WebFilter; | @@ -10,6 +14,7 @@ import org.springframework.web.server.WebFilter; | ||
10 | import org.springframework.web.server.WebFilterChain; | 14 | import org.springframework.web.server.WebFilterChain; |
11 | import reactor.core.publisher.Mono; | 15 | import reactor.core.publisher.Mono; |
12 | 16 | ||
17 | + | ||
13 | /** | 18 | /** |
14 | * @Auther: miaoguoxin | 19 | * @Auther: miaoguoxin |
15 | * @Date: 2020/4/16 09:17 | 20 | * @Date: 2020/4/16 09:17 |
@@ -17,11 +22,12 @@ import reactor.core.publisher.Mono; | @@ -17,11 +22,12 @@ import reactor.core.publisher.Mono; | ||
17 | */ | 22 | */ |
18 | @Component | 23 | @Component |
19 | @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) | 24 | @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) |
20 | -@Slf4j | ||
21 | public class ReactiveContextWebFilter implements WebFilter { | 25 | public class ReactiveContextWebFilter implements WebFilter { |
26 | + private static final Logger log = LoggerFactory.getLogger(ReactiveContextWebFilter.class); | ||
27 | + | ||
22 | @Override | 28 | @Override |
23 | public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | 29 | public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
24 | - long l = System.currentTimeMillis(); | 30 | + long startTime = System.currentTimeMillis(); |
25 | ServerHttpRequest request = exchange.getRequest(); | 31 | ServerHttpRequest request = exchange.getRequest(); |
26 | String path = request.getURI().getRawPath(); | 32 | String path = request.getURI().getRawPath(); |
27 | if (path.contains("favicon.ico")) { | 33 | if (path.contains("favicon.ico")) { |
@@ -30,7 +36,16 @@ public class ReactiveContextWebFilter implements WebFilter { | @@ -30,7 +36,16 @@ public class ReactiveContextWebFilter implements WebFilter { | ||
30 | ReactiveExchangeContextHolder.put(exchange); | 36 | ReactiveExchangeContextHolder.put(exchange); |
31 | return chain.filter(exchange).doFinally(signalType -> { | 37 | return chain.filter(exchange).doFinally(signalType -> { |
32 | ReactiveExchangeContextHolder.remove(); | 38 | ReactiveExchangeContextHolder.remove(); |
33 | - log.info("消耗时间:{}",System.currentTimeMillis()-l); | 39 | + long endTime = System.currentTimeMillis() - startTime; |
40 | + log.info("消耗时间:{}", endTime); | ||
41 | + ApiMetricsInfo apiMetricsInfo = ApiMetricsInfo.build(exchange, endTime); | ||
42 | + ApiMetricsCollector.addMetricsInfo(apiMetricsInfo); | ||
43 | + Throwable throwable = (Throwable) exchange.getAttributes().get(GatewayConst.GATEWAY_EXCEPTION_ATTR); | ||
44 | + if (throwable != null) { | ||
45 | + //log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", errorAttrs.get("path"),errorAttributes.getError(request)); | ||
46 | + log.error("error:{}", throwable); | ||
47 | + } | ||
34 | }); | 48 | }); |
35 | } | 49 | } |
50 | + | ||
36 | } | 51 | } |
src/main/java/com/diligrp/xtrade/gateway/predicates/ReadBodyRoutePredicateFactory.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/predicates/ReadBodyRoutePredicateFactory.java
1 | -package com.diligrp.xtrade.gateway.predicates; | 1 | +package com.diligrp.xtrade.core.predicates; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.filters.global.CacheRequestBodyGlobalFilter; | ||
4 | -import lombok.Data; | ||
5 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | +import com.diligrp.xtrade.core.filters.global.CacheRequestBodyGlobalFilter; | ||
5 | +import org.slf4j.Logger; | ||
6 | +import org.slf4j.LoggerFactory; | ||
6 | import org.springframework.cloud.gateway.handler.AsyncPredicate; | 7 | import org.springframework.cloud.gateway.handler.AsyncPredicate; |
7 | import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory; | 8 | import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory; |
8 | import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | 9 | import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; |
@@ -15,10 +16,8 @@ import org.springframework.web.server.ServerWebExchange; | @@ -15,10 +16,8 @@ import org.springframework.web.server.ServerWebExchange; | ||
15 | import reactor.core.publisher.Mono; | 16 | import reactor.core.publisher.Mono; |
16 | 17 | ||
17 | import java.util.List; | 18 | import java.util.List; |
18 | -import java.util.function.Consumer; | ||
19 | import java.util.function.Predicate; | 19 | import java.util.function.Predicate; |
20 | 20 | ||
21 | -import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQUEST_BODY_OBJECT_KEY; | ||
22 | 21 | ||
23 | 22 | ||
24 | /** | 23 | /** |
@@ -27,10 +26,10 @@ import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQU | @@ -27,10 +26,10 @@ import static com.diligrp.xtrade.gateway.common.constant.GatewayConst.CACHE_REQU | ||
27 | * @Description: 缓存请求体数据predicate | 26 | * @Description: 缓存请求体数据predicate |
28 | * {@link CacheRequestBodyGlobalFilter} | 27 | * {@link CacheRequestBodyGlobalFilter} |
29 | */ | 28 | */ |
30 | -@Slf4j | ||
31 | -//@Component | ||
32 | @Deprecated | 29 | @Deprecated |
33 | public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory<ReadBodyRoutePredicateFactory.Config> { | 30 | public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory<ReadBodyRoutePredicateFactory.Config> { |
31 | + private static final Logger log = LoggerFactory.getLogger(ReadBodyRoutePredicateFactory.class); | ||
32 | + | ||
34 | private static final List<HttpMessageReader<?>> messageReaders = HandlerStrategies | 33 | private static final List<HttpMessageReader<?>> messageReaders = HandlerStrategies |
35 | .withDefaults().messageReaders(); | 34 | .withDefaults().messageReaders(); |
36 | 35 | ||
@@ -70,16 +69,22 @@ public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory | @@ -70,16 +69,22 @@ public class ReadBodyRoutePredicateFactory extends AbstractRoutePredicateFactory | ||
70 | .create(exchange.mutate().request(serverHttpRequest) | 69 | .create(exchange.mutate().request(serverHttpRequest) |
71 | .build(), messageReaders) | 70 | .build(), messageReaders) |
72 | .bodyToMono(String.class) | 71 | .bodyToMono(String.class) |
73 | - .doOnNext(objectValue -> exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, objectValue)) | 72 | + .doOnNext(objectValue -> exchange.getAttributes().put(GatewayConst.CACHED_REQUEST_BODY_STR_ATTR, objectValue)) |
74 | .map(objectValue -> Boolean.TRUE)); | 73 | .map(objectValue -> Boolean.TRUE)); |
75 | }; | 74 | }; |
76 | } | 75 | } |
77 | 76 | ||
78 | - @Data | ||
79 | public static class Config { | 77 | public static class Config { |
80 | //需要跳过的uri | 78 | //需要跳过的uri |
81 | private String shouldSkipUri; | 79 | private String shouldSkipUri; |
82 | 80 | ||
81 | + public String getShouldSkipUri() { | ||
82 | + return shouldSkipUri; | ||
83 | + } | ||
84 | + | ||
85 | + public void setShouldSkipUri(String shouldSkipUri) { | ||
86 | + this.shouldSkipUri = shouldSkipUri; | ||
87 | + } | ||
83 | } | 88 | } |
84 | 89 | ||
85 | } | 90 | } |
src/main/java/com/diligrp/xtrade/gateway/repository/GatewayRepository.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/repository/GatewayRepository.java
1 | -package com.diligrp.xtrade.gateway.repository; | 1 | +package com.diligrp.xtrade.core.repository; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.common.constant.GatewayAttrType; | ||
4 | -import com.diligrp.xtrade.gateway.exception.GatewayServiceException; | ||
5 | -import com.diligrp.xtrade.gateway.repository.dao.AttrConfigDao; | ||
6 | -import com.diligrp.xtrade.gateway.repository.dao.RouteDao; | ||
7 | -import com.diligrp.xtrade.gateway.repository.entity.GatewayAttrConfig; | ||
8 | -import com.diligrp.xtrade.gateway.repository.entity.GatewayConfig; | 3 | +import com.diligrp.xtrade.core.common.constant.GatewayAttrType; |
4 | +import com.diligrp.xtrade.core.exception.GatewayRouteResourceException; | ||
5 | +import com.diligrp.xtrade.core.repository.dao.AttrConfigDao; | ||
6 | +import com.diligrp.xtrade.core.repository.dao.RouteDao; | ||
7 | +import com.diligrp.xtrade.core.repository.entity.GatewayAttrConfig; | ||
8 | +import com.diligrp.xtrade.core.repository.entity.GatewayConfig; | ||
9 | import com.diligrp.xtrade.shared.util.JsonUtils; | 9 | import com.diligrp.xtrade.shared.util.JsonUtils; |
10 | import com.diligrp.xtrade.shared.util.ReflectUtils; | 10 | import com.diligrp.xtrade.shared.util.ReflectUtils; |
11 | import com.fasterxml.jackson.core.type.TypeReference; | 11 | import com.fasterxml.jackson.core.type.TypeReference; |
@@ -70,7 +70,7 @@ public class GatewayRepository implements IGatewayRepository { | @@ -70,7 +70,7 @@ public class GatewayRepository implements IGatewayRepository { | ||
70 | } | 70 | } |
71 | definition.setUri(uri); | 71 | definition.setUri(uri); |
72 | } catch (URISyntaxException e) { | 72 | } catch (URISyntaxException e) { |
73 | - throw new GatewayServiceException("get url failed",e); | 73 | + throw new GatewayRouteResourceException("get url failed",e); |
74 | } | 74 | } |
75 | //设置predicate和filter | 75 | //设置predicate和filter |
76 | this.setPredicatesAndFilters(gatewayConfig, definition); | 76 | this.setPredicatesAndFilters(gatewayConfig, definition); |
@@ -80,7 +80,7 @@ public class GatewayRepository implements IGatewayRepository { | @@ -80,7 +80,7 @@ public class GatewayRepository implements IGatewayRepository { | ||
80 | private void setPredicatesAndFilters(GatewayConfig gatewayConfig, RouteDefinition definition) { | 80 | private void setPredicatesAndFilters(GatewayConfig gatewayConfig, RouteDefinition definition) { |
81 | List<GatewayAttrConfig> gatewayAttrConfigs = gatewayConfig.getAttrConfigs(); | 81 | List<GatewayAttrConfig> gatewayAttrConfigs = gatewayConfig.getAttrConfigs(); |
82 | if (gatewayAttrConfigs.isEmpty()) { | 82 | if (gatewayAttrConfigs.isEmpty()) { |
83 | - throw new GatewayServiceException("gateway attrs can not allow empty, please check"); | 83 | + throw new GatewayRouteResourceException("gateway attrs can not allow empty, please check"); |
84 | } | 84 | } |
85 | List<PredicateDefinition> predicateDefinitions = new ArrayList<>(); | 85 | List<PredicateDefinition> predicateDefinitions = new ArrayList<>(); |
86 | List<FilterDefinition> filterDefinitions = new ArrayList<>(); | 86 | List<FilterDefinition> filterDefinitions = new ArrayList<>(); |
@@ -122,7 +122,7 @@ public class GatewayRepository implements IGatewayRepository { | @@ -122,7 +122,7 @@ public class GatewayRepository implements IGatewayRepository { | ||
122 | | InvocationTargetException | 122 | | InvocationTargetException |
123 | | InstantiationException | 123 | | InstantiationException |
124 | |NoSuchMethodException e) { | 124 | |NoSuchMethodException e) { |
125 | - throw new GatewayServiceException("init gateway resource failed", | 125 | + throw new GatewayRouteResourceException("init gateway resource failed", |
126 | e, | 126 | e, |
127 | new Object[]{JsonUtils.toJsonString(gatewayAttrConfig)}); | 127 | new Object[]{JsonUtils.toJsonString(gatewayAttrConfig)}); |
128 | } | 128 | } |
src/main/java/com/diligrp/xtrade/gateway/repository/IGatewayRepository.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/repository/IGatewayRepository.java
src/main/java/com/diligrp/xtrade/gateway/repository/dao/AttrConfigDao.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/repository/dao/AttrConfigDao.java
1 | -package com.diligrp.xtrade.gateway.repository.dao; | 1 | +package com.diligrp.xtrade.core.repository.dao; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.repository.entity.GatewayAttrConfig; | 3 | + |
4 | +import com.diligrp.xtrade.core.repository.entity.GatewayAttrConfig; | ||
4 | 5 | ||
5 | import java.util.List; | 6 | import java.util.List; |
6 | 7 |
src/main/java/com/diligrp/xtrade/gateway/repository/dao/RouteDao.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/repository/dao/RouteDao.java
1 | -package com.diligrp.xtrade.gateway.repository.dao; | 1 | +package com.diligrp.xtrade.core.repository.dao; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.repository.entity.GatewayConfig; | 3 | + |
4 | +import com.diligrp.xtrade.core.repository.entity.GatewayConfig; | ||
4 | 5 | ||
5 | import java.util.List; | 6 | import java.util.List; |
6 | 7 |
gateway-core/src/main/java/com/diligrp/xtrade/core/repository/entity/ApiMetricsInfoEntity.java
0 → 100644
gateway-core/src/main/java/com/diligrp/xtrade/core/repository/entity/GatewayAttrConfig.java
0 → 100644
1 | +package com.diligrp.xtrade.core.repository.entity; | ||
2 | + | ||
3 | +import com.diligrp.xtrade.core.common.constant.GatewayAttrType; | ||
4 | +import com.diligrp.xtrade.shared.domain.BaseDo; | ||
5 | + | ||
6 | +/** | ||
7 | + * @Auther: miaoguoxin | ||
8 | + * @Date: 2018/12/12 0012 17:43 | ||
9 | + * @Description: 网关断言和过滤器配置实体 | ||
10 | + */ | ||
11 | +public class GatewayAttrConfig extends BaseDo{ | ||
12 | + /**服务id名称*/ | ||
13 | + private String serviceId; | ||
14 | + /**类型 {@link GatewayAttrType}*/ | ||
15 | + private Integer type; | ||
16 | + /**predicate或者filter的名称*/ | ||
17 | + private String attrName; | ||
18 | + /** predicate或者filter所需的参数 json格式*/ | ||
19 | + private String attrArgs; | ||
20 | + /**描述*/ | ||
21 | + private String description; | ||
22 | + /**排序*/ | ||
23 | + private Integer sortOrder; | ||
24 | + | ||
25 | + public String getServiceId() { | ||
26 | + return serviceId; | ||
27 | + } | ||
28 | + | ||
29 | + public void setServiceId(String serviceId) { | ||
30 | + this.serviceId = serviceId; | ||
31 | + } | ||
32 | + | ||
33 | + public Integer getType() { | ||
34 | + return type; | ||
35 | + } | ||
36 | + | ||
37 | + public void setType(Integer type) { | ||
38 | + this.type = type; | ||
39 | + } | ||
40 | + | ||
41 | + public String getAttrName() { | ||
42 | + return attrName; | ||
43 | + } | ||
44 | + | ||
45 | + public void setAttrName(String attrName) { | ||
46 | + this.attrName = attrName; | ||
47 | + } | ||
48 | + | ||
49 | + public String getAttrArgs() { | ||
50 | + return attrArgs; | ||
51 | + } | ||
52 | + | ||
53 | + public void setAttrArgs(String attrArgs) { | ||
54 | + this.attrArgs = attrArgs; | ||
55 | + } | ||
56 | + | ||
57 | + public String getDescription() { | ||
58 | + return description; | ||
59 | + } | ||
60 | + | ||
61 | + public void setDescription(String description) { | ||
62 | + this.description = description; | ||
63 | + } | ||
64 | + | ||
65 | + public Integer getSortOrder() { | ||
66 | + return sortOrder; | ||
67 | + } | ||
68 | + | ||
69 | + public void setSortOrder(Integer sortOrder) { | ||
70 | + this.sortOrder = sortOrder; | ||
71 | + } | ||
72 | + | ||
73 | +} |
src/main/java/com/diligrp/xtrade/gateway/repository/entity/GatewayConfig.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/repository/entity/GatewayConfig.java
1 | -package com.diligrp.xtrade.gateway.repository.entity; | 1 | +package com.diligrp.xtrade.core.repository.entity; |
2 | 2 | ||
3 | import com.diligrp.xtrade.shared.domain.BaseDo; | 3 | import com.diligrp.xtrade.shared.domain.BaseDo; |
4 | -import lombok.Data; | ||
5 | -import lombok.EqualsAndHashCode; | ||
6 | 4 | ||
7 | import java.util.List; | 5 | import java.util.List; |
8 | 6 | ||
@@ -11,8 +9,6 @@ import java.util.List; | @@ -11,8 +9,6 @@ import java.util.List; | ||
11 | * @Date: 2018/12/11 0011 14:53 | 9 | * @Date: 2018/12/11 0011 14:53 |
12 | * @Description: 路由配置实体 | 10 | * @Description: 路由配置实体 |
13 | */ | 11 | */ |
14 | -@EqualsAndHashCode(callSuper = true) | ||
15 | -@Data | ||
16 | public class GatewayConfig extends BaseDo { | 12 | public class GatewayConfig extends BaseDo { |
17 | /**服务id名称*/ | 13 | /**服务id名称*/ |
18 | private String serviceId; | 14 | private String serviceId; |
@@ -24,4 +20,46 @@ public class GatewayConfig extends BaseDo { | @@ -24,4 +20,46 @@ public class GatewayConfig extends BaseDo { | ||
24 | private String description; | 20 | private String description; |
25 | /**包含predicate和filter*/ | 21 | /**包含predicate和filter*/ |
26 | private List<GatewayAttrConfig> attrConfigs; | 22 | private List<GatewayAttrConfig> attrConfigs; |
23 | + | ||
24 | + public String getServiceId() { | ||
25 | + return serviceId; | ||
26 | + } | ||
27 | + | ||
28 | + public void setServiceId(String serviceId) { | ||
29 | + this.serviceId = serviceId; | ||
30 | + } | ||
31 | + | ||
32 | + public String getUrl() { | ||
33 | + return url; | ||
34 | + } | ||
35 | + | ||
36 | + public void setUrl(String url) { | ||
37 | + this.url = url; | ||
38 | + } | ||
39 | + | ||
40 | + @Override | ||
41 | + public Integer getIsDel() { | ||
42 | + return isDel; | ||
43 | + } | ||
44 | + | ||
45 | + @Override | ||
46 | + public void setIsDel(Integer isDel) { | ||
47 | + this.isDel = isDel; | ||
48 | + } | ||
49 | + | ||
50 | + public String getDescription() { | ||
51 | + return description; | ||
52 | + } | ||
53 | + | ||
54 | + public void setDescription(String description) { | ||
55 | + this.description = description; | ||
56 | + } | ||
57 | + | ||
58 | + public List<GatewayAttrConfig> getAttrConfigs() { | ||
59 | + return attrConfigs; | ||
60 | + } | ||
61 | + | ||
62 | + public void setAttrConfigs(List<GatewayAttrConfig> attrConfigs) { | ||
63 | + this.attrConfigs = attrConfigs; | ||
64 | + } | ||
27 | } | 65 | } |
src/main/java/com/diligrp/xtrade/gateway/route/DynamicRouteLoaderIntf.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/route/DynamicRouteLoaderIntf.java
src/main/java/com/diligrp/xtrade/gateway/route/GatewayResourcesLoader.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/route/GatewayResourcesLoader.java
1 | -package com.diligrp.xtrade.gateway.route; | 1 | +package com.diligrp.xtrade.core.route; |
2 | 2 | ||
3 | import org.springframework.beans.factory.annotation.Autowired; | 3 | import org.springframework.beans.factory.annotation.Autowired; |
4 | import org.springframework.boot.ApplicationArguments; | 4 | import org.springframework.boot.ApplicationArguments; |
src/main/java/com/diligrp/xtrade/gateway/route/impl/CloudRouteResourceLoader.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/route/impl/CloudRouteResourceLoader.java
1 | -package com.diligrp.xtrade.gateway.route.impl; | 1 | +package com.diligrp.xtrade.core.route.impl; |
2 | 2 | ||
3 | import com.alibaba.nacos.api.config.ConfigService; | 3 | import com.alibaba.nacos.api.config.ConfigService; |
4 | import com.alibaba.nacos.api.config.listener.Listener; | 4 | import com.alibaba.nacos.api.config.listener.Listener; |
5 | import com.alibaba.nacos.api.exception.NacosException; | 5 | import com.alibaba.nacos.api.exception.NacosException; |
6 | -import com.diligrp.xtrade.gateway.config.GatewayResourceLoaderConfiguration; | ||
7 | -import com.diligrp.xtrade.gateway.exception.GatewayServiceException; | ||
8 | -import com.diligrp.xtrade.gateway.route.DynamicRouteLoaderIntf; | ||
9 | -import lombok.extern.slf4j.Slf4j; | 6 | +import com.diligrp.xtrade.core.config.GatewayResourceLoaderConfiguration; |
7 | +import com.diligrp.xtrade.core.exception.GatewayRouteResourceException; | ||
8 | +import com.diligrp.xtrade.core.route.DynamicRouteLoaderIntf; | ||
9 | +import org.slf4j.Logger; | ||
10 | +import org.slf4j.LoggerFactory; | ||
10 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
11 | import org.springframework.beans.factory.annotation.Value; | 12 | import org.springframework.beans.factory.annotation.Value; |
12 | import org.springframework.cloud.gateway.config.GatewayProperties; | 13 | import org.springframework.cloud.gateway.config.GatewayProperties; |
@@ -29,8 +30,9 @@ import java.util.concurrent.atomic.AtomicBoolean; | @@ -29,8 +30,9 @@ import java.util.concurrent.atomic.AtomicBoolean; | ||
29 | * @Description: 从云端配置中心加载 | 30 | * @Description: 从云端配置中心加载 |
30 | * {@link GatewayResourceLoaderConfiguration} 配置 | 31 | * {@link GatewayResourceLoaderConfiguration} 配置 |
31 | */ | 32 | */ |
32 | -@Slf4j | ||
33 | public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf { | 33 | public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf { |
34 | + private static final Logger log = LoggerFactory.getLogger(CloudRouteResourceLoader.class); | ||
35 | + | ||
34 | /**标记当前gatewayproties的hash值,用于判断配置是否更新过*/ | 36 | /**标记当前gatewayproties的hash值,用于判断配置是否更新过*/ |
35 | private static int gatewayPropertiesHash; | 37 | private static int gatewayPropertiesHash; |
36 | /**防止重复初始化的标记*/ | 38 | /**防止重复初始化的标记*/ |
@@ -108,7 +110,7 @@ public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf { | @@ -108,7 +110,7 @@ public class CloudRouteResourceLoader implements DynamicRouteLoaderIntf { | ||
108 | } | 110 | } |
109 | }); | 111 | }); |
110 | } catch (NacosException e) { | 112 | } catch (NacosException e) { |
111 | - throw new GatewayServiceException("load route resource failed", e); | 113 | + throw new GatewayRouteResourceException("load route resource failed", e); |
112 | } | 114 | } |
113 | } | 115 | } |
114 | 116 |
src/main/java/com/diligrp/xtrade/gateway/route/impl/MysqlRouteResourceLoader.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/route/impl/MysqlRouteResourceLoader.java
1 | -package com.diligrp.xtrade.gateway.route.impl; | 1 | +package com.diligrp.xtrade.core.route.impl; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.config.GatewayResourceLoaderConfiguration; | ||
4 | -import com.diligrp.xtrade.gateway.repository.IGatewayRepository; | ||
5 | -import com.diligrp.xtrade.gateway.route.DynamicRouteLoaderIntf; | ||
6 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.config.GatewayResourceLoaderConfiguration; |
4 | +import com.diligrp.xtrade.core.repository.IGatewayRepository; | ||
5 | +import com.diligrp.xtrade.core.route.DynamicRouteLoaderIntf; | ||
6 | +import org.slf4j.Logger; | ||
7 | +import org.slf4j.LoggerFactory; | ||
7 | import org.springframework.beans.factory.annotation.Autowired; | 8 | import org.springframework.beans.factory.annotation.Autowired; |
8 | import org.springframework.cloud.gateway.event.RefreshRoutesEvent; | 9 | import org.springframework.cloud.gateway.event.RefreshRoutesEvent; |
9 | import org.springframework.cloud.gateway.route.RouteDefinition; | 10 | import org.springframework.cloud.gateway.route.RouteDefinition; |
@@ -20,8 +21,8 @@ import java.util.List; | @@ -20,8 +21,8 @@ import java.util.List; | ||
20 | * @Description: 从数据库加载 | 21 | * @Description: 从数据库加载 |
21 | * 通过 {@link GatewayResourceLoaderConfiguration} 配置 | 22 | * 通过 {@link GatewayResourceLoaderConfiguration} 配置 |
22 | */ | 23 | */ |
23 | -@Slf4j | ||
24 | public class MysqlRouteResourceLoader implements DynamicRouteLoaderIntf { | 24 | public class MysqlRouteResourceLoader implements DynamicRouteLoaderIntf { |
25 | + private static final Logger log = LoggerFactory.getLogger(MysqlRouteResourceLoader.class); | ||
25 | @Autowired | 26 | @Autowired |
26 | private ApplicationContext applicationContext; | 27 | private ApplicationContext applicationContext; |
27 | @Autowired | 28 | @Autowired |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/ApiMetricsCollector.java
0 → 100644
1 | +package com.diligrp.xtrade.core.support; | ||
2 | + | ||
3 | +import com.diligrp.xtrade.core.route.impl.CloudRouteResourceLoader; | ||
4 | +import com.diligrp.xtrade.shared.util.JsonUtils; | ||
5 | +import com.google.common.collect.Queues; | ||
6 | +import org.slf4j.Logger; | ||
7 | +import org.slf4j.LoggerFactory; | ||
8 | +import org.springframework.stereotype.Component; | ||
9 | + | ||
10 | +import javax.annotation.PostConstruct; | ||
11 | +import java.util.ArrayList; | ||
12 | +import java.util.List; | ||
13 | +import java.util.Timer; | ||
14 | +import java.util.TimerTask; | ||
15 | +import java.util.concurrent.ArrayBlockingQueue; | ||
16 | +import java.util.concurrent.BlockingQueue; | ||
17 | +import java.util.concurrent.TimeUnit; | ||
18 | + | ||
19 | +/** | ||
20 | + * @Auther: miaoguoxin | ||
21 | + * @Date: 2020/4/18 16:19 | ||
22 | + * @Description: 收集api记录 | ||
23 | + */ | ||
24 | +@Component | ||
25 | +public class ApiMetricsCollector { | ||
26 | + private static final Logger log = LoggerFactory.getLogger(ApiMetricsCollector.class); | ||
27 | + | ||
28 | + private static final BlockingQueue<ApiMetricsInfo> METRICS_QUEUE = new ArrayBlockingQueue<>(1000, true); | ||
29 | + | ||
30 | + public static void addMetricsInfo(ApiMetricsInfo apiMetricsInfo) { | ||
31 | + if (apiMetricsInfo != null) { | ||
32 | + METRICS_QUEUE.add(apiMetricsInfo); | ||
33 | + } | ||
34 | + } | ||
35 | + | ||
36 | + @PostConstruct | ||
37 | + public void start() { | ||
38 | + Timer timer = new Timer(); | ||
39 | + TimerTask task = new TimerTask() { | ||
40 | + @Override | ||
41 | + public void run() { | ||
42 | + log.info("api metrics collector starting"); | ||
43 | + startCollectMetrics(); | ||
44 | + } | ||
45 | + }; | ||
46 | + timer.schedule(task, 1000 * 5); | ||
47 | + | ||
48 | + } | ||
49 | + | ||
50 | + private void startCollectMetrics() { | ||
51 | + while (true) { | ||
52 | + List<ApiMetricsInfo> metricsInfos = new ArrayList<>(); | ||
53 | + //第一个:传入你需要批量消费的队列 | ||
54 | + //第二个:传入一个用来接收批量消费到的数据 | ||
55 | + //第三个:批量消费数据的大小,即意味着每次消费N条数据 | ||
56 | + //第四个:批量消费的等待的最大间隔,比如说,我先在队列中只有10条数据,它不到100条, | ||
57 | + // 那按道理就不会消费,但是这样显然不合理,所以需要指定当超多多长时间,即使当前队列中数据低于我们设定的阈值也会消费 | ||
58 | + //第五个,指定第四个参数的单位,是秒是分钟还是小时等等 | ||
59 | + try { | ||
60 | + Queues.drain(METRICS_QUEUE, metricsInfos, 50, 1, TimeUnit.MINUTES); | ||
61 | + for (ApiMetricsInfo metricsInfo : metricsInfos) { | ||
62 | + log.info("消费的metrics:{}", JsonUtils.toJsonString(metricsInfo)); | ||
63 | + } | ||
64 | + } catch (Exception e) { | ||
65 | + log.error("collect api metrics error:{}", e); | ||
66 | + } | ||
67 | + } | ||
68 | + } | ||
69 | +} |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/ApiMetricsInfo.java
0 → 100644
1 | +package com.diligrp.xtrade.core.support; | ||
2 | + | ||
3 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; | ||
4 | +import com.diligrp.xtrade.shared.domain.Message; | ||
5 | +import com.diligrp.xtrade.shared.util.JsonUtils; | ||
6 | +import com.fasterxml.jackson.core.type.TypeReference; | ||
7 | +import org.apache.logging.log4j.util.Strings; | ||
8 | +import org.springframework.cloud.gateway.route.Route; | ||
9 | +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||
10 | +import org.springframework.web.server.ServerWebExchange; | ||
11 | + | ||
12 | +import java.net.URI; | ||
13 | +import java.net.URISyntaxException; | ||
14 | +import java.time.LocalDateTime; | ||
15 | + | ||
16 | +/** | ||
17 | + * @Auther: miaoguoxin | ||
18 | + * @Date: 2020/4/18 20:41 | ||
19 | + * @Description: | ||
20 | + */ | ||
21 | +public class ApiMetricsInfo { | ||
22 | + /**响应code*/ | ||
23 | + private Integer code; | ||
24 | + /**响应消息*/ | ||
25 | + private String message; | ||
26 | + /**目标服务id*/ | ||
27 | + private String serviceId; | ||
28 | + /**目标地址*/ | ||
29 | + private String url; | ||
30 | + /**请求体*/ | ||
31 | + private String requestBody; | ||
32 | + /**异常栈跟踪Json信息*/ | ||
33 | + private String stacktrace; | ||
34 | + /**方法执行时间,单位:ms*/ | ||
35 | + private Long executeTime; | ||
36 | + /**创建时间*/ | ||
37 | + private LocalDateTime createdTime; | ||
38 | + | ||
39 | + public ApiMetricsInfo() { | ||
40 | + createdTime = LocalDateTime.now(); | ||
41 | + } | ||
42 | + | ||
43 | + public static ApiMetricsInfo build(ServerWebExchange exchange, long time) { | ||
44 | + try { | ||
45 | + String responseJson = exchange.getAttribute(GatewayConst.CACHED_RESPONSE_BODY_STR_ATTR); | ||
46 | + if (Strings.isNotBlank(responseJson)) { | ||
47 | + Message<Object> message = JsonUtils.fromJsonString(responseJson, new TypeReference<>() { | ||
48 | + }); | ||
49 | + URI uri = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR); | ||
50 | + Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); | ||
51 | + String requestBody = exchange.getAttribute(GatewayConst.CACHED_REQUEST_BODY_STR_ATTR); | ||
52 | + Throwable throwable = (Throwable) exchange.getAttributes().get(GatewayConst.GATEWAY_EXCEPTION_ATTR); | ||
53 | + ApiMetricsInfo info = new ApiMetricsInfo(); | ||
54 | + info.setCode(message.getCode()); | ||
55 | + info.setMessage(message.getMessage()); | ||
56 | + info.setUrl(uri != null ? uri.toString() : "unknown"); | ||
57 | + info.setServiceId(route != null ? route.getId() : "unknown"); | ||
58 | + info.setRequestBody(requestBody); | ||
59 | + info.setStacktrace(throwable != null ? JsonUtils.toJsonString(throwable.getStackTrace()) : null); | ||
60 | + info.setExecuteTime(time); | ||
61 | + return info; | ||
62 | + } | ||
63 | + return null; | ||
64 | + } catch (Exception e) { | ||
65 | + return null; | ||
66 | + } | ||
67 | + } | ||
68 | + | ||
69 | + public Long getExecuteTime() { | ||
70 | + return executeTime; | ||
71 | + } | ||
72 | + | ||
73 | + public void setExecuteTime(Long executeTime) { | ||
74 | + this.executeTime = executeTime; | ||
75 | + } | ||
76 | + | ||
77 | + public LocalDateTime getCreatedTime() { | ||
78 | + return createdTime; | ||
79 | + } | ||
80 | + | ||
81 | + public void setCreatedTime(LocalDateTime createdTime) { | ||
82 | + this.createdTime = createdTime; | ||
83 | + } | ||
84 | + | ||
85 | + public Integer getCode() { | ||
86 | + return code; | ||
87 | + } | ||
88 | + | ||
89 | + public void setCode(Integer code) { | ||
90 | + this.code = code; | ||
91 | + } | ||
92 | + | ||
93 | + public String getMessage() { | ||
94 | + return message; | ||
95 | + } | ||
96 | + | ||
97 | + public void setMessage(String message) { | ||
98 | + this.message = message; | ||
99 | + } | ||
100 | + | ||
101 | + public String getServiceId() { | ||
102 | + return serviceId; | ||
103 | + } | ||
104 | + | ||
105 | + public void setServiceId(String serviceId) { | ||
106 | + this.serviceId = serviceId; | ||
107 | + } | ||
108 | + | ||
109 | + public String getUrl() { | ||
110 | + return url; | ||
111 | + } | ||
112 | + | ||
113 | + public void setUrl(String url) { | ||
114 | + this.url = url; | ||
115 | + } | ||
116 | + | ||
117 | + public String getRequestBody() { | ||
118 | + return requestBody; | ||
119 | + } | ||
120 | + | ||
121 | + public void setRequestBody(String requestBody) { | ||
122 | + this.requestBody = requestBody; | ||
123 | + } | ||
124 | + | ||
125 | + public String getStacktrace() { | ||
126 | + return stacktrace; | ||
127 | + } | ||
128 | + | ||
129 | + public void setStacktrace(String stacktrace) { | ||
130 | + this.stacktrace = stacktrace; | ||
131 | + } | ||
132 | + | ||
133 | +} |
src/main/java/com/diligrp/xtrade/gateway/support/context/ReactiveExchangeContextHolder.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/context/ReactiveExchangeContextHolder.java
src/main/java/com/diligrp/xtrade/gateway/support/context/ReactiveWebContextBeanProcessor.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/context/ReactiveWebContextBeanProcessor.java
1 | -package com.diligrp.xtrade.gateway.support.context; | 1 | +package com.diligrp.xtrade.core.support.context; |
2 | 2 | ||
3 | import org.springframework.beans.BeansException; | 3 | import org.springframework.beans.BeansException; |
4 | import org.springframework.beans.factory.ObjectFactory; | 4 | import org.springframework.beans.factory.ObjectFactory; |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/DispatchContext.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/DispatchContext.java
1 | -package com.diligrp.xtrade.gateway.support.dispatch; | 1 | +package com.diligrp.xtrade.core.support.dispatch; |
2 | 2 | ||
3 | -import lombok.Getter; | ||
4 | import org.springframework.http.server.reactive.ServerHttpRequest; | 3 | import org.springframework.http.server.reactive.ServerHttpRequest; |
5 | import org.springframework.http.server.reactive.ServerHttpResponse; | 4 | import org.springframework.http.server.reactive.ServerHttpResponse; |
6 | 5 | ||
@@ -8,7 +7,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse; | @@ -8,7 +7,6 @@ import org.springframework.http.server.reactive.ServerHttpResponse; | ||
8 | * @Auther: miaoguoxin | 7 | * @Auther: miaoguoxin |
9 | * @Date: 2020/4/16 09:20 | 8 | * @Date: 2020/4/16 09:20 |
10 | */ | 9 | */ |
11 | -@Getter | ||
12 | public class DispatchContext<T> { | 10 | public class DispatchContext<T> { |
13 | private final ServerHttpRequest request; | 11 | private final ServerHttpRequest request; |
14 | private final ServerHttpResponse response; | 12 | private final ServerHttpResponse response; |
@@ -21,4 +19,16 @@ public class DispatchContext<T> { | @@ -21,4 +19,16 @@ public class DispatchContext<T> { | ||
21 | this.param = param; | 19 | this.param = param; |
22 | } | 20 | } |
23 | 21 | ||
22 | + public ServerHttpRequest getRequest() { | ||
23 | + return request; | ||
24 | + } | ||
25 | + | ||
26 | + public ServerHttpResponse getResponse() { | ||
27 | + return response; | ||
28 | + } | ||
29 | + | ||
30 | + public T getParam() { | ||
31 | + return param; | ||
32 | + } | ||
24 | } | 33 | } |
34 | + |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/MappingMetaInfo.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/MappingMetaInfo.java
1 | -package com.diligrp.xtrade.gateway.support.dispatch; | 1 | +package com.diligrp.xtrade.core.support.dispatch; |
2 | 2 | ||
3 | -import lombok.Getter; | ||
4 | import org.springframework.validation.annotation.Validated; | 3 | import org.springframework.validation.annotation.Validated; |
5 | 4 | ||
6 | import java.lang.reflect.Method; | 5 | import java.lang.reflect.Method; |
@@ -9,21 +8,15 @@ import java.lang.reflect.Method; | @@ -9,21 +8,15 @@ import java.lang.reflect.Method; | ||
9 | * @Auther: miaoguoxin | 8 | * @Auther: miaoguoxin |
10 | * @Date: 2020/4/15 09:57 | 9 | * @Date: 2020/4/15 09:57 |
11 | */ | 10 | */ |
12 | -@Getter | ||
13 | public class MappingMetaInfo { | 11 | public class MappingMetaInfo { |
14 | /**方法所属的bean class*/ | 12 | /**方法所属的bean class*/ |
15 | private final Class<?> targetClass; | 13 | private final Class<?> targetClass; |
16 | /** 需要执行的方法 */ | 14 | /** 需要执行的方法 */ |
17 | private final Method targetMethod; | 15 | private final Method targetMethod; |
18 | /***/ | 16 | /***/ |
19 | - private Validated validated; | 17 | + private final Validated validated; |
20 | /**泛化参数类型 {@link DispatchContext#getParam()}*/ | 18 | /**泛化参数类型 {@link DispatchContext#getParam()}*/ |
21 | - private Class<?> parameterType; | ||
22 | - | ||
23 | - public MappingMetaInfo(Class<?> targetBean, Method targetMethod) { | ||
24 | - this.targetClass = targetBean; | ||
25 | - this.targetMethod = targetMethod; | ||
26 | - } | 19 | + private final Class<?> parameterType; |
27 | 20 | ||
28 | public MappingMetaInfo(Class<?> targetClass, Method targetMethod, Validated validated, Class<?> parameterType) { | 21 | public MappingMetaInfo(Class<?> targetClass, Method targetMethod, Validated validated, Class<?> parameterType) { |
29 | this.targetClass = targetClass; | 22 | this.targetClass = targetClass; |
@@ -32,11 +25,30 @@ public class MappingMetaInfo { | @@ -32,11 +25,30 @@ public class MappingMetaInfo { | ||
32 | this.parameterType = parameterType; | 25 | this.parameterType = parameterType; |
33 | } | 26 | } |
34 | 27 | ||
28 | + | ||
35 | @Override | 29 | @Override |
36 | public String toString() { | 30 | public String toString() { |
37 | - return "MappingInfo{" + | ||
38 | - "targetBean=" + targetClass.getName() + | ||
39 | - ", targetMethod=" + targetMethod.getName() + | 31 | + return "MappingMetaInfo{" + |
32 | + "targetClass=" + targetClass + | ||
33 | + ", targetMethod=" + targetMethod + | ||
34 | + ", validated=" + validated + | ||
35 | + ", parameterType=" + parameterType + | ||
40 | '}'; | 36 | '}'; |
41 | } | 37 | } |
38 | + | ||
39 | + public Class<?> getTargetClass() { | ||
40 | + return targetClass; | ||
41 | + } | ||
42 | + | ||
43 | + public Method getTargetMethod() { | ||
44 | + return targetMethod; | ||
45 | + } | ||
46 | + | ||
47 | + public Validated getValidated() { | ||
48 | + return validated; | ||
49 | + } | ||
50 | + | ||
51 | + public Class<?> getParameterType() { | ||
52 | + return parameterType; | ||
53 | + } | ||
42 | } | 54 | } |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/MappingRegister.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/MappingRegister.java
1 | -package com.diligrp.xtrade.gateway.support.dispatch; | 1 | +package com.diligrp.xtrade.core.support.dispatch; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.XtradeGatewayApplication; | ||
4 | -import com.diligrp.xtrade.gateway.common.annotation.DispatchMapping; | ||
5 | -import com.diligrp.xtrade.gateway.common.constant.GatewayConst; | ||
6 | -import com.diligrp.xtrade.gateway.common.utils.PathUtils; | ||
7 | -import com.diligrp.xtrade.gateway.config.property.DispatchProperties; | ||
8 | -import lombok.extern.slf4j.Slf4j; | 3 | +import com.diligrp.xtrade.core.common.annotation.DispatchMapping; |
4 | +import com.diligrp.xtrade.core.common.constant.GatewayConst; | ||
5 | +import com.diligrp.xtrade.core.common.utils.PathUtils; | ||
6 | +import com.diligrp.xtrade.core.config.property.DispatchProperties; | ||
7 | +import com.diligrp.xtrade.core.exception.GatewayDispatchException; | ||
9 | import org.reflections.Reflections; | 8 | import org.reflections.Reflections; |
10 | import org.reflections.scanners.FieldAnnotationsScanner; | 9 | import org.reflections.scanners.FieldAnnotationsScanner; |
11 | import org.reflections.scanners.MethodAnnotationsScanner; | 10 | import org.reflections.scanners.MethodAnnotationsScanner; |
12 | import org.reflections.scanners.SubTypesScanner; | 11 | import org.reflections.scanners.SubTypesScanner; |
13 | import org.reflections.util.ConfigurationBuilder; | 12 | import org.reflections.util.ConfigurationBuilder; |
13 | +import org.slf4j.Logger; | ||
14 | +import org.slf4j.LoggerFactory; | ||
14 | import org.springframework.beans.factory.annotation.Autowired; | 15 | import org.springframework.beans.factory.annotation.Autowired; |
15 | import org.springframework.boot.SpringApplication; | 16 | import org.springframework.boot.SpringApplication; |
16 | import org.springframework.context.ApplicationContext; | 17 | import org.springframework.context.ApplicationContext; |
@@ -31,8 +32,9 @@ import java.util.Set; | @@ -31,8 +32,9 @@ import java.util.Set; | ||
31 | * @Description: 注册聚合服务方法映射 | 32 | * @Description: 注册聚合服务方法映射 |
32 | */ | 33 | */ |
33 | @Component | 34 | @Component |
34 | -@Slf4j | ||
35 | public class MappingRegister implements ApplicationListener<ContextRefreshedEvent> { | 35 | public class MappingRegister implements ApplicationListener<ContextRefreshedEvent> { |
36 | + private static final Logger log = LoggerFactory.getLogger(MappingRegister.class); | ||
37 | + | ||
36 | @Autowired | 38 | @Autowired |
37 | private ApplicationContext applicationContext; | 39 | private ApplicationContext applicationContext; |
38 | @Autowired | 40 | @Autowired |
@@ -109,7 +111,7 @@ public class MappingRegister implements ApplicationListener<ContextRefreshedEven | @@ -109,7 +111,7 @@ public class MappingRegister implements ApplicationListener<ContextRefreshedEven | ||
109 | private String[] getScanPackages() { | 111 | private String[] getScanPackages() { |
110 | String[] scanPackages = dispatchProperties.getAggregationScanPackages(); | 112 | String[] scanPackages = dispatchProperties.getAggregationScanPackages(); |
111 | if (scanPackages == null || scanPackages.length == 0) { | 113 | if (scanPackages == null || scanPackages.length == 0) { |
112 | - return new String[]{XtradeGatewayApplication.class.getPackageName()}; | 114 | + throw new GatewayDispatchException("aggregation scan packages can not be empty"); |
113 | } | 115 | } |
114 | return scanPackages; | 116 | return scanPackages; |
115 | } | 117 | } |
src/main/java/com/diligrp/xtrade/gateway/support/dispatch/RequestDispatcher.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/RequestDispatcher.java
1 | -package com.diligrp.xtrade.gateway.support.dispatch; | 1 | +package com.diligrp.xtrade.core.support.dispatch; |
2 | 2 | ||
3 | -import com.diligrp.xtrade.gateway.common.utils.PathUtils; | ||
4 | -import com.diligrp.xtrade.gateway.common.utils.ValidateUtils; | ||
5 | -import com.diligrp.xtrade.gateway.exception.GatewayParamNotValidException; | ||
6 | -import com.diligrp.xtrade.gateway.exception.GatewayServiceException; | ||
7 | -import com.diligrp.xtrade.gateway.filters.factory.DispatchGatewayFilterFactory; | 3 | +import com.diligrp.xtrade.core.common.utils.PathUtils; |
4 | +import com.diligrp.xtrade.core.common.utils.ValidateUtils; | ||
5 | +import com.diligrp.xtrade.core.exception.GatewayDispatchException; | ||
6 | +import com.diligrp.xtrade.core.filters.factory.DispatchGatewayFilterFactory; | ||
8 | import com.diligrp.xtrade.shared.domain.Message; | 7 | import com.diligrp.xtrade.shared.domain.Message; |
9 | -import com.diligrp.xtrade.shared.type.ErrorCode; | ||
10 | import com.diligrp.xtrade.shared.util.JsonUtils; | 8 | import com.diligrp.xtrade.shared.util.JsonUtils; |
11 | import com.google.common.base.Strings; | 9 | import com.google.common.base.Strings; |
12 | -import lombok.extern.slf4j.Slf4j; | 10 | +import org.slf4j.Logger; |
11 | +import org.slf4j.LoggerFactory; | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | 12 | import org.springframework.beans.factory.annotation.Autowired; |
14 | import org.springframework.cloud.gateway.support.NotFoundException; | 13 | import org.springframework.cloud.gateway.support.NotFoundException; |
15 | import org.springframework.context.ApplicationContext; | 14 | import org.springframework.context.ApplicationContext; |
16 | import org.springframework.stereotype.Component; | 15 | import org.springframework.stereotype.Component; |
17 | import org.springframework.validation.annotation.Validated; | 16 | import org.springframework.validation.annotation.Validated; |
18 | -import org.springframework.web.server.ResponseStatusException; | ||
19 | import org.springframework.web.server.ServerWebExchange; | 17 | import org.springframework.web.server.ServerWebExchange; |
18 | +import org.springframework.web.server.ServerWebInputException; | ||
20 | import org.springframework.web.util.pattern.PathPattern; | 19 | import org.springframework.web.util.pattern.PathPattern; |
21 | 20 | ||
22 | import java.lang.reflect.InvocationTargetException; | 21 | import java.lang.reflect.InvocationTargetException; |
@@ -35,8 +34,8 @@ import java.util.concurrent.atomic.AtomicBoolean; | @@ -35,8 +34,8 @@ import java.util.concurrent.atomic.AtomicBoolean; | ||
35 | * {@link DispatchGatewayFilterFactory} | 34 | * {@link DispatchGatewayFilterFactory} |
36 | */ | 35 | */ |
37 | @Component | 36 | @Component |
38 | -@Slf4j | ||
39 | public class RequestDispatcher { | 37 | public class RequestDispatcher { |
38 | + private static final Logger log = LoggerFactory.getLogger(RequestDispatcher.class); | ||
40 | /** | 39 | /** |
41 | * key: uri value:{@link MappingMetaInfo} | 40 | * key: uri value:{@link MappingMetaInfo} |
42 | */ | 41 | */ |
@@ -69,7 +68,7 @@ public class RequestDispatcher { | @@ -69,7 +68,7 @@ public class RequestDispatcher { | ||
69 | Object resultData = this.beginDispatch(matchUri, paramsJson,exchange); | 68 | Object resultData = this.beginDispatch(matchUri, paramsJson,exchange); |
70 | return Message.success(resultData); | 69 | return Message.success(resultData); |
71 | } catch (Throwable e) { | 70 | } catch (Throwable e) { |
72 | - return handleException(e); | 71 | + throw new GatewayDispatchException("dispatch method error", e); |
73 | } | 72 | } |
74 | } | 73 | } |
75 | 74 | ||
@@ -79,9 +78,9 @@ public class RequestDispatcher { | @@ -79,9 +78,9 @@ public class RequestDispatcher { | ||
79 | * @date 2020/4/16 | 78 | * @date 2020/4/16 |
80 | */ | 79 | */ |
81 | public static void addMapping(String uri, MappingMetaInfo mappingMetaInfo) { | 80 | public static void addMapping(String uri, MappingMetaInfo mappingMetaInfo) { |
82 | - Optional.ofNullable(MAPPING_INFO_MAP.put(uri, mappingMetaInfo)) | 81 | + Optional.ofNullable(MAPPING_INFO_MAP.putIfAbsent(uri, mappingMetaInfo)) |
83 | .ifPresent(info -> { | 82 | .ifPresent(info -> { |
84 | - throw new GatewayServiceException( | 83 | + throw new GatewayDispatchException( |
85 | String.format("mapping:【%s】 duplicate:【%s】", uri, info.toString())); | 84 | String.format("mapping:【%s】 duplicate:【%s】", uri, info.toString())); |
86 | }); | 85 | }); |
87 | 86 | ||
@@ -100,7 +99,7 @@ public class RequestDispatcher { | @@ -100,7 +99,7 @@ public class RequestDispatcher { | ||
100 | Class<?> parameterType = mappingMetaInfo.getParameterType(); | 99 | Class<?> parameterType = mappingMetaInfo.getParameterType(); |
101 | Object paramData = null; | 100 | Object paramData = null; |
102 | if (parameterType != null){ | 101 | if (parameterType != null){ |
103 | - paramData = JsonUtils.fromJsonString(paramsJson, parameterType); | 102 | + paramData = this.getRequestParam(paramsJson, parameterType); |
104 | Validated validated = mappingMetaInfo.getValidated(); | 103 | Validated validated = mappingMetaInfo.getValidated(); |
105 | if (validated != null){ | 104 | if (validated != null){ |
106 | ValidateUtils.validate(paramData,validated.value()); | 105 | ValidateUtils.validate(paramData,validated.value()); |
@@ -114,6 +113,15 @@ public class RequestDispatcher { | @@ -114,6 +113,15 @@ public class RequestDispatcher { | ||
114 | return this.call(targetMethod, targetBean, dispatchContext); | 113 | return this.call(targetMethod, targetBean, dispatchContext); |
115 | } | 114 | } |
116 | 115 | ||
116 | + private Object getRequestParam(String paramsJson, Class<?> parameterType) { | ||
117 | + try { | ||
118 | + return JsonUtils.fromJsonString(paramsJson, parameterType); | ||
119 | + }catch (IllegalArgumentException ex){ | ||
120 | + log.error("paramsJson:{}\n parameterType:{}",paramsJson,parameterType); | ||
121 | + throw new GatewayDispatchException(new ServerWebInputException("illegal json")); | ||
122 | + } | ||
123 | + } | ||
124 | + | ||
117 | public Object call(Method method, Object target, Object params) throws Throwable { | 125 | public Object call(Method method, Object target, Object params) throws Throwable { |
118 | try { | 126 | try { |
119 | return method.invoke(target, params); | 127 | return method.invoke(target, params); |
@@ -122,20 +130,6 @@ public class RequestDispatcher { | @@ -122,20 +130,6 @@ public class RequestDispatcher { | ||
122 | } | 130 | } |
123 | } | 131 | } |
124 | 132 | ||
125 | - private static Message<Object> handleException(Throwable ex) { | ||
126 | - if (ex instanceof ResponseStatusException) { | ||
127 | - int statusCode = ((ResponseStatusException) ex).getStatus().value(); | ||
128 | - log.error("aggregation dispatch {}:{}", statusCode, ex.getMessage()); | ||
129 | - return Message.failure(statusCode,ex.getMessage()); | ||
130 | - } else if (ex instanceof GatewayParamNotValidException) { | ||
131 | - return Message.failure( | ||
132 | - ErrorCode.ILLEGAL_PARAMS.getCode(),ex.getMessage()); | ||
133 | - } else { | ||
134 | - log.error("aggregation dispatch error:{}", ex); | ||
135 | - return Message.failure( | ||
136 | - ErrorCode.UNKNOWN_ERROR.getCode(),ErrorCode.UNKNOWN_ERROR.getName()); | ||
137 | - } | ||
138 | - } | ||
139 | 133 | ||
140 | 134 | ||
141 | } | 135 | } |
src/main/resources/mapper/AttrConfigDao.xml renamed to gateway-core/src/main/resources/mapper/AttrConfigDao.xml
1 | <?xml version="1.0" encoding="UTF-8" ?> | 1 | <?xml version="1.0" encoding="UTF-8" ?> |
2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
3 | <mapper namespace="com.diligrp.xtrade.gateway.repository.dao.AttrConfigDao"> | 3 | <mapper namespace="com.diligrp.xtrade.gateway.repository.dao.AttrConfigDao"> |
4 | - <resultMap id="Base_Result_Map" type="com.diligrp.xtrade.gateway.repository.entity.GatewayAttrConfig"> | 4 | + <resultMap id="Base_Result_Map" type="com.diligrp.xtrade.core.repository.entity.GatewayAttrConfig"> |
5 | <id property="id" column="id"/> | 5 | <id property="id" column="id"/> |
6 | <result property="serviceId" column="service_id"/> | 6 | <result property="serviceId" column="service_id"/> |
7 | <result property="type" column="type"/> | 7 | <result property="type" column="type"/> |
src/main/resources/mapper/RouteDao.xml renamed to gateway-core/src/main/resources/mapper/RouteDao.xml
1 | <?xml version="1.0" encoding="UTF-8" ?> | 1 | <?xml version="1.0" encoding="UTF-8" ?> |
2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
3 | <mapper namespace="com.diligrp.xtrade.gateway.repository.dao.RouteDao"> | 3 | <mapper namespace="com.diligrp.xtrade.gateway.repository.dao.RouteDao"> |
4 | - <resultMap id="Base_Result_Map" type="com.diligrp.xtrade.gateway.repository.entity.GatewayConfig"> | 4 | + <resultMap id="Base_Result_Map" type="com.diligrp.xtrade.core.repository.entity.GatewayConfig"> |
5 | <id property="id" column="id"/> | 5 | <id property="id" column="id"/> |
6 | <result property="url" column="url"/> | 6 | <result property="url" column="url"/> |
7 | <result property="serviceId" column="service_id"/> | 7 | <result property="serviceId" column="service_id"/> |
pom.xml
@@ -2,12 +2,13 @@ | @@ -2,12 +2,13 @@ | ||
2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | 2 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | 3 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
4 | <modelVersion>4.0.0</modelVersion> | 4 | <modelVersion>4.0.0</modelVersion> |
5 | - <parent> | ||
6 | - <groupId>org.springframework.boot</groupId> | ||
7 | - <artifactId>spring-boot-starter-parent</artifactId> | ||
8 | - <version>2.2.5.RELEASE</version> | ||
9 | - <relativePath/> <!-- lookup parent from repository --> | ||
10 | - </parent> | 5 | + <packaging>pom</packaging> |
6 | + <modules> | ||
7 | + <module>gateway-core</module> | ||
8 | + <module>gateway-business</module> | ||
9 | + </modules> | ||
10 | + | ||
11 | + | ||
11 | <groupId>com.diligrp</groupId> | 12 | <groupId>com.diligrp</groupId> |
12 | <artifactId>xtrade-gateway-service</artifactId> | 13 | <artifactId>xtrade-gateway-service</artifactId> |
13 | <version>1.0.0</version> | 14 | <version>1.0.0</version> |
@@ -19,18 +20,16 @@ | @@ -19,18 +20,16 @@ | ||
19 | <spring-cloud.version>Hoxton.SR3</spring-cloud.version> | 20 | <spring-cloud.version>Hoxton.SR3</spring-cloud.version> |
20 | <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version> | 21 | <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version> |
21 | <mybatis-starter.version>2.1.2</mybatis-starter.version> | 22 | <mybatis-starter.version>2.1.2</mybatis-starter.version> |
23 | + <spring-boot.version>2.2.5.RELEASE</spring-boot.version> | ||
24 | + <nacos.version>1.2.0</nacos.version> | ||
25 | + <xtrade-shared.version>0.0.1</xtrade-shared.version> | ||
26 | + <reflection.version>0.9.11</reflection.version> | ||
27 | + <gateway.version>1.0.0</gateway.version> | ||
22 | </properties> | 28 | </properties> |
23 | - | ||
24 | <dependencies> | 29 | <dependencies> |
25 | <dependency> | 30 | <dependency> |
26 | - <groupId>com.diligrp</groupId> | ||
27 | - <artifactId>xtrade-shared-spring-boot-starter</artifactId> | ||
28 | - <version>0.0.1</version> | ||
29 | - </dependency> | ||
30 | - <dependency> | ||
31 | <groupId>org.springframework.boot</groupId> | 31 | <groupId>org.springframework.boot</groupId> |
32 | <artifactId>spring-boot-configuration-processor</artifactId> | 32 | <artifactId>spring-boot-configuration-processor</artifactId> |
33 | - <optional>true</optional> | ||
34 | </dependency> | 33 | </dependency> |
35 | <!-- <dependency> | 34 | <!-- <dependency> |
36 | <groupId>org.springframework.boot</groupId> | 35 | <groupId>org.springframework.boot</groupId> |
@@ -40,61 +39,53 @@ | @@ -40,61 +39,53 @@ | ||
40 | <groupId>net.sf.ehcache</groupId> | 39 | <groupId>net.sf.ehcache</groupId> |
41 | <artifactId>ehcache</artifactId> | 40 | <artifactId>ehcache</artifactId> |
42 | </dependency>--> | 41 | </dependency>--> |
43 | - <dependency> | ||
44 | - <groupId>org.mybatis.spring.boot</groupId> | ||
45 | - <artifactId>mybatis-spring-boot-starter</artifactId> | ||
46 | - <version>${mybatis-starter.version}</version> | ||
47 | - </dependency> | ||
48 | - <dependency> | ||
49 | - <groupId>org.reflections</groupId> | ||
50 | - <artifactId>reflections</artifactId> | ||
51 | - <version>0.9.11</version> | ||
52 | - </dependency> | ||
53 | - <dependency> | ||
54 | - <groupId>org.springframework.cloud</groupId> | ||
55 | - <artifactId>spring-cloud-starter-gateway</artifactId> | ||
56 | - </dependency> | ||
57 | - <dependency> | 42 | + <!-- <dependency> |
58 | <groupId>org.projectlombok</groupId> | 43 | <groupId>org.projectlombok</groupId> |
59 | <artifactId>lombok</artifactId> | 44 | <artifactId>lombok</artifactId> |
60 | <optional>true</optional> | 45 | <optional>true</optional> |
61 | - </dependency> | ||
62 | - <dependency> | ||
63 | - <groupId>com.alibaba.cloud</groupId> | ||
64 | - <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> | ||
65 | - </dependency> | ||
66 | - <dependency> | ||
67 | - <groupId>com.alibaba.cloud</groupId> | ||
68 | - <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
69 | - </dependency> | ||
70 | - <dependency> | ||
71 | - <groupId>org.springframework.boot</groupId> | ||
72 | - <artifactId>spring-boot-starter-test</artifactId> | ||
73 | - <scope>test</scope> | ||
74 | - </dependency> | ||
75 | - <dependency> | ||
76 | - <groupId>org.springframework.boot</groupId> | ||
77 | - <artifactId>spring-boot-starter-data-redis</artifactId> | ||
78 | - </dependency> | ||
79 | - <dependency> | ||
80 | - <groupId>org.apache.commons</groupId> | ||
81 | - <artifactId>commons-pool2</artifactId> | ||
82 | - </dependency> | ||
83 | - <dependency> | ||
84 | - <groupId>com.alibaba.nacos</groupId> | ||
85 | - <artifactId>nacos-api</artifactId> | ||
86 | - <version>1.2.0</version> | ||
87 | - </dependency> | ||
88 | - <dependency> | ||
89 | - <groupId>com.alibaba.nacos</groupId> | ||
90 | - <artifactId>nacos-client</artifactId> | ||
91 | - <version>1.2.0</version> | ||
92 | - </dependency> | ||
93 | - | 46 | + </dependency>--> |
94 | </dependencies> | 47 | </dependencies> |
95 | - | ||
96 | <dependencyManagement> | 48 | <dependencyManagement> |
97 | <dependencies> | 49 | <dependencies> |
50 | + <!-- SpringBoot的依赖配置--> | ||
51 | + <dependency> | ||
52 | + <groupId>org.springframework.boot</groupId> | ||
53 | + <artifactId>spring-boot-dependencies</artifactId> | ||
54 | + <version>${spring-boot.version}</version> | ||
55 | + <type>pom</type> | ||
56 | + <scope>import</scope> | ||
57 | + </dependency> | ||
58 | + <dependency> | ||
59 | + <groupId>com.diligrp</groupId> | ||
60 | + <artifactId>xtrade-shared-spring-boot-starter</artifactId> | ||
61 | + <version>${xtrade-shared.version}</version> | ||
62 | + </dependency> | ||
63 | + <dependency> | ||
64 | + <artifactId>gateway-core</artifactId> | ||
65 | + <groupId>com.diligrp</groupId> | ||
66 | + <version>${gateway.version}</version> | ||
67 | + </dependency> | ||
68 | + | ||
69 | + <dependency> | ||
70 | + <groupId>org.mybatis.spring.boot</groupId> | ||
71 | + <artifactId>mybatis-spring-boot-starter</artifactId> | ||
72 | + <version>${mybatis-starter.version}</version> | ||
73 | + </dependency> | ||
74 | + <dependency> | ||
75 | + <groupId>org.reflections</groupId> | ||
76 | + <artifactId>reflections</artifactId> | ||
77 | + <version>${reflection.version}</version> | ||
78 | + </dependency> | ||
79 | + <dependency> | ||
80 | + <groupId>com.alibaba.nacos</groupId> | ||
81 | + <artifactId>nacos-api</artifactId> | ||
82 | + <version>${nacos.version}</version> | ||
83 | + </dependency> | ||
84 | + <dependency> | ||
85 | + <groupId>com.alibaba.nacos</groupId> | ||
86 | + <artifactId>nacos-client</artifactId> | ||
87 | + <version>${nacos.version}</version> | ||
88 | + </dependency> | ||
98 | <dependency> | 89 | <dependency> |
99 | <groupId>org.springframework.cloud</groupId> | 90 | <groupId>org.springframework.cloud</groupId> |
100 | <artifactId>spring-cloud-dependencies</artifactId> | 91 | <artifactId>spring-cloud-dependencies</artifactId> |
src/main/java/com/diligrp/xtrade/gateway/api/Api.java deleted
100644 → 0
1 | -package com.diligrp.xtrade.gateway.api; | ||
2 | - | ||
3 | -import lombok.Data; | ||
4 | - | ||
5 | -import java.io.Serializable; | ||
6 | - | ||
7 | -/** | ||
8 | - * @Auther: miaoguoxin | ||
9 | - * @Date: 2019/3/31 16:24 | ||
10 | - * @Description: 只用于网关api缓存的对象 | ||
11 | - */ | ||
12 | -@Data | ||
13 | -public class Api implements Serializable { | ||
14 | - /**服务名称*/ | ||
15 | - private String serviceId; | ||
16 | - | ||
17 | - private String patternUrl; | ||
18 | - //并发量 | ||
19 | - private int replenishRate = 2; | ||
20 | - //容量 | ||
21 | - private int burstCapacity = 5; | ||
22 | - | ||
23 | - private Integer state; | ||
24 | -} |
src/main/java/com/diligrp/xtrade/gateway/repository/entity/GatewayAttrConfig.java deleted
100644 → 0
1 | -package com.diligrp.xtrade.gateway.repository.entity; | ||
2 | - | ||
3 | -import com.diligrp.xtrade.gateway.common.constant.GatewayAttrType; | ||
4 | -import com.diligrp.xtrade.shared.domain.BaseDo; | ||
5 | -import lombok.Data; | ||
6 | -import lombok.EqualsAndHashCode; | ||
7 | - | ||
8 | -/** | ||
9 | - * @Auther: miaoguoxin | ||
10 | - * @Date: 2018/12/12 0012 17:43 | ||
11 | - * @Description: 网关断言和过滤器配置实体 | ||
12 | - */ | ||
13 | -@EqualsAndHashCode(callSuper = true) | ||
14 | -@Data | ||
15 | -public class GatewayAttrConfig extends BaseDo{ | ||
16 | - /**服务id名称*/ | ||
17 | - private String serviceId; | ||
18 | - /**类型 {@link GatewayAttrType}*/ | ||
19 | - private Integer type; | ||
20 | - /**predicate或者filter的名称*/ | ||
21 | - private String attrName; | ||
22 | - /** predicate或者filter所需的参数 json格式*/ | ||
23 | - private String attrArgs; | ||
24 | - /**描述*/ | ||
25 | - private String description; | ||
26 | - /**排序*/ | ||
27 | - private Integer sortOrder; | ||
28 | - /**1:删除 0:正常*/ | ||
29 | - private Integer isDel; | ||
30 | -} |
src/main/resources/bootstrap-dev.yml deleted
100644 → 0
1 | -spring: | ||
2 | - cloud: | ||
3 | - nacos: | ||
4 | - discovery: | ||
5 | - username: xtrade | ||
6 | - password: abcd1234 | ||
7 | - server-addr: 10.28.1.79:8848 | ||
8 | - namespace: 35211f71-92a9-4f3e-975e-37b1132e05c8 | ||
9 | - config: | ||
10 | - username: xtrade | ||
11 | - password: abcd1234 | ||
12 | - server-addr: 10.28.1.79:8848 | ||
13 | - namespace: 35211f71-92a9-4f3e-975e-37b1132e05c8 |