Hystrix 断路器

本文详细介绍了Hystrix断路器的工作原理及其在分布式系统中的应用,包括服务降级、熔断、依赖隔离等功能,以及如何在Spring Cloud中进行配置和监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等。如果没有采取任何措施的话就会导致雪崩效应。
服务雪崩效应是一种因“服务提供者的不可用”(原因)导致“服务调用者不可用”(结果),并将不可用逐渐放大的现象
比如A服务调用 B服务,B服务调用C服务,如果C服务出现故障,那么B服务应需调用C服务所以也出现故障 A服务也出现了故障,这就是雪崩效应

如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

Hystrix提供一下几个功能:

  • 服务降级
  • 服务熔断
  • 依赖隔离
  • 监控面板(Hystrix DashBoard)
基本使用

1、依赖

 <dependency>
	 <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</artifactId>
     <version>1.4.5.RELEASE</version>
 </dependency>

2、启动类添加注解

@EnableCircuitBreaker

提示:如果你需要同时使用一下三个注解@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker,那么你可以用@SpringCloudApplication来替换

3、添加fallback

 @HystrixCommand(fallbackMethod = "helloFallBack")
    @RequestMapping("/hello")
    public String hello(){
        throw new RuntimeException();
//        return "hello world, I'm service-demo";
    }


    private String helloFallBack(){
        return "太拥挤了,请稍后再试";
    }

hello方法中,不仅是可以远程调用其他服务中出现连接超时等等异常,可以使用断路,同时自身服务如果出现异常也可以使用fallback来降级

添加默认回退方法

@DefaultProperties(defaultFallback = "defaultFallback")
public class IndexController {

    @HystrixCommand
    @RequestMapping("/hello")
    public String hello(){
        throw new RuntimeException();
    }

    private String defaultFallback(){
        return "出现故障,请稍后再试";
    }
}

还有一个重要的点是设置超时时间,Hystrix默认设置超时时间1s,如果超过就直接降级,其实有很多业务的超时时间都不止一秒,这里需要根据自身的业务情况进行设置

@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })

mark
依赖隔离配置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #超时时间
      circuitBreaker:
        enabled: true #启动断路器
        requestVolumeThreshold: 10 #请求数
        sleepWindowInMilliseconds: 10000 #休眠时间
        errorThresholdPercentage: 60 #错误百分比
    hello: #单个方法名
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #超时时间

Hystrix dashboard仪表盘监视
1、加入依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

2、添加注解

@EnableHystrixDashboard

3、暴露接口

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream
      base-path: /

监视界面:http://ip:port/hystrix
mark
其中service-demo为服务名,监控信息界面如下:
mark

如果界面一直加载中,要注意的是你要先请求然后才能看到结果

Zuul超时配置

网关刚开始启动时,第一次访问一般都会超时,之后访问不会出现错误,zuul中依赖了Hystrix,所以可以在zuul中添加超时配置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #超时时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值