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

相关推荐
炸裂狸花猫1 天前
Kubernetes从零到精通(12-Ingress、Gateway API)
容器·kubernetes·gateway
1.01^10002 天前
[000-01-008].第08节:Sentinel 环境搭建
sentinel
世俗ˊ2 天前
微服务-- Sentinel的使用
java·微服务·sentinel
云来喜3 天前
关于Spring Cloud Gateway中 Filters的理解
java·spring boot·gateway
小小小小关同学3 天前
【Gateway】网关服务快速上手
java·开发语言·gateway
小小小小关同学3 天前
【Gateway】Gateway Filter Factories
网络·gateway
两仪式quq3 天前
服务网关Gateway快速入门
服务器·网络·gateway
朱杰jjj3 天前
Sentinel实时监控不展示问题
sentinel
szc17675 天前
Gateway学习笔记
笔记·学习·gateway
灰色孤星A6 天前
微服务保护学习笔记(五)Sentinel授权规则、获取origin、自定义异常结果、规则持久化
微服务·sentinel·微服务保护·规则持久化·授权规则·雪崩问题