其实openfeign整合sentinel真的很简单,openfeign环境搭建参考三、springCloudAlibaba-feign的简单使用 然后整合过程很简单。
注:不一定要整合到sentinel控制台
1、服务消费者加入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2、feign开启对sentinel的支持
feign:
sentinel:
enabled: true;
3、新建一个异常类,实现feign的对应服务接口
package com.suibibk.springCloud.order.openfeign;
import org.springframework.stereotype.Component;
@Component
public class StockFeignServiceFallback implements StockService{
@Override
public String reduce() {
return "降级了";
}
}
4、加上fallback
package com.suibibk.springCloud.order.openfeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name="stock-service",path="/stock" ,fallback=StockFeignServiceFallback.class)
public interface StockService {
@RequestMapping("/reduce")
public String reduce();
}
ok!