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

相关推荐
小南家的青蛙3 天前
Sentinel实战指南
sentinel
如夏倾城3 天前
BGP(Border Gateway Protocol 边界网关协议)
运维·服务器·网络·gateway·信息与通信
时间的拾荒人7 天前
Redis 哨兵(Sentinel)详解:原理、选举机制与实战部署
redis·面试·架构·sentinel
JJJennie7777 天前
MAI Gateway 免费试用与付费方案详解:先验效果,再谈 ROI
人工智能·大模型·gateway·ai网关
顧棟11 天前
INFINI Gateway 服务器端口重用(SO_REUSEPORT)实现与分析
网络·gateway
博风12 天前
SCA:二、Sentinel熔断、降级、限流
sentinel
逃逸线LOF12 天前
服务网关Gateway
java·运维·gateway
露天赏雪14 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
JJJennie77714 天前
MAI Gateway私有化部署全攻略:大模型网关接入DeepSeek?附架构图
大模型·gateway·ai网关·魔芋ai
JJJennie77718 天前
MAI Gateway私有化部署全攻略:大模型网关如何接入?附架构图
算法·gateway·ai网关·魔芋ai