SpringCloud的Hystrix服务降级和熔断
一、原理:
一个单体服务,拆成多个微服务A、B、C,一切正常的时候还好,万一A有个功能需要调用B的方法,
B的这个方法又要调用C的方法。这个时候C出幺蛾子了,B一直请求不到C就没办法返回结果,A又不断的在请求B。
这个时候就会耗尽资源,导致整个系统崩溃掉,就是所谓的雪崩效应。
那么有什么防止措施吗?那就用Hystrix服务降级和熔断吧。
这东西听起来好像有点高级,其实说白了,
熔断就是请求一个服务的时候, 如果多次请求都还是请求失败,那么就让后面的不要再请求了。
降级就是,请求在规定时间内还没有返回结果的话,就执行另外一个方法(用于快速返回结果,不要一直堵在那里!)
二、实现:
1、pom引入依赖:
1 2 3 4 | <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> |
2、启动类引入注解:
1 | @EnableCircuitBreaker :用于启动熔断功能(就和断路器差不多,压力大了就不干了,关掉,免得失火) |
3、yml配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #feign: # hystrix: # enabled: true #hystrix: # command: # default : # execution: # isolation: # thread: # timeoutInMilliseconds: 10000 #超时时间 # circuitBreaker: # requestVolumeThreshold: 10 #超时时间内,多少个请求失败后,打开断路器 # sleepWindowInMilliseconds: 5000 #打开短路器后,多少秒开始尝试恢复 # errorThresholdPercentage: 50 #在滚动时间内,失败频率达到百分之 50 ,就打开断路器 # metrics: # rollingStats: # timeInMilliseconds: 10000 #设置滚动时间,10000ms,就是在这个时间内, #requestVolumeThreshold: 10 ,有 10 次请求。 #失败率是 errorThresholdPercentage: 50 ,百分之 50 ,就打开断路器。 |
4、代码:
1、通过@HystrixCommand来实现退回机制:
1 2 3 4 5 6 7 8 9 10 11 12 | @HystrixCommand (fallbackMethod = "fallback" ) @GetMapping ( "/postRestTemplateRibbonConsumer" ) public String postRestTemplateRibbonConsumer(){ Map<String,String> paramMap = new HashMap(); paramMap.put( "start" , "1" ); paramMap.put( "page" , "5" ); return this .restTemplateRibbon.postForObject( "http://eureka-client/" + "/postDcRequestBody" ,paramMap,String. class ); } public String fallback() { return "fallback" ; } |
2、通过@FeignClient(value =“eureka-client”,fallback = FeignFallBack.class),指定退回类,实现退回机制:
- 定义一个Feign接口:
1 2 3 4 5 6 7 8 9 | @FeignClient (value = "eureka-client" ,fallback = FeignFallBack. class ) public interface FeignInterface { @PostMapping ( "/postDcRequestBody" ) public String postDcRequestBody( @RequestBody Map<String,String> paramMap); @PostMapping ( "/postFeignClientConsumerHystrix" ) public String postFeignClientConsumerHystrix(); } |
- 创建一个FeignFallBack,实现接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Component public class FeignFallBack implements FeignInterface { @Override public String postDcRequestBody(Map<String, String> paramMap) { return "调用postDcRequestBody失败!" ; } @Override public String postFeignClientConsumerHystrix() { return "调用postFeignClientConsumerHystrix失败!" ; } } |
- 调用接口方法:
1 2 3 4 5 6 7 8 9 10 11 | /** * post请求(Feign接口) * @return */ @GetMapping ( "/postFeignClientConsumer" ) public String postFeignClientConsumer(){ Map<String,String> paramMap = new HashMap(); paramMap.put( "start" , "1" ); paramMap.put( "page" , "5" ); return this .feignInterface.postDcRequestBody(paramMap); } |
说明:方法请求,在规定的时间内,没有返回,就触发降级,执行对应的降级方法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具