springCloud-41 Feign实现熔断

Sentinel 适配了 Feign 组件。如果想使用,除了引入 sentinel - starter 的依赖外还需要 2 个步骤:
  • 配置文件打开 sentinel feign 的支持: feign.sentinel.enabled=true
  • 加入 openfeign starter 依赖使 sentinel starter 中的自动化配置类生效:

一,引入依赖

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

二,开启sentinel 支持

在工程的 application.yml 中添加 sentinel feign 的支持
feign: 
    sentinel: 
        enabled: true

三,配置FeignClient

和使用 Hystrix 的方式基本一致,需要配置 FeignClient 接口以及通过 fallback 指定熔断降级方法
//指定需要调用的微服务名称 
@FeignClient(name="shop-service-product",fallback = ProductFeginClientCallBack.class) public interface ProductFeginClient { 
    //调用的请求路径 
    @RequestMapping(value = "/product/{id}",method = RequestMethod.GET) 
    public Product findById(@PathVariable("id") Long id); 

}

四,配置熔断方法

/*** 
    实现自定义的ProductFeginClient接口 
*   在接口实现类中编写熔断降级方法 
*/ 
@Component 
public class ProductFeginClientCallBack implements ProductFeginClient { 
    /*** 降级方法 */ 
    public Product findById(Long id) { 
        Product product = new Product(); 
        product.setId(-1l); 
        product.setProductName("熔断:触发降级方法"); 
        return product; 
    } 
}
Feign 对应的接口中的资源名策略定义: httpmethod:protocol://requesturl @FeignClient 注解中
的所有属性, Sentinel 都做了兼容。
ProductFeginClient 接口中方法 fifindById 对应的资源名为 GET: http://shop-service
product/product/ {str}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

vegetari

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

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

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

打赏作者

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

抵扣说明:

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

余额充值