Skip to content

Commit 0b5b882

Browse files
committed
添加限流示例
1 parent 96df53d commit 0b5b882

File tree

3 files changed

+108
-30
lines changed

3 files changed

+108
-30
lines changed

spring-cloud-gray-samples/spring-cloud-gray-gateway-sample/pom.xml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<artifactId>spring-cloud-gray-gateway-sample</artifactId>
1313

1414
<dependencies>
15-
<!-- <dependency>-->
16-
<!-- <groupId>org.springframework.boot</groupId>-->
17-
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
18-
<!-- </dependency>-->
15+
<!-- <dependency>-->
16+
<!-- <groupId>org.springframework.boot</groupId>-->
17+
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
18+
<!-- </dependency>-->
1919
<dependency>
2020
<groupId>org.springframework.boot</groupId>
2121
<artifactId>spring-boot-starter-webflux</artifactId>
@@ -24,10 +24,10 @@
2424
<groupId>org.springframework.boot</groupId>
2525
<artifactId>spring-boot-starter-test</artifactId>
2626
</dependency>
27-
<!-- <dependency>-->
28-
<!-- <groupId>org.springframework.cloud</groupId>-->
29-
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
30-
<!-- </dependency>-->
27+
<!-- <dependency>-->
28+
<!-- <groupId>org.springframework.cloud</groupId>-->
29+
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
30+
<!-- </dependency>-->
3131
<dependency>
3232
<groupId>com.alibaba.cloud</groupId>
3333
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
@@ -38,10 +38,14 @@
3838
<groupId>org.springframework.cloud</groupId>
3939
<artifactId>spring-cloud-gateway-core</artifactId>
4040
</dependency>
41-
<!-- <dependency>-->
42-
<!-- <groupId>org.springframework.cloud</groupId>-->
43-
<!-- <artifactId>spring-cloud-starter-gateway</artifactId>-->
44-
<!-- </dependency>-->
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
44+
</dependency>
45+
<!-- <dependency>-->
46+
<!-- <groupId>org.springframework.cloud</groupId>-->
47+
<!-- <artifactId>spring-cloud-starter-gateway</artifactId>-->
48+
<!-- </dependency>-->
4549

4650
<dependency>
4751
<groupId>cn.springcloud.gray</groupId>
@@ -65,10 +69,10 @@
6569
<groupId>cn.springcloud.gray</groupId>
6670
<artifactId>spring-cloud-gray-plugin-gateway</artifactId>
6771
</dependency>
68-
<!-- <dependency>-->
69-
<!-- <groupId>cn.springcloud.gray</groupId>-->
70-
<!-- <artifactId>spring-cloud-gray-plugin-event-stream</artifactId>-->
71-
<!-- </dependency>-->
72+
<!-- <dependency>-->
73+
<!-- <groupId>cn.springcloud.gray</groupId>-->
74+
<!-- <artifactId>spring-cloud-gray-plugin-event-stream</artifactId>-->
75+
<!-- </dependency>-->
7276

7377
</dependencies>
7478

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.springcloud.gray.gateway.configuration;
2+
3+
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
4+
import org.springframework.context.annotation.Bean;
5+
import reactor.core.publisher.Mono;
6+
7+
//@Configuration
8+
public class GatewayConfiguration {
9+
10+
11+
@Bean
12+
public KeyResolver uriKeyResolver() {
13+
return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
14+
}
15+
16+
17+
// @Bean
18+
// public RequestLocalStorage requestLocalStorage() {
19+
// return new RequestLocalStorage() {
20+
//
21+
// private FastThreadLocal<GrayTrackInfo> trackThreadLocal = new FastThreadLocal<>();
22+
// private FastThreadLocal<GrayRequest> reqThreadLocal = new FastThreadLocal<>();
23+
//
24+
// @Override
25+
// public void setGrayTrackInfo(GrayTrackInfo grayTrackInfo) {
26+
// trackThreadLocal.set(grayTrackInfo);
27+
// }
28+
//
29+
// @Override
30+
// public void removeGrayTrackInfo() {
31+
// trackThreadLocal.remove();
32+
// }
33+
//
34+
// @Override
35+
// public GrayTrackInfo getGrayTrackInfo() {
36+
// return trackThreadLocal.get();
37+
// }
38+
//
39+
// @Override
40+
// public void setGrayRequest(GrayRequest grayRequest) {
41+
// reqThreadLocal.set(grayRequest);
42+
// }
43+
//
44+
// @Override
45+
// public void removeGrayRequest() {
46+
// reqThreadLocal.remove();
47+
// }
48+
//
49+
// @Override
50+
// public GrayRequest getGrayRequest() {
51+
// return reqThreadLocal.get();
52+
// }
53+
// };
54+
// }
55+
56+
}

spring-cloud-gray-samples/spring-cloud-gray-gateway-sample/src/main/resources/config/application.yml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ spring:
99
nacos:
1010
discovery:
1111
server-addr: 127.0.0.1:8848
12+
gateway:
13+
discovery:
14+
locator:
15+
enabled: true
16+
# lowerCaseServiceId: true
17+
# routes:
18+
# # 会员服务
19+
# - id: service-a
20+
# uri: lb://service-a
21+
# predicates:
22+
# - Path=/ser-a/**
23+
# filters:
24+
# # 限流配置
25+
# - name: RequestRateLimiter
26+
# args:
27+
# key-resolver: '#{@uriKeyResolver}'
28+
# redis-rate-limiter.replenishRate: 500
29+
# redis-rate-limiter.burstCapacity: 1000
30+
# 降级配置
31+
# - name: Hystrix
32+
# args:
33+
# name: default
34+
# fallbackUri: 'forward:/fallback'
1235
stream:
1336
bindings:
1437
GrayEventInput:
@@ -28,11 +51,6 @@ spring:
2851
# requeueRejected: true #spring cloud stream 如果出现异常, 是否需要重新投递消息, false表示丢弃。 也有相应的Exception, true-MessageRejectedWhileStoppingException false-AmqpRejectAndDontRequeueException
2952
# auto-bind-dlq: true
3053
acknowledgeMode: AUTO
31-
gateway:
32-
discovery:
33-
locator:
34-
enabled: true
35-
lowerCaseServiceId: true
3654
server:
3755
port: 20401
3856
eureka:
@@ -41,10 +59,10 @@ eureka:
4159
defaultZone: http://localhost:20001/eureka/
4260

4361
ribbon:
44-
ReadTimeout: 30000
45-
ConnectTimeout: 30000
46-
eureka:
47-
enabled: true
62+
ReadTimeout: 30000
63+
ConnectTimeout: 30000
64+
eureka:
65+
enabled: true
4866
hystrix:
4967
threadpool:
5068
# 这是默认的配置
@@ -76,11 +94,11 @@ gray:
7694
definitionsInitializeDelayTimeInMs: 10000
7795
web:
7896
track-definitions:
79-
# - name: HttpIP
80-
# - name: HttpMethod
81-
# - name: HttpURI
82-
# - name: HttpReceive
83-
# - name: HttpHeader
97+
# - name: HttpIP
98+
# - name: HttpMethod
99+
# - name: HttpURI
100+
# - name: HttpReceive
101+
# - name: HttpHeader
84102
# - name: HttpParameter
85103
# value: version,test
86104
path-patterns: /*

0 commit comments

Comments
 (0)