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

相关推荐
dgvri6 小时前
Gateway Timeout504 网关超时的完美解决方法
gateway
回到原点的码农10 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
小川zs1 天前
OpenClaw Gateway 频繁断开/重启问题诊断
linux·服务器·gateway
没有bug.的程序员1 天前
黑客僵尸网络的降维打击:Spring Cloud Gateway 自定义限流剿杀 Sentinel 内存黑洞
java·网络·spring·gateway·sentinel
zhangshuang-peta1 天前
弥合 n8n 中的 AI 上下文鸿沟:为何采用 MCP Gateway 构建更智能的工作流
网络·人工智能·gateway·ai agent·mcp·peta
shamalee3 天前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
xiaolingting3 天前
Gateway 网关流控与限流架构指南
spring cloud·架构·gateway·sentinel
无级程序员3 天前
k8s v1.35配置gateway, istio通过metalb vip访问
kubernetes·gateway·istio
sc_爬坑之路4 天前
redis windows环境配置读写分离:一主一从 + Sentinel 完整实战
windows·redis·sentinel
sc_爬坑之路4 天前
Linux 部署 Redis:一主一从 + Sentinel 完整实战
linux·redis·sentinel