SpringCloudAlibaba网关Gateway集成Sentinel

上一篇:SpringCloudAlibaba集成Gateway动态路由Nacos服务

之前我们搭建了网关项目gateway-web,进行api管理。
我们之前使用Sentinel对web项目的接口和Dubbo 的RPC接口分别进行了流控,那么Sentinel能对网关项目进行流控吗,答案是肯定的。
Sentinel的GitHub中对适配Gateway做了介绍和demo,传送门:https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#api-gateway-%E9%80%82%E9%85%8D

但是这个文档已经比较老, 可以看到wiki是2年前编写的,由于我们现在用的SpringCloud和SpringBoot依赖是最新的,所以官方文档中的使用方式并不太适用。

下面来介绍一下,最新的使用方式。

pom.xml中引入依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

application.yml文件中添加配置

spring:
  application:
    name: gateway-web
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8888
        port: 8888

启动gateway-web项目,访问gateway-web接口:


image.png

可以在Sentinel管理后台发现gateway-web服务


image.png

对admin-1添加流控规则,设置qps为1:


image.png

疯狂访问gateway-web接口,出现错误信息:


image.png

打完收工。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容