SpringCloud学习笔记三:Spring Cloud Hystrix 熔断器

 

Spring Cloud Hystrix 熔断器的用处

当服务A依赖服务B和服务C,而服务B和服务C又依赖其他服务时,这时服务B发生了异常,就会导致整体流程阻塞在服务B处,服务A中堆积了大量的请求,最终导致一系列问题的发生。

Hystrix就是为了保证给服务加上异常保护措施,一但某个服务发生了异常,就会启用该服务的备用方案,不至于阻塞整体流程。

 

在上一篇中(《SpringCloud学习笔记二:Spring Cloud Eureka 服务治理》),我们使用Eureka搭建了SpringCloud的服务治理功能,现在我们基于上一章的代码,讲一讲如何搭建Hystrix的服务熔断工程。

Spring Cloud Hystrix工程的搭建

在上一讲的ribbon工程基础上,添加

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

启动类中加上

@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;
}

编码中忌讳重复造轮子,所以我们用Feign进行封装,(Feign是一个声明式Web服务客户端,旨在使编写Java Http客户端变得更加容易)

新增接口 SchedualServiceHi

@Service
@FeignClient(value = "hello-service",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

再写实现类 SchedualServiceHiHystric

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

最后在controller中添加服务方法

@Autowired
HelloService helloService;

@RequestMapping(value = "/hi")
public String hi(@RequestParam String name) {
    return helloService.hiService(name);
}

@Autowired
SchedualServiceHi schedualServiceHi;

@RequestMapping(value = "/hi2", method = RequestMethod.GET)
public String sayHi(@RequestParam String name) {
    return schedualServiceHi.sayHiFromClientOne(name);
}

至此,我们启动SpringCloud注册服务工程、Eureka服务工程和刚刚写好的Hystrix工程。

调用接口 http://localhost:8101/hi?name=bruce 显示

 

代表服务正常运行。

此时,我们关掉Eureka服务工程,再次调用 http://localhost:8101/hi?name=bruce 接口

会发现服务已经跳转到HiError方法中。

其中主要的逻辑就在于代码中,通过实现Feign的注解,在服务发生异常的时候,自动调用SchedualServiceHiHystric类中的sayHiFromClientOne方法,实现了服务的熔断。

如何查看Hystrix的监控面板

首先访问 http://localhost:8101/hystrix,进入到Hystrix页面

在第一个输入框中,我们输入http://localhost:8101/hystrix.stream,在title中,我们随便起一个名称,点击确认,进入监控面板页面。

再次访问服务接口http://localhost:8101/hi2?name=bruce,页面会发生相应变化。

一圈–实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。

该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化。流量越大该实心圆也就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。

曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。

详细参数说明如下:

参考资料

《Hystrix使用详解》

《SpringCloud学习笔记(3)——Hystrix》

《SpringCloud-容错处理Hystrix熔断器(五)》

《史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)》

《SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard》

发布了60 篇原创文章 · 获赞 57 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qixinbruce/article/details/87926622