spring cloud gateway + nacos + sentinel实现网关限流

背景

项目中有一些接口是非常消耗资源和耗时的,出于保护系统的目的,需要限流

目标

实现网关接口限流,保护系统

方案

项目采用了spring cloud gateway作为网关,nacos作为配置中心和注册中心

sentinel是开源的比较优秀的限流组件,此文采用sentinel作为限流实现

下面重点介绍sentinel集成

maven坐标

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
 <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>

限流组件相关配置

限流配置

配置参考com.alibaba.cloud.sentinel.datasource.RuleType

sentinel-gw-flow.json,参考com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule

sentinel-api-group.json,参考com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition

最后更新于