一、先启动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服务

  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>
  1. 开启sentinel支持
    在工程的application.yml中添加sentinel 对 feign 的支持激活sentinel的支持
feign:sentinel:enabled: true
  1. 调用端配置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;}
}
  1. 被调用端的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实现网关限流相关推荐

  1. Sentinel 之 网关限流

    网关限流 该文档来之Sentinel官方文档 Sentinel 支持对 Spring Cloud Gateway.Zuul 等主流的 API Gateway 进行限流. Sentinel 1.6.0 ...

  2. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战

    文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流实现,过滤器是RequestRateLimiterGatewayFilterFactory,不过这种上不了台面的就 ...

  3. Spirng Cloud 中gateway 网关限流和熔断

    分流:原先数据库只放一个服务器,无论多少个都只能访问这个服务器,访问不了就排队(延迟)(如果同一时间也高并发了那就限流) 限流:同一时间限制访问的人数 限流的算法 漏桶算法:把请求放到一个容器中,控制 ...

  4. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!

    前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...

  5. 【云原生微服务>SCG网关篇十二】Spring Cloud Gateway集成Sentinel API实现多种限流方式

    文章目录 一.前言 二.Gateway集成Sentinel API 0.集成Sentinel的核心概念 1)GatewayFlowRule 和 ApiDefinition 2)GatewayFlowR ...

  6. Gateway Sentinel 做网关降级/流控,转发header和cookie

    大家好,我是烤鸭: Springcloud Gateway 使用 Sentinel 流量控制. 环境 springcloud-gateway的网关应用,springboot的服务,nacos作为注册中 ...

  7. Spring Cloud :Gateway 网关限流(五)

    目录 一.概述 1. 为什么需要限流 二.限流算法 1. 计数器算法 2. 漏桶算法 3. 令牌桶算法 四.Gateway 限流 1. 添加依赖 2. 配置文件 3. 限流规则配置类 Spring C ...

  8. Sentinel(七)之网关限流

    转载自  网关限流 Sentinel 支持对 Spring Cloud Gateway.Zuul 等主流的 API Gateway 进行限流. Sentinel 1.6.0 引入了 Sentinel ...

  9. 微服务网关鉴权:gateway使用、网关限流使用、用户密码加密、JWT鉴权

    点击上方"芋道源码",选择"设为星标" 管她前浪,还是后浪? 能浪的浪,才是好浪! 每天 10:33 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | ...

最新文章

  1. iOS - Tools Using's Problem
  2. [DiscuzNt]整合DiscuzNt论坛目前所发现的小BUG及个人简单解决办法
  3. 网站如何从http升级成https
  4. flask 中文编码解码
  5. 你在 或者不在 需要逻辑回归来算 | 协和八
  6. 向“3+1” SQLServer2008集群增加磁盘
  7. python运维脚本部署jdk_Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
  8. Oryx 2.1.2版本: src源码编译
  9. redis面试常问--缓存穿透
  10. python进阶-argparse
  11. 共同富裕指数集:31省份共同富裕-富裕度、共同度两大维度数据(2000-2021年)
  12. ABAP RFC函数导入参数 EXPORTING 传入内表形式参数
  13. Linux命令怎么查找IP,在Linux中,如何查找您的IP地址
  14. 计算机图形学原理与实践 答案,知到高级计算机图形学原理与实践单元测试答案...
  15. 一键领取php,新款引流必备一键领取名片赞PHP单页源码
  16. 使用ATMEGA8A 自己制作ARDUINO全过程。
  17. Python pyspider的安装
  18. Java类包的定义(package)
  19. (U8)系统管理登录时提示“缺少根元素”
  20. 400分理科学计算机,400分左右的理科大学 高考400分能上什么学校

热门文章

  1. 通过excel模板文件根据数据库数据修改其中的单元格数据
  2. js中判断-0 ,js无穷数Infinity
  3. MVVMLight 实现指定Frame控件的导航
  4. 面向对象的继承关系体现在数据结构上时,如何表示
  5. Android:阻止输入法将图片压缩变形
  6. Golang 入门笔记(一)
  7. Django入门:(admin.E108
  8. 机器学习导论(张志华):渐近性质
  9. 4种必须知道的Android屏幕自适应解决方案
  10. OpenMP的环境变量