Spring-Cloud-Gateway集成Sentinel限流

1)gateway添加sentinel相关依赖

xml 复制代码
<spring-cloud.version>2021.0.1</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>

<dependencies>

    <!--gateway-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

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

</dependencies>

2)gateway添加相关的配置

yml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: http://localhost:8081
          predicates:
            - Path=/user/*
    sentinel:
      transport:
        dashboard: localhost:8888

3)启动gateway

4)下载sentinel控制台并启动

下载地址:https://github.com/alibaba/Sentinel/releases

启动:java -Dserver.port=8888 -jar sentinel-dashboard-1.8.6.jar

5)sentinel控制台添加流控规则

先访问一下网关:http://localhost:8080/user/1

添加流控规则:

6)jmeter压测

10个并发请求,只有5个会成功:

7)API分组限流

新建API分组

添加流控规则

测试代码下载:
https://github.com/xjs1919/enumdemo/tree/master/gateway-sentinel-demo

相关推荐
青莲网络12 小时前
安全第一与合规治理:魔芋 AI 正式发布企业级大模型网关 Mai Gateway
人工智能·安全·gateway
爱吃羊的老虎13 小时前
【JAVA】Java微服务—网关Gateway
java·微服务·gateway
常常有14 小时前
Redis:哨兵模式 (Sentinel)
redis·python·sentinel
小悟空17 小时前
[AI 生成] Nginx 502 Bad Gateway 排查手册(Python 后端篇)
python·nginx·gateway
小马爱打代码17 小时前
SpringBoot + SpringCloud Gateway + Sentinel + Redis:API 网关层的接口限流、黑名单拦截与用户认证
spring boot·spring cloud·gateway
Waay2 天前
从 0 到 1 实操 K8s Gateway API+Istio:告别 Ingress,用新标准实现域名访问
kubernetes·gateway·istio
海市公约4 天前
Redis 哨兵模式底层原理与自动故障转移全流程
redis·sentinel·redis哨兵·高可用架构·主观下线·客观下线·leader选举
辞忧九千七5 天前
Redis 哨兵(Sentinel)模式部署教程(基于一主二从架构)
redis·架构·sentinel
AOwhisky5 天前
Ceph系列第五期:Ceph 对象存储(RADOS Gateway)精讲
linux·运维·笔记·ceph·gateway·对象存储
西凉的悲伤6 天前
Spring Cloud Gateway介绍
java·spring cloud·gateway