fixes
This commit is contained in:
parent
2bca6e0c83
commit
f34d44faaa
@ -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>
|
||||||
|
@ -1,7 +1,18 @@
|
|||||||
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 {
|
||||||
@ -9,4 +20,34 @@ 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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,12 @@ public class GreetingController {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,14 +39,21 @@ public class GreetingControllerTest {
|
|||||||
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);
|
|
||||||
|
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());
|
assertEquals(OK, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
|
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
|
||||||
ResponseEntity<String> response = this.restTemplate
|
ResponseEntity<String> response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
|
||||||
.getForEntity(ADVANCED_GREETING, String.class);
|
|
||||||
HttpHeaders headers = response.getHeaders();
|
HttpHeaders headers = response.getHeaders();
|
||||||
String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
|
String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
|
||||||
assertHeaders(headers, key, false, false);
|
assertHeaders(headers, key, false, false);
|
||||||
@ -56,6 +63,15 @@ public class GreetingControllerTest {
|
|||||||
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
|
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());
|
assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
|
||||||
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());
|
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());
|
||||||
|
|
||||||
@ -67,8 +83,7 @@ public class GreetingControllerTest {
|
|||||||
assertEquals(OK, response.getStatusCode());
|
assertEquals(OK, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertHeaders(HttpHeaders headers, String key, boolean nullable,
|
private void assertHeaders(HttpHeaders headers, String key, boolean nullable, boolean quotaHeaders) {
|
||||||
boolean quotaHeaders) {
|
|
||||||
String quota = headers.getFirst(HEADER_QUOTA + key);
|
String quota = headers.getFirst(HEADER_QUOTA + key);
|
||||||
String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key);
|
String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key);
|
||||||
String limit = headers.getFirst(HEADER_LIMIT + key);
|
String limit = headers.getFirst(HEADER_LIMIT + key);
|
||||||
@ -95,6 +110,4 @@ public class GreetingControllerTest {
|
|||||||
assertNotNull(reset);
|
assertNotNull(reset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user