解决Spring Cloud 2020后openfeign不能集成Hystrix

2020 年 12 月 22 日Spring官方博客宣布,Spring Cloud 2020.0.0正式发布。从 Spring Cloud 2020.0.0 开始,Spring Cloud 废除了这种英国伦敦地铁站的命名方式,而使用了全新的 "日历化" 版本命名方式。

Spring Cloud官方尤其着重指出ribbon、hystrix 和 zuul从Spring Cloud 2020.0正式版发布后将不再被Spring Cloud支持。在目前最新的Spring Cloud 2020.0中仅仅剩下了Eureka。

so,Hystrix没了。很多教程做降级、熔断都是用的Hystrix,而这些教程无一例外都是使用的2020.0.0之前的版本。所以,如果你使用的是2020.0.0后的版本去跟着网上学习,你会发现,到了openfeign这一步,怎么样都无法在接口服务关闭后,能返回默认的异常处理方法。

这个时候通常会报错:java.net.UnknownHostException: 你所调用的服务名

不过没关系,通往罗马的路不止一条。作为当代优质程序员,得与时俱进,Sentinel就很棒呀。

我们可以在父工程添加,SpringCloudAlibaba依赖

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

然后在feign工程加入sentinel依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

在feign工程的配置文件加入开启Sentinel支持属性

feign:
  sentinel:
    enabled: true # Enable the Sentinel support for feign in the properties file (开启Sentinel支持属性)

其它操作就和Hystrix一模一样了。 

猜你喜欢

转载自blog.csdn.net/m0_57545353/article/details/125423812