009 SpringCloud 学习笔记5-----Hystrix保护机制
1.概述
Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制。
Hystrix也是Netflix公司的一款组件。
主页:https://github.com/Netflix/Hystrix/
Hystix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败。
2.
例如微服务I发生异常,请求阻塞,用户不会得到响应,则tomcat的这个线程不会释放,于是越来越多的用户请求到来,越来越多的线程会阻塞:
线程隔离 (2)
用户的请求将不再直接访问服务,而是通过线程池中的空闲线程来访问服务,如果线程池已满,或者请求超时
用户的请求故障时,不会被阻塞,更不会无休止的等待或者看到系统崩溃,至少可以看到一个执行结果(例如返回友好的提示信息) 。
服务降级虽然会导致请求失败,但是不会导致阻塞,而且最多会影响这个依赖服务对应的线程池中的资源,对其它服务没有响应。
触发Hystix服务降级的情况:(1)线程池已满 (2)请求超时
4.入门案例
(1)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
(2)开启熔断
(3)
package lucky.service.controller; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import lucky.service.domain.Users; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; import java.util.List; @Controller @RequestMapping(path = "/consumer/user") public class UserController { @Autowired private RestTemplate restTemplate; @RequestMapping(path = "/queryUsersById") @ResponseBody @HystrixCommand(fallbackMethod = "queryUserByIdFallback") public String queryUserById(@RequestParam("id") Integer id){ // 获取ip和端口信息,拼接成服务地址 String baseUrl = "http://SERVICE-PROVIDER/users/queryUsersById?id=" + id; return this.restTemplate.getForObject(baseUrl, String.class); } public String queryUserByIdFallback(Integer id){ return "服务正忙,请稍后再试"; } }
<2>lucky-service-provider停机时
(5)优化----
package lucky.service.controller; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import lucky.service.domain.Users; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; import java.util.List; @Controller @RequestMapping(path = "/consumer/user") @DefaultProperties(defaultFallback = "fallBackMethod") // 指定一个类的全局熔断方法 public class UserController { @Autowired private RestTemplate restTemplate; @RequestMapping(path = "/queryUsersById") @ResponseBody @HystrixCommand // 标记该方法需要熔断 public String queryUserById(@RequestParam("id") Integer id){ // 获取ip和端口信息,拼接成服务地址 String baseUrl = "http://SERVICE-PROVIDER/users/queryUsersById?id=" + id; return this.restTemplate.getForObject(baseUrl, String.class); } /** * 熔断方法 * 返回值要和被熔断的方法的返回值一致 * 熔断方法不需要参数 * @return */ public String fallBackMethod(){ return "服务正忙,请稍后再试"; } }
- @DefaultProperties(defaultFallback = "defaultFallBack"):在类上指明统一的失败降级方法
- @HystrixCommand:在方法上直接使用该注解,使用默认的熔断方法。
- defaultFallback:默认降级方法,不用任何参数,以匹配更多方法,但是返回值一定一致
(6)设置超时
我们可以通过hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
server:
port: 8080
logging:
level:
root: info
spring:
application:
name: service-consumer #注册到eureka后的微服务的名称
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 # 设置hystrix的超时时间为6000ms
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)