SpringCloud Hystrix
⒈Hystrix是什么?
Hystrix使一个用于处理分布式系统的延迟和容错的开源库。在分布式系统里,许多依赖不可避免的因服务超时、服务异常等导致调用失败,Hystrix能够保证在一个依赖出现问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
⒉断路器&服务熔断
“断路器”本身使一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似于熔断保险丝),向服务调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要的占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回“错误”的响应信息,当检测到该节点的微服务调用响应正常后恢复调用链路。在SpringCloud框架里,熔断机制通过Hystrix实现,Hystrix会监控微服务间调用的状况,当失败的调用达到一定的阈值(默认是5秒内20次调用失败),就会启动熔断机制。
⒊示例
①在服务提供者项目中添加Hystrix starter依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 8 </dependency> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 12 </dependency>
②在服务提供者项目中对控制器中的Action方法指定熔断调用方法
1 package cn.coreqi.controller; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import java.util.List; 10 11 @RestController 12 public class UserController { 13 @Autowired 14 private UserService userService; 15 16 @GetMapping("/users") 17 @HystrixCommand(fallbackMethod = "getUsersFallback") //一旦服务消费者远程调用该方法失败并抛出错误信息后,Hystrix会自动调用@HystrixCommand注解fallbackMethod属性中标注的方法返回 18 public List<User> getUsers(){ 19 throw new NullPointerException(); 20 //return userService.getList(); 21 } 22 23 public List<User> getUsersFallback(){ 24 return null; 25 } 26 }
③在主程序启动类上添加@EnableCircuitBreaker注解
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 8 @SpringBootApplication 9 @EnableEurekaClient //启用Eureka客户端功能 10 @EnableCircuitBreaker //对Hystrix熔断机制的支持 11 public class SpringbootcloudserviceproviderApplication { 12 13 public static void main(String[] args) { 14 SpringApplication.run(SpringbootcloudserviceproviderApplication.class, args); 15 } 16 17 }
⒋服务降级
服务器整体资源快不够了,将某些服务先关掉,待渡过难关再开启回来,服务降级处理是在服务消费者实现完成的,与服务提供者没有关系
⒌示例
①在服务消费者配置文件中开启
1 feign.hystrix.enabled=true
②编写回退方法
1 package cn.coreqi.fallbackfactory; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import feign.hystrix.FallbackFactory; 6 import org.springframework.stereotype.Component; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 11 @Component 12 public class UserServiceFallbackFactory implements FallbackFactory<UserService> { 13 @Override 14 public UserService create(Throwable throwable) { 15 return new UserService() { 16 @Override 17 public void addUser(User user) { 18 19 } 20 21 @Override 22 public void delById(Integer id) { 23 24 } 25 26 @Override 27 public void modifyUser(User user) { 28 29 } 30 31 @Override 32 public User getById(Integer id) { 33 return null; 34 } 35 36 @Override 37 public List<User> getList() { 38 List<User> userList = new ArrayList<>(); 39 userList.add(new User(0,"Error","Error",0)); 40 return userList; 41 } 42 }; 43 } 44 }
③在@FeignClient注解中指定fallbackFactory的属性值
1 package cn.coreqi.feign; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.fallbackfactory.UserServiceFallbackFactory; 5 import org.springframework.cloud.openfeign.FeignClient; 6 import org.springframework.web.bind.annotation.GetMapping; 7 8 import java.util.List; 9 10 @FeignClient(value = "USER-PROVIDER",fallbackFactory = UserServiceFallbackFactory.class) //指定微服务实例名称 11 public interface UserFeignClient { 12 @GetMapping("/users") //指定调用微服务的服务地址 13 public List<User> getList(); 14 }
⒍服务监控-Hystrix Dashboard
Hystrix提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续的记录所有通过Hystrix 发起请求的执行信息,并以统计报表和图形的形式展示给用户,Netfilx通过hystrix-metrics-event-stream项目实现了对以上指标的监控,Spring Cloud也提供了对Hystrix Dashboard的整合,对监控内容转化为可视化界面。
7示例
①新建监控项目添加依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 8 </dependency> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 12 </dependency>
②配置文件中指定运行端口
1 server.port=9001
③主程序启动类添加@EnableHystrixDashboard注解
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 6 7 @SpringBootApplication 8 @EnableHystrixDashboard 9 public class HystrixDashboardApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(HystrixDashboardApplication.class, args); 13 } 14 15 }
④对所有需要监控的服务提供者项目添加以下依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-actuator</artifactId> 4 </dependency>
⑤访问监控项目web管理地址
http://localhost:9001/hystrix
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10484825.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!