gateway集成sentinel实现网关限流
一、先启动sentinel
参考文章
二、编写gateway服务
- yaml文件
server:port: 2004
spring:cloud:gateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: sentinel-user-feign #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: lb://sentinel-user-feign #根据服务名称匹配后提供服务的路由地址,使用负载均衡的方式predicates:- Path=/user/** # 断言,路径相匹配的进行路由
三、编写sentinel-user-feign服务
- 引入依赖
<!--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>
- 开启sentinel支持
在工程的application.yml中添加sentinel 对 feign 的支持激活sentinel的支持
feign:sentinel:enabled: true
- 调用端配置FeignClient
需要配置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);
}
创建一个实现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;}
}
- 被调用端的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;}
}
四、编写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", "当前商品服务调用成功,查询商品id为:" + id + ",当前处理服务的端口号为: " + port);return map;}
}
五、加入sentinel的限流规则
访问http://localhost:2004/user/getProductInfo?id=7
经由gateway路由后,如果阈值达到限定值,自动限流
gateway集成sentinel实现网关限流相关推荐
- Sentinel 之 网关限流
网关限流 该文档来之Sentinel官方文档 Sentinel 支持对 Spring Cloud Gateway.Zuul 等主流的 API Gateway 进行限流. Sentinel 1.6.0 ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战
文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流实现,过滤器是RequestRateLimiterGatewayFilterFactory,不过这种上不了台面的就 ...
- Spirng Cloud 中gateway 网关限流和熔断
分流:原先数据库只放一个服务器,无论多少个都只能访问这个服务器,访问不了就排队(延迟)(如果同一时间也高并发了那就限流) 限流:同一时间限制访问的人数 限流的算法 漏桶算法:把请求放到一个容器中,控制 ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...
- 【云原生微服务>SCG网关篇十二】Spring Cloud Gateway集成Sentinel API实现多种限流方式
文章目录 一.前言 二.Gateway集成Sentinel API 0.集成Sentinel的核心概念 1)GatewayFlowRule 和 ApiDefinition 2)GatewayFlowR ...
- Gateway Sentinel 做网关降级/流控,转发header和cookie
大家好,我是烤鸭: Springcloud Gateway 使用 Sentinel 流量控制. 环境 springcloud-gateway的网关应用,springboot的服务,nacos作为注册中 ...
- Spring Cloud :Gateway 网关限流(五)
目录 一.概述 1. 为什么需要限流 二.限流算法 1. 计数器算法 2. 漏桶算法 3. 令牌桶算法 四.Gateway 限流 1. 添加依赖 2. 配置文件 3. 限流规则配置类 Spring C ...
- Sentinel(七)之网关限流
转载自 网关限流 Sentinel 支持对 Spring Cloud Gateway.Zuul 等主流的 API Gateway 进行限流. Sentinel 1.6.0 引入了 Sentinel ...
- 微服务网关鉴权:gateway使用、网关限流使用、用户密码加密、JWT鉴权
点击上方"芋道源码",选择"设为星标" 管她前浪,还是后浪? 能浪的浪,才是好浪! 每天 10:33 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | ...
最新文章
- iOS - Tools Using's Problem
- [DiscuzNt]整合DiscuzNt论坛目前所发现的小BUG及个人简单解决办法
- 网站如何从http升级成https
- flask 中文编码解码
- 你在 或者不在 需要逻辑回归来算 | 协和八
- 向“3+1” SQLServer2008集群增加磁盘
- python运维脚本部署jdk_Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
- Oryx 2.1.2版本: src源码编译
- redis面试常问--缓存穿透
- python进阶-argparse
- 共同富裕指数集:31省份共同富裕-富裕度、共同度两大维度数据(2000-2021年)
- ABAP RFC函数导入参数 EXPORTING 传入内表形式参数
- Linux命令怎么查找IP,在Linux中,如何查找您的IP地址
- 计算机图形学原理与实践 答案,知到高级计算机图形学原理与实践单元测试答案...
- 一键领取php,新款引流必备一键领取名片赞PHP单页源码
- 使用ATMEGA8A 自己制作ARDUINO全过程。
- Python pyspider的安装
- Java类包的定义(package)
- (U8)系统管理登录时提示“缺少根元素”
- 400分理科学计算机,400分左右的理科大学 高考400分能上什么学校