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

相关推荐
Song_RsHt5 小时前
Gateway (上):路由与转发,微服务的统一入口
java·笔记·后端·学习·gateway
4SAPI1 天前
官方API、API聚合平台与自建LLM Gateway:三种方案怎么选
大数据·人工智能·gateway·php
Raas1002 天前
AI网关怎么选?MAI Gateway(魔芋企业级AI网关)入选2026年企业级AI网关推荐清单
大数据·网络·人工智能·gateway·mai gateway
Raas1002 天前
AI网关能做调用审计吗?MAI Gateway(魔芋企业级AI网关)实战能力深度解读
java·服务器·网络·数据库·gateway·prompt·企业级产品
像风一样自由20203 天前
53.NginxAPI-Gateway与鉴权如何保护大模型接口
人工智能·大模型·gateway·智能体
Raas1003 天前
MAI Gateway(魔芋企业级AI网关)能力解析:AI网关能做调用审计吗?AI网关核心功能详解
大数据·网络·人工智能·gateway·ai网关·mai gateway
流烟默3 天前
Spring Cloud 微服务 Sentinel 热点参数与授权规则集成指南(附架构建议)
spring cloud·微服务·架构·sentinel
Raas1004 天前
MAI Gateway(魔芋企业级AI网关)功能全解:AI网关能做Prompt注入防护吗?一文看懂AI网关能力矩阵(v1.2.0)
网络·人工智能·gateway·prompt·mai gateway·企业级ai网关·企业级产品
Raas1004 天前
AI网关能做Prompt注入防护吗?MAI Gateway(魔芋企业级AI网关)实战能力深度解读
gateway·prompt
Thomas.Sir4 天前
第23课:Sentinel熔断降级、系统自适应保护、集群限流
spring cloud·sentinel