Appearance
简单理解:使用Open-Feign获取远程数据时,如果远程数据出现故障获取不到,我们可以实现这个Service接口,当没有远程数据时,会将兜底数据展现给用户
xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
1.在配置文件中开启OpenFeign对Sentinel的支持
yaml
feign:
sentinel:
enabled: true
2.创建容错类
容错类要求必须实现被容错的接口,并为每个方法实现容错方案
java
@Service
public class VideoServiceFallback implements VideoService {
@Override
public Video findById(int videoId) {
Video video = new Video();
video.setTitle("熔断降级数据");
return video;
}
@Override
public Video saveVideo(Video video) {
return null;
}
}
3.为被容器的接口指定容错类
java
@FeignClient(value = "xdclass-video-service", fallback = VideoServiceFallback.class)
public interface VideoService {
@GetMapping(value = "/api/v1/video/find_by_id")
Video findById(@RequestParam("videoId") int videoId);
……
}