Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rockoder committed Nov 1, 2018
1 parent 21d6a0d commit 6f87196
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,14 @@
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;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@SpringCloudApplication
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
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.baeldung.spring.cloud.zuulratelimitdemo.controller;

import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@EnableZuulProxy
@SpringCloudApplication
@Controller
@RequestMapping("/greeting")
public class GreetingController {

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

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

@GetMapping("/advanced")
public ResponseEntity<String> getAdvanced() {
return ResponseEntity.ok(ADVANCED_RESPONSE);
return ResponseEntity.ok("Hello, how you doing?");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() thr
assertNotEquals(reset, "2000");

assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody());

TimeUnit.SECONDS.sleep(2);

Expand Down

0 comments on commit 6f87196

Please sign in to comment.