likes
comments
collection
share

Springcloud组件-hystrix

作者站长头像
站长
· 阅读数 17

1、问题描述

通过service-edu 调用service-vod,在调用时service-vod服务宕机,调用不到服务,出现了超时提示。

connect timed out executing DELETE http://service-vod/eduvod/video/241beb010b8e470b9650c468e700341c

2、如何使用熔断器

(1)在service模块中添加依赖 Springcloud组件-hystrix

(2)在service-edu添加配置

#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

(3)编写vodClient实现类在client目录下

@Component
public class VodFileDegradeFeignClient implements VodClient {
    @Override
    public R deleteVideoAliyun(String videoId) {
        return R.error();
    }
    @Override
    public R deleteMoreVideo(List<String> videoIdList) {
        return R.error();
    }
}

(4)在VodClient里接口注解添加属性

@FeignClient(value = "service-vod",fallback = VodFileDegradeFeignClient.class)//服务名就是配置文件里配置的
@Component
public interface VodClient {
……
}

3、测试

通过service-edu 调用service-vod,在调用时service-vod服务宕机,没有抛异常,继续执行删除流程。