Skip to content

Commit ea52c02

Browse files
authored
Merge pull request eugenp#5520 from rockoder/zuul_throttling
Zuul throttling
2 parents df8158a + 6f87196 commit ea52c02

5 files changed

Lines changed: 250 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung.spring.cloud</groupId>
7+
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-cloud-zuul-ratelimit</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.marcosbarbero.cloud</groupId>
39+
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
40+
<version>2.2.0.RELEASE</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-data-jpa</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.h2database</groupId>
48+
<artifactId>h2</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
<dependencyManagement>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-dependencies</artifactId>
63+
<version>${spring-cloud.version}</version>
64+
<type>pom</type>
65+
<scope>import</scope>
66+
</dependency>
67+
</dependencies>
68+
</dependencyManagement>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-maven-plugin</artifactId>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
80+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.spring.cloud.zuulratelimitdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.cloud.client.SpringCloudApplication;
5+
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
6+
7+
@EnableZuulProxy
8+
@SpringCloudApplication
9+
public class ZuulRatelimitDemoApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(ZuulRatelimitDemoApplication.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.cloud.zuulratelimitdemo.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
8+
@Controller
9+
@RequestMapping("/greeting")
10+
public class GreetingController {
11+
12+
@GetMapping("/simple")
13+
public ResponseEntity<String> getSimple() {
14+
return ResponseEntity.ok("Hi!");
15+
}
16+
17+
@GetMapping("/advanced")
18+
public ResponseEntity<String> getAdvanced() {
19+
return ResponseEntity.ok("Hello, how you doing?");
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
zuul:
2+
routes:
3+
serviceSimple:
4+
path: /greeting/simple
5+
url: forward:/
6+
serviceAdvanced:
7+
path: /greeting/advanced
8+
url: forward:/
9+
ratelimit:
10+
enabled: true
11+
repository: JPA
12+
policy-list:
13+
serviceSimple:
14+
- limit: 5
15+
refresh-interval: 60
16+
type:
17+
- origin
18+
serviceAdvanced:
19+
- limit: 1
20+
refresh-interval: 2
21+
type:
22+
- origin
23+
strip-prefix: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.baeldung.spring.cloud.zuulratelimitdemo.controller;
2+
3+
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_LIMIT;
4+
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_QUOTA;
5+
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING;
6+
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING_QUOTA;
7+
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_RESET;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotEquals;
10+
import static org.junit.Assert.assertNotNull;
11+
import static org.junit.Assert.assertNull;
12+
import static org.springframework.http.HttpStatus.OK;
13+
import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS;
14+
15+
import java.util.concurrent.TimeUnit;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.boot.test.web.client.TestRestTemplate;
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.ResponseEntity;
24+
import org.springframework.test.context.junit4.SpringRunner;
25+
26+
@AutoConfigureTestDatabase
27+
@RunWith(SpringRunner.class)
28+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
29+
public class GreetingControllerTest {
30+
31+
private static final String SIMPLE_GREETING = "/greeting/simple";
32+
private static final String ADVANCED_GREETING = "/greeting/advanced";
33+
34+
@Autowired
35+
private TestRestTemplate restTemplate;
36+
37+
@Test
38+
public void whenRequestNotExceedingCapacity_thenReturnOkResponse() {
39+
ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class);
40+
HttpHeaders headers = response.getHeaders();
41+
String key = "rate-limit-application_serviceSimple_127.0.0.1";
42+
43+
String limit = headers.getFirst(HEADER_LIMIT + key);
44+
String remaining = headers.getFirst(HEADER_REMAINING + key);
45+
String reset = headers.getFirst(HEADER_RESET + key);
46+
47+
assertEquals(limit, "5");
48+
assertEquals(remaining, "4");
49+
assertEquals(reset, "60000");
50+
51+
assertEquals(OK, response.getStatusCode());
52+
}
53+
54+
@Test
55+
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
56+
ResponseEntity<String> response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
57+
HttpHeaders headers = response.getHeaders();
58+
String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
59+
assertHeaders(headers, key, false, false);
60+
assertEquals(OK, response.getStatusCode());
61+
62+
for (int i = 0; i < 2; i++) {
63+
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
64+
}
65+
66+
headers = response.getHeaders();
67+
String limit = headers.getFirst(HEADER_LIMIT + key);
68+
String remaining = headers.getFirst(HEADER_REMAINING + key);
69+
String reset = headers.getFirst(HEADER_RESET + key);
70+
71+
assertEquals(limit, "1");
72+
assertEquals(remaining, "0");
73+
assertNotEquals(reset, "2000");
74+
75+
assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
76+
77+
TimeUnit.SECONDS.sleep(2);
78+
79+
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
80+
headers = response.getHeaders();
81+
assertHeaders(headers, key, false, false);
82+
assertEquals(OK, response.getStatusCode());
83+
}
84+
85+
private void assertHeaders(HttpHeaders headers, String key, boolean nullable, boolean quotaHeaders) {
86+
String quota = headers.getFirst(HEADER_QUOTA + key);
87+
String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key);
88+
String limit = headers.getFirst(HEADER_LIMIT + key);
89+
String remaining = headers.getFirst(HEADER_REMAINING + key);
90+
String reset = headers.getFirst(HEADER_RESET + key);
91+
92+
if (nullable) {
93+
if (quotaHeaders) {
94+
assertNull(quota);
95+
assertNull(remainingQuota);
96+
} else {
97+
assertNull(limit);
98+
assertNull(remaining);
99+
}
100+
assertNull(reset);
101+
} else {
102+
if (quotaHeaders) {
103+
assertNotNull(quota);
104+
assertNotNull(remainingQuota);
105+
} else {
106+
assertNotNull(limit);
107+
assertNotNull(remaining);
108+
}
109+
assertNotNull(reset);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)