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>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>LATEST</version>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,7 +1,18 @@
package com.baeldung.spring.cloud.zuulratelimitdemo;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication;
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
public class ZuulRatelimitDemoApplication {
@ -9,4 +20,34 @@ public class ZuulRatelimitDemoApplication {
public static void main(String[] 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

@ -15,12 +15,12 @@ public class GreetingController {
public static final String ADVANCED_RESPONSE = "Hello, how you doing?";
@GetMapping("/simple")
public ResponseEntity<String> serviceA() {
public ResponseEntity<String> getSimple() {
return ResponseEntity.ok(SIMPLE_RESPONSE);
}
@GetMapping("/advanced")
public ResponseEntity<String> serviceB() {
public ResponseEntity<String> getAdvanced() {
return ResponseEntity.ok(ADVANCED_RESPONSE);
}
}

View File

@ -39,14 +39,21 @@ public class GreetingControllerTest {
ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class);
HttpHeaders headers = response.getHeaders();
String key = "rate-limit-application_serviceSimple_127.0.0.1";
assertHeaders(headers, key, false, false);
String limit = headers.getFirst(HEADER_LIMIT + key);
String remaining = headers.getFirst(HEADER_REMAINING + key);
String reset = headers.getFirst(HEADER_RESET + key);
assertEquals(limit, "5");
assertEquals(remaining, "4");
assertEquals(reset, "60000");
assertEquals(OK, response.getStatusCode());
}
@Test
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
ResponseEntity<String> response = this.restTemplate
.getForEntity(ADVANCED_GREETING, String.class);
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);
@ -56,6 +63,15 @@ public class GreetingControllerTest {
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
}
headers = response.getHeaders();
String limit = headers.getFirst(HEADER_LIMIT + key);
String remaining = headers.getFirst(HEADER_REMAINING + key);
String reset = headers.getFirst(HEADER_RESET + key);
assertEquals(limit, "1");
assertEquals(remaining, "0");
assertNotEquals(reset, "2000");
assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());
@ -67,8 +83,7 @@ public class GreetingControllerTest {
assertEquals(OK, response.getStatusCode());
}
private void assertHeaders(HttpHeaders headers, String key, boolean nullable,
boolean quotaHeaders) {
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);
@ -95,6 +110,4 @@ public class GreetingControllerTest {
assertNotNull(reset);
}
}
}