This commit is contained in:
Ganesh Pagade 2018-10-26 10:21:35 +05:30
parent 2bca6e0c83
commit f34d44faaa
5 changed files with 133 additions and 79 deletions

View File

@ -37,7 +37,7 @@
<dependency> <dependency>
<groupId>com.marcosbarbero.cloud</groupId> <groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId> <artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>LATEST</version> <version>2.2.0.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -1,12 +1,53 @@
package com.baeldung.spring.cloud.zuulratelimitdemo; package com.baeldung.spring.cloud.zuulratelimitdemo;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.context.annotation.Bean;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitKeyGenerator;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitUtils;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.RateLimiterErrorHandler;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.DefaultRateLimiterErrorHandler;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.DefaultRateLimitKeyGenerator;
@SpringBootApplication @SpringBootApplication
public class ZuulRatelimitDemoApplication { public class ZuulRatelimitDemoApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ZuulRatelimitDemoApplication.class, args); SpringApplication.run(ZuulRatelimitDemoApplication.class, args);
} }
@Bean
public RateLimitKeyGenerator ratelimitKeyGenerator(RateLimitProperties properties, RateLimitUtils rateLimitUtils) {
return new DefaultRateLimitKeyGenerator(properties, rateLimitUtils) {
@Override
public String key(HttpServletRequest request, Route route, RateLimitProperties.Policy policy) {
return super.key(request, route, policy) + ":" + request.getMethod();
}
};
}
@Bean
public RateLimiterErrorHandler rateLimitErrorHandler() {
return new DefaultRateLimiterErrorHandler() {
@Override
public void handleSaveError(String key, Exception e) {
// custom code
}
@Override
public void handleFetchError(String key, Exception e) {
// custom code
}
@Override
public void handleError(String msg, Exception e) {
// custom code
}
};
}
} }

View File

@ -11,16 +11,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/greeting") @RequestMapping("/greeting")
public class GreetingController { public class GreetingController {
public static final String SIMPLE_RESPONSE = "Hi!"; public static final String SIMPLE_RESPONSE = "Hi!";
public static final String ADVANCED_RESPONSE = "Hello, how you doing?"; public static final String ADVANCED_RESPONSE = "Hello, how you doing?";
@GetMapping("/simple") @GetMapping("/simple")
public ResponseEntity<String> serviceA() { public ResponseEntity<String> getSimple() {
return ResponseEntity.ok(SIMPLE_RESPONSE); return ResponseEntity.ok(SIMPLE_RESPONSE);
} }
@GetMapping("/advanced") @GetMapping("/advanced")
public ResponseEntity<String> serviceB() { public ResponseEntity<String> getAdvanced() {
return ResponseEntity.ok(ADVANCED_RESPONSE); return ResponseEntity.ok(ADVANCED_RESPONSE);
} }
} }

View File

@ -11,13 +11,13 @@ zuul:
repository: JPA repository: JPA
policy-list: policy-list:
serviceSimple: serviceSimple:
- limit: 5 - limit: 5
refresh-interval: 60 refresh-interval: 60
type: type:
- origin - origin
serviceAdvanced: serviceAdvanced:
- limit: 1 - limit: 1
refresh-interval: 2 refresh-interval: 2
type: type:
- origin - origin
strip-prefix: true strip-prefix: true

View File

@ -28,73 +28,86 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingControllerTest { public class GreetingControllerTest {
private static final String SIMPLE_GREETING = "/greeting/simple"; private static final String SIMPLE_GREETING = "/greeting/simple";
private static final String ADVANCED_GREETING = "/greeting/advanced"; private static final String ADVANCED_GREETING = "/greeting/advanced";
@Autowired @Autowired
private TestRestTemplate restTemplate; private TestRestTemplate restTemplate;
@Test @Test
public void whenRequestNotExceedingCapacity_thenReturnOkResponse() { public void whenRequestNotExceedingCapacity_thenReturnOkResponse() {
ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class); ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class);
HttpHeaders headers = response.getHeaders(); HttpHeaders headers = response.getHeaders();
String key = "rate-limit-application_serviceSimple_127.0.0.1"; String key = "rate-limit-application_serviceSimple_127.0.0.1";
assertHeaders(headers, key, false, false);
assertEquals(OK, response.getStatusCode());
}
@Test String limit = headers.getFirst(HEADER_LIMIT + key);
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException { String remaining = headers.getFirst(HEADER_REMAINING + key);
ResponseEntity<String> response = this.restTemplate String reset = headers.getFirst(HEADER_RESET + key);
.getForEntity(ADVANCED_GREETING, String.class);
HttpHeaders headers = response.getHeaders();
String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
assertHeaders(headers, key, false, false);
assertEquals(OK, response.getStatusCode());
for (int i = 0; i < 2; i++) { assertEquals(limit, "5");
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); assertEquals(remaining, "4");
assertEquals(reset, "60000");
assertEquals(OK, response.getStatusCode());
} }
assertEquals(TOO_MANY_REQUESTS, response.getStatusCode()); @Test
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody()); public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
ResponseEntity<String> response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
HttpHeaders headers = response.getHeaders();
String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
assertHeaders(headers, key, false, false);
assertEquals(OK, response.getStatusCode());
TimeUnit.SECONDS.sleep(2); for (int i = 0; i < 2; i++) {
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
}
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); headers = response.getHeaders();
headers = response.getHeaders(); String limit = headers.getFirst(HEADER_LIMIT + key);
assertHeaders(headers, key, false, false); String remaining = headers.getFirst(HEADER_REMAINING + key);
assertEquals(OK, response.getStatusCode()); String reset = headers.getFirst(HEADER_RESET + key);
}
private void assertHeaders(HttpHeaders headers, String key, boolean nullable, assertEquals(limit, "1");
boolean quotaHeaders) { assertEquals(remaining, "0");
String quota = headers.getFirst(HEADER_QUOTA + key); assertNotEquals(reset, "2000");
String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key);
String limit = headers.getFirst(HEADER_LIMIT + key);
String remaining = headers.getFirst(HEADER_REMAINING + key);
String reset = headers.getFirst(HEADER_RESET + key);
if (nullable) { assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
if (quotaHeaders) { assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());
assertNull(quota);
assertNull(remainingQuota); TimeUnit.SECONDS.sleep(2);
} else {
assertNull(limit); response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
assertNull(remaining); headers = response.getHeaders();
} assertHeaders(headers, key, false, false);
assertNull(reset); assertEquals(OK, response.getStatusCode());
} else {
if (quotaHeaders) {
assertNotNull(quota);
assertNotNull(remainingQuota);
} else {
assertNotNull(limit);
assertNotNull(remaining);
}
assertNotNull(reset);
} }
}
private void assertHeaders(HttpHeaders headers, String key, boolean nullable, boolean quotaHeaders) {
String quota = headers.getFirst(HEADER_QUOTA + key);
String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key);
String limit = headers.getFirst(HEADER_LIMIT + key);
String remaining = headers.getFirst(HEADER_REMAINING + key);
String reset = headers.getFirst(HEADER_RESET + key);
if (nullable) {
if (quotaHeaders) {
assertNull(quota);
assertNull(remainingQuota);
} else {
assertNull(limit);
assertNull(remaining);
}
assertNull(reset);
} else {
if (quotaHeaders) {
assertNotNull(quota);
assertNotNull(remainingQuota);
} else {
assertNotNull(limit);
assertNotNull(remaining);
}
assertNotNull(reset);
}
}
} }