Sentinel结合Fejgn接口,进行调用远程接口的调用和限流
生活随笔
收集整理的這篇文章主要介紹了
Sentinel结合Fejgn接口,进行调用远程接口的调用和限流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
sentinel 適配了 Feign 組件。如果想使用,除了引入 sentinel-starter 的依賴外還需要 2 個步驟:
1、引入依賴
<!--feign對sentinel的支持--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>2、開啟sentinel支持
在工程的application.yml中添加sentinel 對 feign 的支持
激活sentinel的支持
feign:sentinel:enabled: true3、調用端配置FeignClient
需要配置FeignClient接口以及通過 fallback 指定熔斷降級方法
創建一個實現ProductFeign接口的兜底類
@Component public class ProductFeignHandler implements ProductFeign {@Overridepublic Map<String, Object> find(String id) {Map map=new HashMap(16);map.put("507","遠程調用服務接口被限流");return map;}// Fallback 函數,函數簽名與原函數一致或加一個 Throwable 類型的參數.@Overridepublic Map<String, Object> pdg(String id) {Map map=new HashMap(16);map.put("507","遠程調用服務接口被降級處理");return map;} }調用端Controller
@RestController @Slf4j public class UserController {@Autowiredprivate ProductFeign productFeign;@GetMapping("/user/getProductInfo")public Map<String, Object> getProductInfo(String id) {Map<String, Object> map = productFeign.find(id);log.info("返回的信息:[{}]" + map);return map;}@GetMapping("/user/getProductInfodg")public Map<String, Object> getProductInfodg(String id) {Map<String, Object> map = productFeign.pdg(id);log.info("返回的信息:[{}]" + map);return map;} }4、被調用端的Controller
@RestController @Slf4j public class ProductController {@Value("${server.port}")private int port;@GetMapping("/product/find")public Map<String, Object> find(@RequestParam("id") String id) {Map<String, Object> map = new HashMap<>();log.info("進入商品服務,當前接收的商品id為:[{}]", id);map.put("status", true);map.put("msg", "當前商品服務調用成功,查詢商品id為:" + id + ",當前處理服務的端口號為: " + port);return map;}@GetMapping("/product/finddg")public Map<String,Object> pdg(@RequestParam("id") String id){Map<String, Object> map = new HashMap<>();log.info("進入降級接口商品服務,當前接收的商品id為:[{}]", id);map.put("status", true);map.put("msg", "當前通過降級接口商品服務調用成功,查詢商品id為:" + id + ",當前處理服務的端口號為: " + port);return map;} }5、sentinel控制臺進行設置
- 降級規則
- 限流規則
平常手速調用:http://localhost:9099/user/getProductInfo?id=7
祖傳手速:
訪問:http://localhost:9099/user/getProductInfodg?id=7
平時手速:
祖傳手速:
總結
以上是生活随笔為你收集整理的Sentinel结合Fejgn接口,进行调用远程接口的调用和限流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021快手母婴行业数据价值报告
- 下一篇: 产品经理面试全流程深度复盘【面试准备篇】