Sentinel整合Open Feign

suaxi
2021-04-29 / 0 评论 / 269 阅读 / 正在检测是否收录...

1、环境搭建

以8884模块为例

pom文件添加依赖
<!-- openfign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>


yml添加
# 激活sentinel对feign的支持
feign:
  sentinel:
    enabled: true


启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderMain84 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain84.class, args);
    }
}


创建远程调用接口
@FeignClient(value = "nacos-payment-provider")
public interface PaymentService {

    @GetMapping("/payment/{id}")
    public CommonResult<Payment> payment(@PathVariable("id")Long id);
}
实现类(用于服务降级)
@Component
public class PaymentFallbackService implements PaymentService {
    @Override
    public CommonResult<Payment> payment(Long id) {
        return new CommonResult<Payment>(444, "服务降级返回--PaymentService", new Payment(id, "errorSerial"));
    }
}
接口指定降级的类
@FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class)
public interface PaymentService {

    @GetMapping("/payment/{id}")
    public CommonResult<Payment> payment(@PathVariable("id")Long id);
}


controller
@Resource
private PaymentService service;

@GetMapping("/payment/{id}")
public CommonResult<Payment> payment(@PathVariable("id")Long id){
    return service.payment(id);
}


2、测试

启动9003、8884,如果中途9003出现问题关闭,8884服务降级成功

1.Open Feign测试结果.png


3、熔断框架比较

SentinelHystrixresilience4j
隔离策略信号量隔离线程池/信号量隔离信号量隔离
熔断降级策略基于响应时间、异常比例、异常数异常比例异常比例、响应时间
实时统计滑动窗口(LeapArray)滑动窗口(Rxjava)Ring Bit Buffer
动态规则支持多种数据源支持多种数据源有限的支持
扩展性多个扩展点插件接口
基于注解的支持支持支持支持
限流QPS、调用关系有限的支持Rate Limiter

图片来源:尚硅谷 - 周阳 - Spring Cloud Alibaba

0

评论 (0)

取消