How to Integrate Sentinel with Spring Cloud Gateway for Flow Control
This guide explains how to use Alibaba Sentinel with Spring Cloud Gateway, covering dependency setup, YAML configuration, gateway startup parameters, and defining flow‑control rules to protect microservice routes, including code examples and a brief summary of customization options.
Introduction
Sentinel can not only be used between services but also works perfectly with service gateways such as Spring Cloud Gateway or Zuul to implement flow control. Since version 1.6.0, Sentinel provides an adapter module for Spring Cloud Gateway, offering two resource dimensions for rate limiting:
Route dimension: the route entry configured in Spring configuration files, with the resource name being the corresponding routeId.
Custom API dimension: users can define API groups using Sentinel's provided API.
Dependency
Include the following Maven dependency:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>YAML Configuration
Configure the Sentinel dashboard address, Nacos discovery, and gateway routes as follows:
server:
port: 2001
spring:
application:
name: gateway-service
profiles:
active: dev
cloud:
# Registration center
nacos:
discovery:
server-addr: 127.0.0.1:8848
# Flow control configuration
sentinel:
transport:
port: 8719
dashboard: localhost:8080
# Degrade strategy
scg:
fallback:
mode: response
response-status: 426
response-body: error request
# Gateway configuration
gateway:
routes:
- id: stock_route
uri: lb://stock-service
predicates:
- Path=/stock/**Start the Gateway
Add the following JVM argument to mark the application as an API Gateway type:
-Dcsp.sentinel.app.type=1Configure Flow‑Control Rules
Use the Sentinel console to add rate‑limiting rules for the stock_route resource.
Summary
By integrating Sentinel with a gateway, you can easily set up flow control for a cluster of services or multiple service entry points. The example shows basic error‑response handling; for custom exception handling and more complex production scenarios, you can extend Sentinel's features or customize its source code.
Reference
https://github.com/alibaba/Sentinel/wiki
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
