Hystrix
Hystrix 是 Netflix 的断路器,提供服务降级、熔断功能。
如何引入?
- 引入依赖
org.springframework.cloud:spring-cloud-starter-netflix-hystrix。 - 启动类添加
@EnableCircuitBreaker注解。
Hystrix 服务降级
发生异常、调用超时(默认1s)、线程不足时,会发生服务降级,服务降级时,Hystrix 会执行配置的方法,快速返回结果。
@Service
@DefaultProperties(defaultFallback = "orderFail") // 默认的降级配置
public class SimpleOrderService {
@HystrixCommand(fallbackMethod = "orderFail")
public String order(String goodCode) {
// dosth....
return "ok";
}
public String orderFail(String goodCode) {
return "下单失败";
}
}
Hystrix 服务熔断
发生多次降级达到熔断条件后,断路器断开,再次调用方法不会被执行,而是直接返回降级结果。一段时间后,断路器切换到半开模式,尝试放行一部分流量,成功执行后,断路器恢复正常,否则再次切换到断开状态。
Hystrix 服务限流
略…
Hystrix Dashboard
Hystrix Dashboard 是一个图形化的监控组件。

如何引入?
- 引入依赖
org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard。 - 启动类添加注解
@EnableHystrixDashboard。 application.propeties配置监控白名单。
hystrix.dashboard.proxy-stream-allow-list=**
- 被监控应用添加
actuator依赖,配置监控路径。
@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;
}

本文详细介绍了Netflix的Hystrix断路器,包括服务降级、熔断及限流功能。通过示例代码展示了如何配置服务降级方法,以及断路器的工作原理。同时,介绍了HystrixDashboard的使用,用于实时监控断路器状态。
1022

被折叠的 条评论
为什么被折叠?



