Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rockoder committed Oct 26, 2018
1 parent 2bca6e0 commit f34d44f
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 87 deletions.
2 changes: 1 addition & 1 deletion spring-cloud/spring-cloud-zuul-throttling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
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 {

public static void main(String[] args) {
SpringApplication.run(ZuulRatelimitDemoApplication.class, args);
}
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
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
@RequestMapping("/greeting")
public class GreetingController {

public static final String SIMPLE_RESPONSE = "Hi!";
public static final String ADVANCED_RESPONSE = "Hello, how you doing?";
public static final String SIMPLE_RESPONSE = "Hi!";
public static final String ADVANCED_RESPONSE = "Hello, how you doing?";

@GetMapping("/simple")
public ResponseEntity<String> serviceA() {
return ResponseEntity.ok(SIMPLE_RESPONSE);
}
@GetMapping("/simple")
public ResponseEntity<String> getSimple() {
return ResponseEntity.ok(SIMPLE_RESPONSE);
}

@GetMapping("/advanced")
public ResponseEntity<String> serviceB() {
return ResponseEntity.ok(ADVANCED_RESPONSE);
}
@GetMapping("/advanced")
public ResponseEntity<String> getAdvanced() {
return ResponseEntity.ok(ADVANCED_RESPONSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ zuul:
repository: JPA
policy-list:
serviceSimple:
- limit: 5
refresh-interval: 60
type:
- origin
- limit: 5
refresh-interval: 60
type:
- origin
serviceAdvanced:
- limit: 1
refresh-interval: 2
type:
- origin
strip-prefix: true
- limit: 1
refresh-interval: 2
type:
- origin
strip-prefix: true
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,86 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingControllerTest {

private static final String SIMPLE_GREETING = "/greeting/simple";
private static final String ADVANCED_GREETING = "/greeting/advanced";

@Autowired
private TestRestTemplate restTemplate;

@Test
public void whenRequestNotExceedingCapacity_thenReturnOkResponse() {
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);
assertEquals(OK, response.getStatusCode());
}

@Test
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());

for (int i = 0; i < 2; i++) {
response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
private static final String SIMPLE_GREETING = "/greeting/simple";
private static final String ADVANCED_GREETING = "/greeting/advanced";

@Autowired
private TestRestTemplate restTemplate;

@Test
public void whenRequestNotExceedingCapacity_thenReturnOkResponse() {
ResponseEntity<String> response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class);
HttpHeaders headers = response.getHeaders();
String key = "rate-limit-application_serviceSimple_127.0.0.1";

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(TOO_MANY_REQUESTS, response.getStatusCode());
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());

TimeUnit.SECONDS.sleep(2);

response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
headers = response.getHeaders();
assertHeaders(headers, key, false, false);
assertEquals(OK, response.getStatusCode());
}

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);
@Test
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());

for (int i = 0; i < 2; i++) {
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());

TimeUnit.SECONDS.sleep(2);

response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
headers = response.getHeaders();
assertHeaders(headers, key, false, false);
assertEquals(OK, response.getStatusCode());
}
}

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);
}
}
}

0 comments on commit f34d44f

Please sign in to comment.