Hystrix源码分析-依赖关系

核心依赖

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.18</version>
</dependency>

这个依赖是整个Hystrix最底层的,实际上有了hystrix-core就能完成hystrix的功能,但是这个只能通过采用编程式的方式使用Hystrix,也就是自己构建继承HystrixCommand的对象然后从写run()和getFallback()方法,完成自定义run()方法中业务逻辑和getFallback()熔断异常兜底!这种方式是最底层,最核心的依赖包,用起来还是比较复杂的,如果想让一个方法支持fallback还得去继承HystrixCommand,跟业务完全耦合到一起,对业务的侵入性太大了,显然不利于hystrix的使用,那么基于注解的方式,采用声明式就接着诞生了!

声明式增强

<dependency>
   <groupId>com.netflix.hystrix</groupId>
   <artifactId>hystrix-javanica</artifactId>
   <version>1.5.18</version>
</dependency>

在Hystrix核心依赖对代码侵入性较强,使用复杂的情况下,Hystrix提供了面向切面,基于注解,提供声明式编程方案hystrix-javanica就诞生了,使我们减少业务代码侵入,提高开发效率,hystrix-javanica项目使Hystrix更容易使用,我们只需要简单使用@HystrixCommand(fallbackMethod = “fallbackMethod”)标记业务层方法,然后主启动类标注@EnableHystrix即可完成hystrix熔断
依赖关系
hystrix-javanica依赖hystrix-core

获取监控指标

<dependency>
  <groupId>com.netflix.hystrix</groupId>
  <artifactId>hystrix-metrics-event-stream</artifactId>
  <version>1.5.18</version>
</dependency>

我们知道Hystrix是有调用监控指标的,是在通过hystrix-core依赖维护在容器中的,但是并没有提供怎么获取这些监控指标的地方,那么hystrix-metrics-event-stream就是专门用来获取这些监控指标的业务逻辑!
依赖关系
hystrix-metrics-event-stream依赖(hystrix-serialization、hystrix-core)

Hystrix序列化

<dependency>
   <groupId>com.netflix.hystrix</groupId>
   <artifactId>hystrix-serialization</artifactId>
   <version>1.5.18</version>
</dependency>

hystrix-serialization这个就是用于hystrix-metrics-event-stream做序列化的

Hystrix仪表盘

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

前面有提到Hystrix提供监控指标的,而且有提供获取这些指标的hystrix-metrics-event-stream以及序列化hystrix-serialization,那么展示这些监控指标数据仪表盘的就是spring-cloud-starter-netflix-hystrix-dashboard了
依赖关系
spring-cloud-starter-netflix-hystrix-dashboard依赖hystrix-metrics-event-stream、hystrix-serialization、hystrix-core

微服务starter

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

这是微服务整合的starter,spring-cloud-starter-netflix-hystrix内部依赖hystrix-metrics-event-stream、hystrix-serialization、hystrix-core、hystrix-javanica,也就是导入了spring-cloud-starter-netflix-hystrix这个依赖后,那么我们就可以使用声明式、编程式使用Hystrix,同时也提供了获取服务监控指标的逻辑(hystrix-metrics-event-stream),但是这个微服务starter并没有将监控仪表盘整合进去,如果需要查看监控仪表盘,那么还需要而外导入spring-cloud-starter-netflix-hystrix-dashboard

Feign整合Hystrix

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

这个依赖就是Feign整合Hystrix使用的starter,这个也是目前比较常见的用法,因为无论是单独使用hystrix-core、hystrix-javanica,声明式、编程式,或者使用pring-cloud-starter-netflix-hystrix都需要我们自己或多或少编写一些和Feign整合的代码,Hystrix部分侵入性太强,那么使用spring-cloud-starter-openfeign就可以帮我们隐藏掉Hystrix部分代码!降低业务代码侵入,让我们把精力放在业务层,无需关注Hystrix和Feign怎么整合的!
注意:spring-cloud-starter-openfeign中只包含Hystrix的hystrix-core依赖,那么单独导入spring-cloud-starter-openfeign依赖是不支持声明式Hystrix的,也不支持仪表盘功能,如果需要那么需要而外导入对应依赖!
依赖关系
spring-cloud-starter-openfeign依赖hystrix-core

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员劝退师-TAO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值