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

相关推荐
T_Ghost3 小时前
SpringCloud微服务服务容错机制Sentinel熔断器
spring cloud·微服务·sentinel
你是人间五月天21 小时前
sentinel实现控制台与nacos数据双向绑定
windows·sentinel
无名客021 小时前
sentinel限流常见的几种算法以及优缺点
算法·sentinel·限流
tsxchen1 天前
centos9安装sentinel
sentinel
寒士obj1 天前
Sentinel服务治理:服务降级、熔断与线程隔离
sentinel
iiYcyk1 天前
Hystrix与Sentinel-熔断限流
hystrix·sentinel·springcloud
勇往直前plus1 天前
Sentinel微服务保护
java·spring boot·微服务·sentinel
boy快快长大3 天前
【Spring Cloud Alibaba】Sentinel(一)
sentinel
java干货3 天前
Sentinel和Cluster,到底该怎么选?
sentinel
27^×3 天前
Sentinel 与 Feign 整合详解:实现服务调用的流量防护
sentinel