第1步: 引入sentinel的依赖
<!--sentinel客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
第2步: 在配置文件中开启Feign对Sentinel的支持
feign:
sentinel:
enabled: true
第3步: 创建容错类
package com.bingo.service.fallback;
import com.bingo.domain.Product;
import com.bingo.service.ProductService;
import org.springframework.stereotype.Service;
//这是个容错类,需要实现Feign所在接口,并实现接口的所有方法
//一旦Feign远程调用出现问题,就会进入当前类中的同名方法,执行容错逻辑
@Service
public class ProductServiceFallback implements ProductService {
@Override
public Product findById(Integer pid) {
//容错逻辑
Product product = new Product();
product.setPid(999);
product.setPname("微服务异常,进入容错逻辑");
return product;
}
}
第4步: 为被容器的接口指定容错类(调用的地方)