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

相关推荐
全靠bug跑2 天前
Sentinel 服务保护实战:限流、隔离与熔断降级详解
java·sentinel
黄俊懿2 天前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
Hello.Reader2 天前
Flink SQL 的 JOB 管理语句SHOW / DESCRIBE / STOP(SQL CLI & SQL Gateway 实战)
sql·flink·gateway
kkoral4 天前
单机docker部署的redis sentinel,使用python调用redis,报错
redis·python·docker·sentinel
骚戴4 天前
n1n:从替代LiteLLM Proxy自建网关到企业级统一架构的进阶之路
人工智能·python·大模型·llm·gateway·api
墨白曦煜4 天前
深入剖析 Redis 客户端:Sentinel 模式下的“寻址”与“感知”艺术
数据库·redis·sentinel
遇见火星4 天前
Redis高可用-哨兵模式(Sentinel)
redis·sentinel
骚戴4 天前
LLM API Gateway:LLM API 架构、AI 聚合与成本优化全解(2025深度指南)
人工智能·python·大模型·llm·gateway·api
YDS8295 天前
SpringCloud —— Sentinel详解
java·spring cloud·sentinel
后端小张5 天前
【JAVA 进阶】深入理解Sentinel:分布式系统的流量守卫者
java·开发语言·spring boot·后端·spring·spring cloud·sentinel