上一篇:SpringCloud 2020-OpenFeign服务接口调用(笔记)
Hystrix断路器
- 1、概述
- 2、Hystrix重要概念
- 3、hystrix案例
- 3.1 构建
- 3.2 高并发测试(搞事情【滑稽】)
- 3.3 80端口加入高并发测试
- 3.4 服务降级(@HystrixCommand)
- 3.4.1 8001和8002实现服务降级
- 3.4.2 cloud-consumer-feign-hystrix-order80端实现服务降级
- 3.4.3 解决兜底方法代码膨胀问题
- 3.4.4 解决兜底方法代码混乱问题
- 3.5 服务熔断
- 3.5.1 概念
- 3.5.2 实现服务熔断
- 3.5.3 总结
- 3.6 服务限流(暂无)
- 4、hystrix工作流程
- 5、服务监控hystrixDashboard
- 5.1 搭建hystrixDashboard平台
- 5.2 断路器演示
- 6、下一篇:SpringCloud 2020-Gateway新一代网关(笔记)
- 视频地址:
Hystrix在消费侧和服务侧都能添加
1、概述
1、分布式系统面临的问题
2、是什么:
3、能干嘛:服务降级、服务熔断、接近实时的监控
4、官网资料:https://github.com/Netflix/Hystrix/wiki/How-To-Use
5、Hystrix官宣,停更进维:https://github.com/Netflix/Hystrix
2、Hystrix重要概念
3、hystrix案例
3.1 构建
1、新建cloud-provider-hystrix-payment8001
2、POM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId>com.liukai.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-hystrix-payment8001</artifactId> <dependencies> <!--新增hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.liukai.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3、YML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18server: port: 8001 eureka: client: register-with-eureka: true #表识不向注册中心注册自己 fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 service-url: # defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 # server: # enable-self-preservation: false spring: application: name: cloud-provider-hystrix-payment # eviction-interval-timer-in-ms: 2000
4、主启动PaymentHystrixMain8001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.liukai.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @author liukai * @version 1.0.0 * @ClassName PaymentHystrixMain8001.java * @Description TODO * @createTime 2021年03月22日 20:25:00 */ @SpringBootApplication @EnableEurekaClient public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentHystrixMain8001.class); } }
5、业务类service,新建service包,下面新建PaymentService类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package com.liukai.springcloud.service; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** * @author liukai * @version 1.0.0 * @ClassName PaymentService.java * @Description TODO * @createTime 2021年03月22日 20:27:00 */ @Service public class PaymentService { //成功 public String paymentInfo_OK(Integer id) { return "线程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id: " + id + "t" + "哈哈哈"; } //失败 public String paymentInfo_TimeOut(Integer id) { int timeNumber = 3; try { TimeUnit.SECONDS.sleep(timeNumber); } catch (Exception e) { e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id: " + id + "t" + "呜呜呜" + " 耗时(秒)" + timeNumber; } }
6、业务类controller,新建controller包,下面新建PaymentService类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.liukai.springcloud.controller; import com.liukai.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author liukai * @version 1.0.0 * @ClassName PaymentController.java * @Description TODO * @createTime 2021年03月22日 20:28:00 */ @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @GetMapping("/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id){ String result = paymentService.paymentInfo_OK(id); log.info("*******result:"+result); return result; } @GetMapping("/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentService.paymentInfo_TimeOut(id); log.info("*******result:"+result); return result; } }
7、按照以上步骤新建cloud-provider-hystrix-payment8002,将端口改为8002
8、正常测试,依次启动7001,7002,8001,8002
访问 http://localhost:8001/payment/hystrix/ok/31
http://localhost:8001/payment/hystrix/timeout/31
8001自测通过,同理访问8002进行自测
3.2 高并发测试(搞事情【滑稽】)
以上述为根基平台,从正确->错误->降级熔断->恢复
1、Jmeter压测测试:Jmeter下载
2、解压双击jmeter.bat
Ctrl + s保存
3、访问http://localhost:8001/payment/hystrix/ok/31 ,本来之前不用等待,可以直接得到结果,但是现在需要等待了。
原因:tomcat的默认的工作线程数被打满了,没有多余的线程来分解压力和处理。
4、结果:上面还是服务提供者8001自己测试,假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死
3.3 80端口加入高并发测试
1、新建cloud-consumer-feign-hystrix-order80
2、POM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId>com.liukai.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-hystrix-order80</artifactId> <dependencies> <!--新增hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.liukai.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3、YML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17server: port: 80 eureka: client: register-with-eureka: true #表识不向注册中心注册自己 fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 # defaultZone: http://eureka7001.com:7001/eureka/ spring: application: name: cloud-provider-hystrix-order
4、主启动类OrderHystrixMain80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package com.liukai.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author liukai * @version 1.0.0 * @ClassName OrderHystrixMain80.java * @Description TODO * @createTime 2021年03月22日 21:41:00 */ @SpringBootApplication @EnableFeignClients public class OrderHystrixMain80 { public static void main(String[] args) { SpringApplication.run(OrderHystrixMain80.class,args); } }
5、80端口新建service包,下面新建PaymentService 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.liukai.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author liukai * @version 1.0.0 * @ClassName PaymentHystrixService.java * @Description TODO * @createTime 2021年03月22日 21:43:00 */ @Component @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT") public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id); }
6、80端口新建controller包,下面新建OrderHystrixController类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.liukai.springcloud.controller; import com.liukai.springcloud.service.PaymentHystrixService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author liukai * @version 1.0.0 * @ClassName OrderHystrixController.java * @Description TODO * @createTime 2021年03月22日 21:45:00 */ @RestController @Slf4j public class OrderHystrixController { @Resource private PaymentHystrixService paymentHystrixService; @Value("${server.port}") private String serverPort; @GetMapping("/consumer/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_OK(id); log.info("*******result:"+result); return result; } @GetMapping("/consumer/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_TimeOut(id); log.info("*******result:"+result); return result; } }
7、测试:访问 http://localhost/consumer/payment/hystrix/ok/31
多访问几次,会发现端口号在 8001和8002之间变化
在jmeter中刚才的线程池中新建一个HTTP REQUEST,访问8002端口
然后启动jmeter,浏览器访问 http://localhost/consumer/payment/hystrix/ok/31,可以发现无论consumer访问返回的是哪一个端口,都会等待一会儿。有可能会出现如下错误
3.4 服务降级(@HystrixCommand)
3.4.1 8001和8002实现服务降级
设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback
1、修改8001和8002端口的PaymentService类,为paymentInfo_TimeOut方法添加以下注解
2、新建方法paymentInfo_TimeOutHandler作为paymentInfo_TimeOut出问题的兜底方法,即:当paymentInfo_TimeOutHandler出问题就会访问paymentInfo_TimeOutHandler方法,在里面返回数据给调用方。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22@Value("${server.port}") private String serverPort; //失败 @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //3秒钟以内就是正常的业务逻辑 }) public String paymentInfo_TimeOut(Integer id) { int timeNumber = 5; try { TimeUnit.SECONDS.sleep(timeNumber); } catch (Exception e) { e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,端口: " + serverPort + "t" + "呜呜呜" + " 耗时(秒)" + timeNumber; } public String paymentInfo_TimeOutHandler(Integer id) { return "线程池:" + Thread.currentThread().getName() + " 系统繁忙请稍后再试,端口: " + serverPort + "t" + "超时呜呜呜"; }
3、主启动类添加@EnableCircuitBreaker注解
4、测试,访问 http://localhost:8001/payment/hystrix/timeout/31
等几秒之后,返回以下数据,控制台也会抛出异常,同理访问8002端口,结果一样。
添加一个10/0的异常,还是会执行paymentInfo_TimeOutHandler这个兜底方法
一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
3.4.2 cloud-consumer-feign-hystrix-order80端实现服务降级
80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护
我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务
1、修改cloud-consumer-feign-hystrix-order80的yml文件
1
2
3
4feign: hystrix: enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
2、cloud-consumer-feign-hystrix-order80的主启动类添加@EnableHystrix注解
3、修改业务类OrderHystrixController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59package com.liukai.springcloud.controller; import com.liukai.springcloud.service.PaymentHystrixService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author liukai * @version 1.0.0 * @ClassName OrderHystrixController.java * @Description TODO * @createTime 2021年03月22日 21:45:00 */ @RestController @Slf4j public class OrderHystrixController { @Resource private PaymentHystrixService paymentHystrixService; @Value("${server.port}") private String serverPort; @GetMapping("/consumer/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_OK(id); log.info("*******result:"+result); return result; } // @GetMapping("/consumer/payment/hystrix/timeout/{id}") // public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ // String result = paymentHystrixService.paymentInfo_TimeOut(id); // log.info("*******result:"+result); // return result; // } @GetMapping("/consumer/payment/hystrix/timeout/{id}") @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //3秒钟以内就是正常的业务逻辑 }) public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_TimeOut(id); return result; } //兜底方法 public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){ return "我是消费者80,对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬_┬)"; } }
4、测试: 访问 http://localhost/consumer/payment/hystrix/timeout/31
3.4.3 解决兜底方法代码膨胀问题
问题:每一个方法都单独写一个兜底的方法,不仅兜底方法和逻辑方法混杂在一起,而且还会写多个兜底方法
解决办法:写一个全局的兜底方法,使用@DefaultProperties(defaultFallback注解
1、修改cloud-consumer-feign-hystrix-order80的controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67package com.liukai.springcloud.controller; import com.liukai.springcloud.service.PaymentHystrixService; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author liukai * @version 1.0.0 * @ClassName OrderHystrixController.java * @Description TODO * @createTime 2021年03月22日 21:45:00 */ @RestController @Slf4j @DefaultProperties(defaultFallback = "payment_Global_FallbackMethod") public class OrderHystrixController { @Resource private PaymentHystrixService paymentHystrixService; @Value("${server.port}") private String serverPort; @GetMapping("/consumer/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_OK(id); log.info("*******result:"+result); return result; } // @GetMapping("/consumer/payment/hystrix/timeout/{id}") // public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ // String result = paymentHystrixService.paymentInfo_TimeOut(id); // log.info("*******result:"+result); // return result; // } @GetMapping("/consumer/payment/hystrix/timeout/{id}") @HystrixCommand() // @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = { // @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //3秒钟以内就是正常的业务逻辑 // }) public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_TimeOut(id); return result; } //兜底方法 public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){ return "我是消费者80,对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬_┬)"; } //下面是全局fallback兜底方法 public String payment_Global_FallbackMethod(){ return "Global异常处理信息,请稍后再试,(┬_┬)"; } }
2、测试: 依次启动7001,7002,8001,8002,80。访问:http://localhost/consumer/payment/hystrix/timeout/31
可以发现全局全局兜底方法设置成功
3.4.4 解决兜底方法代码混乱问题
1、修改cloud-consumer-feign-hystrix-order80
根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理。即调用服务的时候出现异常,就交给该方法的具体实现去处理异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.liukai.springcloud.service; import org.springframework.stereotype.Component; /** * @author liukai * @version 1.0.0 * @ClassName PaymentFallbackService.java * @Description TODO * @createTime 2021年03月23日 00:51:00 */ @Component public class PaymentFallbackService implements PaymentHystrixService { @Override public String paymentInfo_OK(Integer id) { return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)"; } @Override public String paymentInfo_TimeOut(Integer id) { return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)"; } }
2、修改PaymentHystrixService接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.liukai.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author liukai * @version 1.0.0 * @ClassName PaymentHystrixService.java * @Description TODO * @createTime 2021年03月22日 21:43:00 */ @Component @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class) public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") public String paymentInfo_OK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id); }
3、测试:依次启动7001,7002,8001,8002,80。访问 http://localhost/consumer/payment/hystrix/ok/31
可以发现访问正常。关闭8001,8002,再次访问上面的连接。
因为8001,8002没有启动,相当于宕机了,此时任何访问抛出异常。
3.5 服务熔断
3.5.1 概念
是什么
大神论文https://martinfowler.com/bliki/CircuitBreaker.html
3.5.2 实现服务熔断
1、修改cloud-provider-hystrix-payment8001
2、修改PaymentService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71package com.liukai.springcloud.service; import cn.hutool.core.util.IdUtil; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable; import java.util.concurrent.TimeUnit; /** * @author liukai * @version 1.0.0 * @ClassName PaymentService.java * @Description TODO * @createTime 2021年03月22日 20:27:00 */ @Service public class PaymentService { @Value("${server.port}") private String serverPort; //成功 public String paymentInfo_OK(Integer id) { return "线程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id: " + id + "t" + "哈哈哈"; } //失败 @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //3秒钟以内就是正常的业务逻辑 }) public String paymentInfo_TimeOut(Integer id) { int timeNumber = 5; try { TimeUnit.SECONDS.sleep(timeNumber); } catch (Exception e) { e.printStackTrace(); } return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,端口: " + serverPort + "t" + "呜呜呜" + " 耗时(秒)" + timeNumber; } public String paymentInfo_TimeOutHandler(Integer id) { return "线程池:" + Thread.currentThread().getName() + " 系统繁忙请稍后再试,端口: " + serverPort + "t" + "超时呜呜呜"; } //服务熔断 @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器 @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间范围 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸 // 上述意思就是开启服务熔断,在10秒内,请求10此失败率达到60%以上熔断器打开,此时拒绝访问, // 同样,在接下来的10秒内,熔断器半开状态,如果可以访问的错误率达到60%以下,就开始放行,关闭熔断器, }) public String paymentCircuitBreaker(@PathVariable("id") Integer id){ if (id < 0){ throw new RuntimeException("*****id 不能负数"); } String serialNumber = IdUtil.simpleUUID(); return Thread.currentThread().getName()+"t"+"调用成功,流水号:"+serialNumber; } public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){ return "id 不能负数,请稍候再试,(┬_┬)/~~ id: " +id; } }
3、为什么@HystrixProperty后面配置这些参数
在HystrixCommandProperties这个类里面可以查看可以配置哪些参数
4、修改PaymentController,添加如下方法
1
2
3
4
5
6
7
8//===服务熔断 @GetMapping("/payment/circuit/{id}") public String paymentCircuitBreaker(@PathVariable("id") Integer id){ String result = paymentService.paymentCircuitBreaker(id); log.info("*******result:"+result); return result; }
5、测试:自测cloud-provider-hystrix-payment8001,启动7001,7002,8001。访问 http://localhost:8001/payment/circuit/31
访问 http://localhost:8001/payment/circuit/-31
重点测试: 多次错误,然后慢慢正确,发现刚开始不满足条件,就算是正确的访问地址也不能进行访问,需要慢慢的恢复链路。
10几秒内疯狂访问http://localhost:8001/payment/circuit/-31,再去访问 http://localhost:8001/payment/circuit/31,会发现参数为正数的也不能正常访问了。
然后等几秒中,参数为正数的又能正常访问了。
3.5.3 总结
大神结论:
官网断路器流程图
断路器在什么情况下开始起作用:
断路器开启或者关闭的条件:
断路器打开之后:
All配置:
3.6 服务限流(暂无)
4、hystrix工作流程
https://github.com/Netflix/Hystrix/wiki/How-it-Works
hystrix工作流程
5、服务监控hystrixDashboard
5.1 搭建hystrixDashboard平台
1、新建cloud-consumer-hystrix-dashboard9001
2、pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId>com.liukai.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId> <dependencies> <!--新增hystrix dashboard--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3、yml
1
2
3server: port: 9001
4、主启动类 HystrixDashboardMain9001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.liukai.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /** * @author liukai * @version 1.0.0 * @ClassName HystrixDashboardMain9001.java * @Description TODO * @createTime 2021年03月23日 14:23:00 */ @SpringBootApplication @EnableHystrixDashboard public class HystrixDashboardMain9001 { public static void main(String[] args) { SpringApplication.run(HystrixDashboardMain9001.class); } }
5、所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置
1
2
3
4
5<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
6、启动cloud-consumer-hystrix-dashboard9001,访问 http://localhost:9001/hystrix
5.2 断路器演示
修改cloud-provider-hystrix-payment8001
1、修改cloud-provider-hystrix-payment8001的主启动类,添加如下代码
1
2
3
4
5
6
7
8
9
10@Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
2、监控测试,启动7001,7002,8001。
在http://localhost:9001/hystrix的页面填写如下数据。
连续多次访问 http://localhost:8001/payment/circuit/31
点击一下按钮
就能看见8001的访问情况
也可以先连续访问几次参数为负数的情况,造成熔断器打开,然后在访问参数为正数的情况,可以在此界面看到熔断器的状态,以及各种信息
3、如何分析
6、下一篇:SpringCloud 2020-Gateway新一代网关(笔记)
SpringCloud 2020-Gateway新一代网关(笔记)
视频地址:
此笔记的视频地址:尚硅谷SprngCloud(H版&alibaba框架开发教程(大牛讲授SpringCloud))
最后
以上就是舒心衬衫最近收集整理的关于SpringCloud 2020-Hystrix断路器(笔记)的全部内容,更多相关SpringCloud内容请搜索靠谱客的其他文章。
发表评论 取消回复