|
@@ -0,0 +1,45 @@
|
|
1
|
+package com.yaozhitech.spring5.config;
|
|
2
|
+
|
|
3
|
+import org.springframework.beans.factory.annotation.Configurable;
|
|
4
|
+import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
|
|
5
|
+import org.springframework.cloud.gateway.route.RouteLocator;
|
|
6
|
+import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
|
7
|
+import org.springframework.context.annotation.Bean;
|
|
8
|
+import org.springframework.context.annotation.Configuration;
|
|
9
|
+
|
|
10
|
+@Configuration
|
|
11
|
+public class RouterConfig {
|
|
12
|
+
|
|
13
|
+ @Bean
|
|
14
|
+ public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
|
15
|
+ //@formatter:off
|
|
16
|
+ return builder.routes()
|
|
17
|
+ .route("path_route", r -> r.path("/city_list.do")
|
|
18
|
+ .uri("https://api.bbztx.com"))
|
|
19
|
+ .route("host_route", r -> r.host("*.myhost.org")
|
|
20
|
+ .uri("http://httpbin.org"))
|
|
21
|
+// .route("rewrite_route", r -> r.host("*.rewrite.org")
|
|
22
|
+// .filters(f -> f.rewritePath("/foo/(?<segment>.*)",
|
|
23
|
+// "/${segment}"))
|
|
24
|
+// .uri("http://httpbin.org"))
|
|
25
|
+// .route("hystrix_route", r -> r.host("*.hystrix.org")
|
|
26
|
+// .filters(f -> f.hystrix(c -> c.setName("slowcmd")))
|
|
27
|
+// .uri("http://httpbin.org"))
|
|
28
|
+// .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
|
|
29
|
+// .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
|
|
30
|
+// .uri("http://httpbin.org"))
|
|
31
|
+// .route("limit_route", r -> r
|
|
32
|
+// .host("*.limited.org").and().path("/anything/**")
|
|
33
|
+// .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
|
|
34
|
+// .uri("http://httpbin.org"))
|
|
35
|
+// .route("websocket_route", r -> r.path("/echo")
|
|
36
|
+// .uri("ws://localhost:9000"))
|
|
37
|
+ .build();
|
|
38
|
+ //@formatter:on
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ @Bean
|
|
42
|
+ RedisRateLimiter redisRateLimiter() {
|
|
43
|
+ return new RedisRateLimiter(1, 2);
|
|
44
|
+ }
|
|
45
|
+}
|