一导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!--hystrix依赖,主要是用 @HystrixCommand --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--服务注册--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--服务调用--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
二在配置文件中添加hystrix配置
#开启熔断机制 feign.hystrix.enabled=true # 设置hystrix超时时间,默认1000ms hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
三在service-edu的client包里面创建熔断器的实现类
package com.atguigu.eduservice.client; import com.atguigu.commonutils.R; import org.springframework.stereotype.Component; @Component public class VodFileDegradeFeignClient implements VodeClient { //删除一个视频,出错之后会执行 @Override public R removeAlyVideo(String id) { return R.error().message("视频出错了" ); } }
四修改VodClient接口的注解
package com.atguigu.eduservice.client; import com.atguigu.commonutils.R; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(name = "service-vod",fallback = VodFileDegradeFeignClient.class) public interface VodeClient { @DeleteMapping("/eduvod/video/removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable("id") String id); }