Parcourir la source

hystrix 熔断

yufeng0528 il y a 4 ans
Parent
commit
c43920f652

+ 27 - 2
README.md

@@ -5,12 +5,37 @@ spring5_demo
5 5
 
6 6
 spring5 -> webflux ->spring gateway
7 7
 
8
-# 版本
8
+## 版本
9 9
 * 2.2.* -> Hoxton
10 10
 * 2.1.* -> Greenwich
11 11
 * 2.0.* -> Finchley
12 12
 
13
-# 路由配置参考
13
+## 路由配置参考
14 14
 1. https://www.jianshu.com/p/86660b8b24c4
15 15
 2. https://www.jdon.com/51642
16 16
 3. 
17
+
18
+## gateway filter
19
+1. AddRequestHeader 添加header给下流接口
20
+2. AddRequestParameter 
21
+3. AddResponseHeader 
22
+4. DedupeResponseHeader 剔除重复的响应头
23
+5. Hystrix 未来,Hystrix会被Spring Cloud移除掉,取而代之的是Alibaba Sentinel/Resilience4J
24
+6. PrefixPath 为匹配的路由添加前缀
25
+7. PreserveHostHeader
26
+8. RequestRateLimiter
27
+9. RedirectTo
28
+10. RemoveRequestHeader
29
+11. RemoveResponseHeader
30
+12. RewritePath
31
+13. RewriteResponseHeader
32
+14. SaveSession 
33
+15. RequestSize 为后端服务设置收到的最大请求包大小
34
+16. ...
35
+
36
+
37
+
38
+
39
+
40
+
41
+

+ 11 - 4
pom.xml

@@ -50,10 +50,17 @@
50 50
             <artifactId>spring-cloud-starter</artifactId>
51 51
        </dependency>
52 52
        
53
-       <dependency>
54
-            <groupId>org.springframework.cloud</groupId>
55
-            <artifactId>spring-cloud-starter-gateway</artifactId>
56
-        </dependency>
53
+       <!-- 网关 -->
54
+		<dependency>
55
+			<groupId>org.springframework.cloud</groupId>
56
+			<artifactId>spring-cloud-starter-gateway</artifactId>
57
+		</dependency>
58
+	
59
+		<!-- hystrix -->
60
+		<dependency>
61
+			<groupId>org.springframework.cloud</groupId>
62
+			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
63
+		</dependency>
57 64
         
58 65
 		<!-- Spring Boot Web Flux 依赖 -->
59 66
 		<dependency>

+ 6 - 0
src/main/java/com/yaozhitech/spring5/config/RouterConfig.java

@@ -25,6 +25,12 @@ public class RouterConfig {
25 25
 						.filters(f -> f.rewritePath("city_list",  // rewritePath方法会使用内建的过滤器重写路径
26 26
 								"city_list.do"))
27 27
 						.uri("https://api.bbztx.com"))
28
+				.route("hystrix_fallback_route", r -> r.path("/slow_query.do")
29
+						.filters(f -> f
30
+										.rewritePath("_query.do", "")
31
+										.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))
32
+							    )
33
+						.uri("http://127.0.0.1:8080"))
28 34
 //				.route("hystrix_route", r -> r.host("*.hystrix.org")
29 35
 //						.filters(f -> f.hystrix(c -> c.setName("slowcmd")))
30 36
 //								.uri("http://httpbin.org"))

+ 29 - 0
src/main/java/com/yaozhitech/spring5/controller/HystrixfallbackController.java

@@ -0,0 +1,29 @@
1
+package com.yaozhitech.spring5.controller;
2
+
3
+import org.springframework.web.bind.annotation.GetMapping;
4
+import org.springframework.web.bind.annotation.RestController;
5
+
6
+import lombok.extern.slf4j.Slf4j;
7
+import reactor.core.publisher.Mono;
8
+
9
+@RestController
10
+@Slf4j
11
+public class HystrixfallbackController {
12
+
13
+	@GetMapping(value = "/hystrixfallback")
14
+	public Mono<String> hystrixfallback() {
15
+		return Mono.create(monoSink -> monoSink.success("hystrixfallback"));
16
+	}
17
+	
18
+	@GetMapping(value = "/slow")
19
+	public Mono<String> slow() {
20
+		log.info("slow ing");
21
+		try {
22
+			Thread.sleep(4700);
23
+		} catch (InterruptedException e) {
24
+			// TODO Auto-generated catch block
25
+			e.printStackTrace();
26
+		}
27
+		return Mono.create(monoSink -> monoSink.success("slow"));
28
+	}
29
+}

+ 3 - 1
src/main/resources/application.properties

@@ -8,4 +8,6 @@ spring.redis.password=bbztx123456
8 8
 # 连接超时时间(毫秒)
9 9
 spring.redis.timeout=5000
10 10
 #日志
11
-logging.level.org.springframework.cloud=debug
11
+logging.level.org.springframework.cloud=debug
12
+#hystrix
13
+hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 4000