千家信息网

SpringCloud中如何使用Hystrix熔断器

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇文章为大家展示了SpringCloud中如何使用Hystrix熔断器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、简介什么是Hystrix单词本身意思
千家信息网最后更新 2026年11月03日SpringCloud中如何使用Hystrix熔断器

本篇文章为大家展示了SpringCloud中如何使用Hystrix熔断器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一、简介

什么是Hystrix
  1. 单词本身意思就是断路器,发音 [hɪst'rɪks] ,怎么读听这里 https://www.koolearn.com/dict/wd_73657.html

  2. Hystrix是NetFlix公司开源的项目,提供了熔断器的功能,能够阻止分布式系统中出现的联动故障。

  3. Hystrix通过隔离服务的访问点阻止联动故障,并且提供故障解决方案,提高了分布式系统的弹性和容错。

Hystrix所解决的问题
  1. 防止服务故障导致资源耗尽。

    通过熔断机制,可以防止单个服务故障导致影响整个Servlet容器的线程资源。

  2. 避免过长等待。

    快速失败机制,如果某个服务故障,则接下来该服务不会处理请求,快速的返回失败。

  3. 增加服务弹性。

    • 提供了回退方案,请求发生故障时,可以调用fallback处理。

    • 提供熔断机制,防止故障传播到其他服务。

    • 熔断后不断尝试,特定条件下可以自动修复服务。

  4. 提供监控。

    提供熔断器的监控组件 Hystrix Dashboard,可以实时监控断路器的状态。

二、简单使用

无论是Ribbon使用还是Feign使用,都需要前面两步

  1. pom.xml中增加依赖

    org.springframework.cloud    spring-cloud-starter-hystrix
  1. 启动类增加注解表示开启熔断器功能

@SpringBootApplication@EnableEurekaClient@EnableHystrixpublic class ServerApplication {    private static final Logger logger = LoggerFactory.getLogger(ServerApplication.class);    public static void main(String[] args) {        SpringApplication.run(ServerApplication.class,args);        logger.info("hystrix server start up finished !");    }}
Ribbon搭配使用
@Autowiredprivate RestTemplate restTemplate;/** * 每个需要熔断的远程方法上,增加注解,定义回调 * @HystrixCommand定义服务降级,这里的fallbackMethod服务调用失败后调用的方法。 * @return */@HystrixCommand(fallbackMethod = "hiError")public String getHi(){    return restTemplate.getForObject("http://hystrix-service/hi", String.class);}/** * fallbackMethod指向此方法,表示远程调用失败后,快速的使用此方法替代 */public String hiError(){    return "error";}
Fiegn搭配使用
  1. 在远程服务的FeignClient的注解上增加回调,定义回调类

@FeignClient(value = "common-service",              configuration = FeignConfiguration.class,              fallback = RemoteCommonServiceCallback.class)public interface RemoteCommonServiceClient {    /**     * 调用远程方法     * @return     */    @GetMapping(value = "/hello")    String remoteCommonServiceHello();}
  1. 实现FeignClient中定义的回调类

/** * 

* @Component注册为一个组件,实现RemoteCommonServiceClient,重写方法定义回调 *

* @author Calvin * @date 2019/10/24 * @since 1.0 */@Componentpublic class RemoteCommonServiceCallback implements RemoteCommonServiceClient { @Override public String remoteCommonServiceHello() { return "something error, this is callback"; }}

  1. application.yml中开启Feign对Hystrix的支持

feign:  hystrix:    enabled: true

具体测试,可以通过停掉需要被调用的远程服务来测试,或者自己制造一些,服务在某段时间不可用,使线程睡眠等操作

三、监控

Hystrix Dashboard
  1. 增加依赖

    org.springframework.boot    spring-boot-starter-actuator    org.springframework.cloud    spring-cloud-starter-hystrix-dashboard
  1. 启动类增加注解开启dashboard

/** * 

* 启动类,集合了服务注册、FeignClient声明式调用、开启熔断器、开启熔断器监控 *

* * @author Calvin * @date 2019/10/24 * @since 1.0 */@SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableHystrix@EnableHystrixDashboardpublic class HystrixServerApplication { private static final Logger logger = LoggerFactory.getLogger(HystrixServerApplication.class); public static void main(String[] args) { SpringApplication.run(HystrixServerApplication.class,args); logger.info("hystrix server start up finished !"); }}
  1. 监控页面观察

  • step1:浏览器输入http://localhost:8080/hystrix

  • step2:页面填入 http://localhost:8080/hystrix.stream Dalay和Title自己填

  • step3: 没有step3,以下是踩过的坑

    1. 地址栏输入的和页面填进去的东西一定不是同一个,输错了只能看到一直在ping,就像这样

    2. 刚进去的时候是loading状态的时候表着急,就像下图,你只需要调用一下远程服务

    3. 指标意义

      注:此图来源 https://blog.csdn.net/wm6752062/article/details/86136204 侵删

Turbine聚合监控
  1. 新建服务turbine-monitor

  2. pom.xml

    hystrix-test    com.calvin.hystrix    1.0-SNAPSHOT4.0.0turbine-monitor            org.springframework.cloud        spring-cloud-starter-turbine                org.springframework.cloud        spring-cloud-starter-hystrix                org.springframework.cloud        spring-cloud-starter-hystrix-dashboard                org.springframework.boot        spring-boot-starter-actuator                            org.springframework.boot            spring-boot-maven-plugin            
  1. TurbineMonitorApplication

/** * 

* 启动类 *

* * @author Calvin * @date 2019/10/24 * @since */@SpringBootApplication@EnableTurbine@EnableHystrix@EnableHystrixDashboardpublic class MonitorApplication { public static void main(String[] args) { SpringApplication.run(MonitorApplication.class,args); }}
  1. application.yml

spring:  application:    name: turbine-monitorserver:  port: 8040eureka:  client:    service-url:      defaultZone: http://localhost:8010/eureka/turbine:# 需要监控的服务名  app-config: common-service, hystrix-service# 服务名的集群  cluster-name-expression: new String("default")
  1. 启动服务

  • 浏览器:http://localhost:8040/hystrix



  • 调用远程服务

上述内容就是SpringCloud中如何使用Hystrix熔断器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

服务 故障 监控 熔断器 方法 注解 机制 页面 内容 分布式 功能 就是 弹性 技能 断路器 方案 时候 此方法 浏览器 状态 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 南京欣网互联网络科技 互联网科技工作委员会主任 互联网的科技创新 美国互联网黑科技 有木互联网科技 金融科技和互联网金融 科技大学互联网专业好不好 栖莱科技互联网股份有限公司 港股科技和互联网 荆州市昕露互联网络科技有限公司 移动互联网科技公司 开一家科技互联网公司需要多少钱 互联网科技和信息 保险科技服务互联网营销体系 互联网科技接地气 万物互联网智能科技 银川新华互联网科技学校原画 保山浩联互联网科技有限公司 青岛互联网科技园 科技美学互联网 艺术 浩特互联网科技 远拓互联网科技 成都狮子座互联网科技 花神(南京)互联网科技有限公司 广州市恰铎互联网科技有限公司 企晟互联网信息科技有限公司 四川慕妮娅互联网科技有限公司 广东工业互联网大赛深圳学泰科技 中天科技中国工业互联网 银川新华互联网科技学校原画
0