springcloud整合hystrix
首先添加服务熔断的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
然后在配置文件中添加配置
##开启熔断机制
feign.hystrix.enabled=true
## 设置超时时间,默认为1000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
第三步,在创建interface之后,还要创建对应的实现类,实现出错后输出的内容
import com.hzh.commonutils.R; import org.springframework.stereotype.Component; import java.util.List; /** * @Description: 熔断实现类 * @Author: 何志恒 **/ @Component public class VodFileDegradeFeignClient implements VodClient { // 出错之后会执行 @Override public R removeAlyVideo(String id) { return R.error().message("删除视频失败"); } @Override public R deleteBatch(List videoIdList) { return R.error().message("删除多个视频失败"); } }
最后在interface中添加注解
@FeignClient(name = "service-vod", fallback = VodFileDegradeFeignClient.class)
至此实现hystrix熔断机制
浙公网安备 33010602011771号