forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
8 additions
and
53 deletions.
There are no files selected for viewing
47 changes: 4 additions & 43 deletions
47
...c/main/java/com/baeldung/spring/cloud/zuulratelimitdemo/ZuulRatelimitDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} | ||
} |
13 changes: 4 additions & 9 deletions
13
.../main/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters