How to Implement Interface Rate Limiting with Sentinel in Spring Cloud Alibaba
This tutorial explains how Sentinel, the distributed system traffic guardian, can be deployed and integrated into a Spring Cloud Alibaba application to provide flow control and circuit breaking, covering dashboard setup, Maven configuration, code examples, rule creation, and verification of rate‑limiting behavior.
Sentinel, described as the "distributed system traffic guardian", provides flow control and circuit breaking for service stability, and is a recommended alternative to Hystrix in Spring Cloud Alibaba.
Deploy Sentinel Dashboard
Download the matching version of sentinel-dashboard-1.4.0.jar and start it with java -jar sentinel-dashboard-1.4.0.jar. The dashboard runs on port 8080 by default and can be accessed at http://localhost:8080.
Integrate Sentinel into a Spring Cloud application
Step 1: Add the spring-cloud-starter-alibaba-sentinel dependency (and optional Lombok) to the project's pom.xml.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>Step 2: Configure the dashboard address in application.properties (e.g., spring.cloud.sentinel.transport.dashboard=localhost:8080) and set the service name.
Step 3: Create a Spring Boot main class and a simple REST controller exposing /hello that returns a string.
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
@RestController
class TestController {
@GetMapping("/hello")
public String hello() {
return "didispace.com";
}
}Step 4: Run the application (e.g., on port 8001) and verify the endpoint works with curl http://localhost:8001/hello. The request appears in the Sentinel Dashboard.
Configure a flow‑control rule
In the dashboard, select the service alibaba-sentinel-rate-limiting, open the “Flow Control” tab, add a new rule for the /hello resource, choose QPS as the threshold type, and set the limit to 2 requests per second.
After saving, the rule is listed under “Flow Control Rules”.
Validate the rule
Calling the endpoint three times quickly shows the third request blocked with the message “Blocked by Sentinel (flow limiting)”.
$ curl http://localhost:8001/hello
didispace.com
$ curl http://localhost:8001/hello
didispace.com
$ curl http://localhost:8001/hello
Blocked by Sentinel (flow limiting)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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
