SpringCloud-07-Hystrix
Hystrix 熔断器
1、Hystrix 概述
Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。
雪崩:一个服务失败,导致整条链路的服务都失败的情形。
Hystix 主要功能:
- 隔离
- 线程池隔离
- 信号量隔离
- 降级:异常,超时
- 熔断
- 限流
2、Hystrix 降级 – 服务提供方
在服务提供方,引入 hystrix 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>定义降级方法
@GetMapping("/{id}") @HystrixCommand(fallbackMethod = "queryById_fallback",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"), @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"), @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"), }) public User queryById(@PathVariable("id") Long id, @RequestHeader(value = "filter",required = false) String filter, @RequestHeader(value = "default-filter",required = false) String default_filter) { // int i=5/0; if(id==1){ int i=1/0; } // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println(filter); System.out.println(default_filter); return userService.queryById(id); } public User queryById_fallback(Long id, String filter, String default_filter) { User user=new User(); user.setUsername("错误用户..."); return user; }使用 @HystrixCommand 注解配置降级方法
@HystrixCommand(fallbackMethod = "queryById_fallback",commandProperties = { @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"), @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"), @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"), })在启动类上开启Hystrix功能:@EnableCircuitBreaker
//@MapperScan("cn.itcast.user.mapper") @SpringBootApplication @EnableCircuitBreaker public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
3、Hystrix 降级 – 服务消费方
feign 组件已经集成了 hystrix 组件。
定义feign 调用接口实现类,复写方法,即 降级方法
在 @FeignClient 注解中使用 fallback 属性设置降级处理类。
@Component public class UserClientCallBack implements UserClient { @Override public User findUserById(Long id) { User user = new User(); user.setUsername("调用端降级..."); return user; } }配置开启 feign.hystrix.enabled = true
4、Hystrix 熔断
Hystrix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。
调节熔断参数:
- circuitBreaker.sleepWindowInMilliseconds:监控时间
- circuitBreaker.requestVolumeThreshold:失败次数
- circuitBreaker.errorThresholdPercentage:失败率
5、Hystrix 熔断监控
Hystrix 提供了 Hystrix-dashboard 功能,用于实时监控微服务运行状态。 但是Hystrix-dashboard只能监控一个微服务。 Netflix 还提供了 Turbine ,进行聚合监控。
标签:
Hystrix
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~