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

相关推荐
代码写到35岁11 小时前
Gateway+OpenFeign 踩坑总结
gateway
invicinble11 小时前
对于gateway信息量沉淀
gateway
空中海1 天前
第六篇:可靠性篇 — Sentinel 熔断限流与 Seata 分布式事务
分布式·sentinel
郝开2 天前
Spring Cloud Gateway 3.5.14 使用手册
java·数据库·spring boot·gateway
JAVA面经实录9172 天前
如何选择适合项目的「限流 / 熔断 / 降级」方案
java·spring·kafka·sentinel·guava
Ribou3 天前
Kubernetes v1.35.2 基于 Cilium Gateway API 的服务访问架构
架构·kubernetes·gateway
蓝眸少年CY3 天前
(第十五篇)spring cloud之Sentinel实现熔断与限流
数据库·spring cloud·sentinel
huipeng9263 天前
GateWay使用详解
java·spring boot·spring cloud·微服务·gateway
无风听海5 天前
Python 哨兵值模式(Sentinel Value Pattern)深度解析
开发语言·python·sentinel
空中海7 天前
Redis 原理深度解析:持久化 × 主从复制 × Sentinel × Cluster × 性能排查全攻略
数据库·redis·sentinel