spring cloud gateway 限流测试

使用maven搭建工程,maven工程中包括3个module 分别是eureka-server、gateway-server、service-demo。父pom文件如下

 1 <parent>
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>2.0.2.RELEASE</version>
 5         <relativePath/>
 6     </parent>
 7 
 8     <modules>
 9         <module>eureka-server</module>
10         <module>service-demo</module>
11         <module>gateway-server</module>
12     </modules>
13 
14     <properties>
15         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17         <java.version>1.8</java.version>
18         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-test</artifactId>
25             <scope>test</scope>
26         </dependency>
27     </dependencies>
28 
29     <dependencyManagement>
30         <dependencies>
31             <dependency>
32                 <groupId>org.springframework.cloud</groupId>
33                 <artifactId>spring-cloud-dependencies</artifactId>
34                 <version>${spring-cloud.version}</version>
35                 <type>pom</type>
36                 <scope>import</scope>
37             </dependency>
38         </dependencies>
39     </dependencyManagement>
40 
41     <build>
42         <plugins>
43             <plugin>
44                 <groupId>org.springframework.boot</groupId>
45                 <artifactId>spring-boot-maven-plugin</artifactId>
46             </plugin>
47         </plugins>
48     </build>
1.eureka-server工程配置及代码
 1   <name>eureka-server</name>
 2     <description>Demo project for Spring Boot</description>
 3 
 4     <parent>
 5         <groupId>com.xxx</groupId>
 6         <artifactId>xxx</artifactId>
 7         <version>0.0.1-SNAPSHOT</version>
 8     </parent>
 9 
10     <dependencies>
11         <dependency>
12             <groupId>org.springframework.cloud</groupId>
13             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
14         </dependency>
15     </dependencies>

application.yml

 1 server:
 2   port: 8761
 3 
 4 eureka:
 5   instance:
 6     hostname: localhost
 7   client:
 8     registerWithEureka: false
 9     fetchRegistry: false
10     serviceUrl:
11       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
12 
13 spring:
14   application:
15     name: eurka-server
View Code
EurekaServerApplication
1 @SpringBootApplication
2 @EnableEurekaServer
3 public class EurekaServerApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run( EurekaServerApplication.class, args );
7     }
8 }
View Code

2.gateway-server代码和配置

pom.xml

 1  <parent>
 2         <groupId>com.xxx</groupId>
 3         <artifactId>xxx</artifactId>
 4         <version>0.0.1-SNAPSHOT</version>
 5     </parent>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>org.springframework.cloud</groupId>
10             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11         </dependency>
12         <dependency>
13             <groupId>org.springframework.cloud</groupId>
14             <artifactId>spring-cloud-starter-gateway</artifactId>
15         </dependency>
16 
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-test</artifactId>
20             <scope>test</scope>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
25         </dependency>
26     </dependencies>
27 
28     <build>
29         <plugins>
30             <plugin>
31                 <groupId>org.springframework.boot</groupId>
32                 <artifactId>spring-boot-maven-plugin</artifactId>
33             </plugin>
34         </plugins>
35     </build>
View Code

application.yml   

  • filter名称必须是RequestRateLimiter
  • redis-rate-limiter.replenishRate:允许用户每秒处理多少个请求
  • redis-rate-limiter.burstCapacity:令牌桶的容量,允许在一秒钟内完成的最大请求数
  • key-resolver:使用SpEL按名称引用bean

设置 redis-rate-limiter 的 参数都为1 供测试  注意! redis不要使用windows版本 有的命令不存在 !

 1 server:
 2   port: 8888
 3 
 4 spring:
 5   application:
 6     name: gateway-server
 7   redis:
 8     host: 127.0.0.1
 9     port: 6379
10   cloud:
11     gateway:
12       discovery:
13         locator:
14           enabled: false
15           lowerCaseServiceId: true
16       routes:
17       - id: service-hi
18         uri: lb://SERVICE-HI
19         predicates:
20           - Path=/demo/**
21         filters:
22           - StripPrefix=1
23           - name: RequestRateLimiter
24             args:
25               key-resolver: '#{@uriKeyResolver}'
26               redis-rate-limiter.replenishRate: 1
27               redis-rate-limiter.burstCapacity: 1
28           - AddResponseHeader=X-Response-Foo, Bar
29 eureka:
30   client:
31     service-url:
32       defaultZone: http://localhost:8761/eureka/
View Code
UriKeyResolver 通过什么限流 实现 KeyResolver接口 ,可以实现其他方式 这里做测试 写死返回值
 1 public class UriKeyResolver implements KeyResolver {
 2 
 3     @Override
 4     public Mono<String> resolve(ServerWebExchange exchange) {
 5 //        return Mono.just(exchange.getRequest().getURI().getPath());
 6         String user = "1";
 7         return Mono.just(user);
 8     }
 9 
10 }
View Code
GatewayApplication
 1 @SpringBootApplication
 2 @EnableEurekaClient
 3 public class GatewayApplication {
 4 
 5     public static void main(String[] args) {
 6         SpringApplication.run(GatewayApplication .class, args);
 7     }
 8 
 9     @Bean
10     public UriKeyResolver uriKeyResolver() {
11         return new UriKeyResolver();
12     }
View Code

3.service-demo代码和配置

pom.xml

 1  <parent>
 2         <groupId>com.xxx</groupId>
 3         <artifactId>xxxx</artifactId>
 4         <version>0.0.1-SNAPSHOT</version>
 5     </parent>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>org.springframework.cloud</groupId>
10             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
11         </dependency>
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-starter-web</artifactId>
15         </dependency>
16     </dependencies>
17 
18     <build>
19         <plugins>
20             <plugin>
21                 <groupId>org.springframework.boot</groupId>
22                 <artifactId>spring-boot-maven-plugin</artifactId>
23             </plugin>
24         </plugins>
25     </build>
View Code

application.yml 

 1 server:
 2   port: 8762
 3 
 4 spring:
 5   application:
 6     name: service-demo
 7 
 8 eureka:
 9   client:
10     serviceUrl:
11       defaultZone: http://localhost:8761/eureka/
View Code

ServiceDemoApplication 声明hi接口

 1 @SpringBootApplication
 2 @EnableEurekaClient
 3 @RestController
 4 /**
 5  * @author chaixg
 6  */
 7 public class ServiceDemoApplication {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(ServiceDemoApplication.class, args);
11     }
12 
13     @Value("${server.port}")
14     String port;
15 
16     @RequestMapping("/hi")
17     public String home(@RequestParam(value = "name", defaultValue = "xxx") String name) {
18         return "hi " + name + " ,i am from port:" + port;
19     }
View Code

依次启动三个工程

先在浏览器上测试下是否通了  

如果频繁的刷新会出现429 因为上面设置的是一个令牌一个请求

over....

猜你喜欢

转载自www.cnblogs.com/jasonChai/p/10188924.html