gateway集成sentinel实现网关限流
生活随笔
收集整理的這篇文章主要介紹了
gateway集成sentinel实现网关限流
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、先啟動sentinel
參考文章
二、編寫gateway服務
- yaml文件
三、編寫sentinel-user-feign服務
在工程的application.yml中添加sentinel 對 feign 的支持激活sentinel的支持
需要配置FeignClient接口以及通過 fallback 指定熔斷降級方法
@FeignClient(value = "client",fallback = ProductFeignHandler.class) public interface ProductFeign {@GetMapping("/product/find")Map<String,Object> find(@RequestParam("id") String id);@GetMapping("/product/finddg")Map<String,Object> pdg(@RequestParam("id") String id); }創(chuàng)建一個實現(xiàn)ProductFeign接口的兜底類
@Component public class ProductFeignHandler implements ProductFeign {@Overridepublic Map<String, Object> find(String id) {Map map=new HashMap(16);map.put("507","遠程調(diào)用服務接口被限流");return map;}// Fallback 函數(shù),函數(shù)簽名與原函數(shù)一致或加一個 Throwable 類型的參數(shù).@Overridepublic Map<String, Object> pdg(String id) {Map map=new HashMap(16);map.put("507","遠程調(diào)用服務接口被降級處理");return map;} }調(diào)用端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;} }四、編寫sentinel-product-feign服務
@RestController @Slf4j public class ProductController {@Value("${server.port}")private int port;@GetMapping("/product/find")public Map<String, Object> find(String id) {Map<String, Object> map = new HashMap<>();log.info("進入商品服務,當前接收的商品id為:[{}]", id);map.put("status", true);map.put("msg", "當前商品服務調(diào)用成功,查詢商品id為:" + id + ",當前處理服務的端口號為: " + port);return map;} }五、加入sentinel的限流規(guī)則
訪問http://localhost:2004/user/getProductInfo?id=7
經(jīng)由gateway路由后,如果閾值達到限定值,自動限流
總結
以上是生活随笔為你收集整理的gateway集成sentinel实现网关限流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020年产品经理生存报告
- 下一篇: 能干的产品经理比不上能说的产品经理