Implement Single-Node QPS Rate Limiting with Sentinel in SpringBoot
This guide walks through creating a SpringBoot service, adding Sentinel dependencies, writing a high‑traffic test endpoint, explaining Sentinel’s entry/exit logic, and configuring the Sentinel dashboard to enforce a single‑node QPS limit of 20, complete with code samples and screenshots.
Backend developers often need to handle message queues in micro‑service architectures, and when downstream services become a bottleneck, rate limiting is required.
Create a SpringBoot service
Add the following dependencies to
pom.xml:
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.4</version>
</dependency></code>Expose an HTTP endpoint that triggers the rate‑limiting logic:
<code>package com.example.demo.controller;
import com.alibaba.csp.sentinel.SphO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Function:
* Author:ziyou
* Date:2022-05-08 12:56
* Desc:无
*/
@RestController
public class LoginController {
@GetMapping(value = "/login")
public void login(String username, String password) {
System.out.println("login");
//模拟一百万条消息
for (int i = 0; i < 1000000; i++) {
boolean entry = false;
try {
entry = SphO.entry("HelloWorld");
while (!entry) {
try {
Thread.sleep(50);
System.out.println("entry false");
entry = SphO.entry("HelloWorld");
} catch (InterruptedException e) {
}
}
System.out.println("entry true");
} catch (Exception e) {
} finally {
if (entry) {
SphO.exit();
}
}
}
}
}
</code>The loop simulates one million messages to test high traffic.
SphO.entry("HelloWorld") is Sentinel’s resource controller; it returns true when the resource is available and false when it is limited.
When the resource is limited, the program waits before retrying.
In the finally block you must call SphO.exit() to release the entry, otherwise memory leaks and FullGC may occur.
Running the service without a rule shows no throttling.
Configure the Sentinel dashboard
Download the Sentinel dashboard JAR (e.g., version 1.8.4) and start it:
<code>java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar</code>Log in with the default credentials (both username and password are
sentinel).
Add the transport dependency to
pom.xml:
<code><dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.4</version>
</dependency></code>Include the JVM argument
-Dcsp.sentinel.dashboard.server=localhost:8081when starting the application.
After configuring a QPS limit of 20 in the dashboard, invoking the endpoint shows the processing speed reduced to 20 requests per second, achieving the desired single‑node rate limiting.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.