3.1-Hystrix-概述
• Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。
• 雪崩:一个服务失败,导致整条链路的服务都失败的情形
Hystix 主要功能
• 隔离
线程池隔离
信号量隔离
• 降级:异常,超时
• 熔断
• 限流


3.2-Hystrix-降级
3.2.1-提供方降级
Hystix 降级:当服务发生异常或调用超时,返回默认数据
服务提供方降级
在服务提供方,引入 hystrix 依赖
<!-- hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
定义降级方法
/*** 定义降级方法:* 1. 方法的返回值需要和原方法一样* 2. 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}
- 使用 @HystrixCommand 注解配置降级方法
/*** 降级:* 1. 出现异常* 2. 服务调用超时* * 默认1s超时** @HystrixCommand(fallbackMethod = "findOne_fallback")* fallbackMethod:指定降级后调用的方法名称*/@GetMapping("/findOne/{id}")@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public Goods findOne(@PathVariable("id") int id){//1.造个异常int i = 3/0;try {//2. 休眠2秒Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}Goods goods = goodsService.findOne(id);goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上return goods;}
- 在启动类上开启Hystrix功能:@EnableCircuitBreaker
package com.itheima.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/*** 启动类*/@EnableEurekaClient //该注解 在新版本中可以省略@SpringBootApplication@EnableCircuitBreaker // 开启Hystrix功能public class ProviderApp {public static void main(String[] args) {SpringApplication.run(ProviderApp.class,args);}}
3.2.2-消费方降级
- feign 组件已经集成了 hystrix 组件。
- 定义feign 调用接口实现类,复写方法,即 降级方法
GoodsFeignClientFallback
package com.itheima.consumer.feign;import com.itheima.consumer.domain.Goods;import org.springframework.stereotype.Component;/*** Feign 客户端的降级处理类* 1. 定义类 实现 Feign 客户端接口* 2. 使用@Component注解将该类的Bean加入SpringIOC容器*/@Componentpublic class GoodsFeignClientFallback implements GoodsFeignClient {@Overridepublic Goods findGoodsById(int id) {Goods goods = new Goods();goods.setTitle("又被降级了~~~");return goods;}}
- 在 @FeignClient 注解中使用 fallback 属性设置降级处理类。
GoodsFeignClient
package com.itheima.consumer.feign;import com.itheima.consumer.domain.Goods;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)public interface GoodsFeignClient {@GetMapping("/goods/findOne/{id}")public Goods findGoodsById(@PathVariable("id") int id);}
- 配置开启 feign.hystrix.enabled = true
application.yml
# 开启feign对hystrix的支持feign:hystrix:enabled: true
3.3-Hystrix-熔断
3.3.1-熔断-概念
• Hystrix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开
断路器,拒绝所有请求,直到服务恢复正常为止。
断路器三种状态:打开、半开、关闭、

3.3.2-熔断-代码演示
修改服务提供方的方法,演示熔断机制
熔断配置
• circuitBreaker.sleepWindowInMilliseconds:监控时间
• circuitBreaker.requestVolumeThreshold:失败次数
• circuitBreaker.errorThresholdPercentage:失败率
GoodsController
package com.itheima.provider.controller;import com.itheima.provider.domain.Goods;import com.itheima.provider.service.GoodsService;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;import org.springframework.beans.factory.annotation.Autowired;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.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;/*** Goods Controller 服务提供方*/@RestController@RequestMapping("/goods")public class GoodsController {@Autowiredprivate GoodsService goodsService;@Value("${server.port}")private int port;/*** 降级:* 1. 出现异常* 2. 服务调用超时* * 默认1s超时** @HystrixCommand(fallbackMethod = "findOne_fallback")* fallbackMethod:指定降级后调用的方法名称*/@GetMapping("/findOne/{id}")@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),//监控时间 默认5000 毫秒@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),//失败次数。默认20次@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),//失败率 默认50%@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50") })public Goods findOne(@PathVariable("id") int id){//如果id == 1 ,则出现异常,id != 1 则正常访问if(id == 1){//1.造个异常int i = 3/0;}/*try {//2. 休眠2秒Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}*/Goods goods = goodsService.findOne(id);goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上return goods;}/*** 定义降级方法:* 1. 方法的返回值需要和原方法一样* 2. 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}}
3.3.3-熔断监控
• Hystrix 提供了 Hystrix-dashboard 功能,用于实时监控微服务运行状态。
• 但是Hystrix-dashboard只能监控一个微服务。
• Netflix 还提供了 Turbine ,进行聚合监控。
熔断器监控安装 请查看Turbine搭建步骤.md

