Hystrix替代品之 Alibaba Sentinel 简介

Hystrix停更,那么Spring Cloud生态中是否有替代产品呢?

Alibaba Sentinel

Sentinel 是阿里巴巴开源的一款断路器实现,目前在Spring Cloud的孵化器项目Spring Cloud Alibaba中,预计Spring Cloud H系列中可以孵化完成。

尽管Sentinel尚未在Spring Cloud项目中孵化完成,但Sentinel本身在阿里内部已经被大规模采用,非常稳定。因此可以作为一个较好的替代品。

Resilience4J

Resilicence4J 在今年的7月进入笔者视野,小玩了一下,觉得非常轻量、简单,并且文档非常清晰、丰富。个人比较看好,这也是Hystrix官方推荐的替代产品

不仅如此,Resilicence4j还原生支持Spring Boot 1.x/2.x,而且监控也不像Hystrix一样弄Dashboard/Hystrix等一堆轮子,而是支持和Micrometer(Pivotal开源的监控门面,Spring Boot 2.x中的Actuator就是基于Micrometer的)、prometheus(开源监控系统,来自谷歌的论文)、以及Dropwizard metrics(Spring Boot曾经的模仿对象,类似于Spring Boot)进行整合。

笔者特别看重Resilience4J和micrometer整合的能力,这意味着:如果你用Spring Boot 2.x并在项目中引入Resilience4J,那么监控数据和Actuator天生就是打通的!你不再需要一个专属的、类似于Hystrix Dashboard的东西去监控断路器。


 

Hystrix宣布停止维护 后,社区推荐了Resilience4j ,而业界还有Alibaba Sentinel可供选择——3款产品各有优势,具体的功能差异参考下表,该表来自 Sentinel Wiki :

 SentinelHystrixresilience4j
隔离策略信号量隔离(并发线程数限流)线程池隔离/信号量隔离信号量隔离
熔断降级策略基于响应时间、异常比率、异常数基于异常比率基于异常比率、响应时间
实时统计实现滑动窗口(LeapArray)滑动窗口(基于 RxJava)Ring Bit Buffer
动态规则配置支持多种数据源支持多种数据源有限支持
扩展性多个扩展点插件的形式接口的形式
基于注解的支持支持支持支持
限流基于 QPS,支持基于调用关系的限流有限的支持Rate Limiter
流量整形支持预热模式、匀速器模式、预热排队模式不支持简单的 Rate Limiter 模式
系统自适应保护支持不支持不支持
控制台提供开箱即用的控制台,可配置规则、查看秒级监控、机器发现等简单的监控查看不提供控制台,可对接其它监控系统

 

目前,Sentinel 在 Spring Cloud Alibaba中已适配Spring Cloud体系,完全可用来替代 Hystrix 的功能。不仅如此,阿里内部很多产品线都已使用Sentinel实现限流降级,Sentinel是经过生产流量大规模验证的。

下面来探讨如何从Hystrix迁移至Sentinel——

Spring Cloud Alibaba Sentinel 代替 Hystrix

要想使用Spring Cloud Alibaba Sentinel,需添加如下依赖,并去除Spring Cloud Netflix Hystrix( spring-cloud-starter-netflix-hystrix )的依赖。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel</artifactId>
    <version>0.2.1.RELEASE</version>
</dependency>

0代码修改兼容Feign

加上Feign的依赖:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>${latest.version}</version>
</dependency>

application.yml 中添加feign.sentinel.enabled=true 即可为Feign启用Sentinel支持:

# 去掉
# feign.hystrix.enabled: true
# 改为如下即可
feign.sentinel.enabled: true

Feign Client无需修改:

@FeignClient(name = "service-provider")
public interface EchoService {
    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    String echo(@PathVariable("str") String str);
  
  	@RequestMapping(value = "/echo/save", method = RequestMethod.POST)
    String save(Foo foo);
}

对于这个 EchoService,echo方法对应的资源名是 GET:http://service-provider/echo/{str}, save 方法对应的资源名是 POST:http://service-provider/echo/save

只需配置这些规则,限流降级操作即可生效。

一行代码支持RestTemplate

Sentinel与Spring生态的 RestTemplate 也进行了整合,可对 RestTemplate 请求过程进行限流和降级,只需在构造 RestTemplate 的时候加上 @SentinelRestTemplate 注解即可,如下所示:

@Bean
@SentinelRestTemplate
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@SentinelRestTemplate 注解还暴露出了对应的属性可进行限流降级后的自定义错误,默认的行为是返回 “RestTemplate request block by sentinel” 信息。关于 @SentinelRestTemplate 的详细信息可以参考 Wiki

 


迁移系列:

https://my.oschina.net/eacdy/blog/2962937

https://yq.aliyun.com/articles/691128

https://yq.aliyun.com/articles/691135

https://yq.aliyun.com/articles/691501

https://yq.aliyun.com/articles/691504

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值