1.pom.xml加入sentinel依赖
<!-- sentinel 服务限流 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
2.yml配置文件汇总开启sentinel
feign:
sentinel:
enabled: true #开启熔断
3.Feign接口熔断降级
3.1.在UserClient接口@FeignClient中加上fallback属性值,用于熔断处理
FeignClient(value = "user-server", fallback = UserClientFallBack.class)
public interface UserClient {
//调用目标服务的controller的方法,和UseController中的方法路径保持一致
@GetMapping("/user/{id}")
User getById(@PathVariable Long id);
}
3.2.写一个降级处理类
@Component
public class UserClientFallBack implements UserClient {
@Override
public User getById(Long id) {
return new User(-1L,"无此用户","无此用户");
}
}