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

相关推荐
东阳马生架构9 小时前
Sentinel源码—7.参数限流和注解的实现一
java·sentinel
B博士10 小时前
Sentinel数据S2_SR_HARMONIZED连续云掩膜+中位数合成
sentinel·云掩膜·去云·qa60·scl·msk_cldprb
码代码的小农21 小时前
深入浅出Sentinel:分布式系统的流量防卫兵
sentinel
搬砖天才、2 天前
日常记录-redis主从复制(master-slave)+ Sentinel 哨兵(高可用)
数据库·redis·sentinel
是赵敢敢啊2 天前
sentinel
sentinel
东阳马生架构3 天前
Sentinel源码—9.限流算法的实现对比二
算法·sentinel
东阳马生架构3 天前
Sentinel源码—9.限流算法的实现对比一
算法·sentinel
东阳马生架构3 天前
Sentinel源码—9.限流算法的实现对比
sentinel
东阳马生架构4 天前
Sentinel源码—7.参数限流和注解的实现二
java·sentinel
东阳马生架构4 天前
Sentinel源码—8.限流算法和设计模式总结一
算法·sentinel