Backend Development 7 min read

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.

macrozheng
macrozheng
macrozheng
Implement Single-Node QPS Rate Limiting with Sentinel in SpringBoot

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>&lt;dependency&gt;
   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
   &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
   &lt;groupId&gt;com.alibaba.csp&lt;/groupId&gt;
   &lt;artifactId&gt;sentinel-core&lt;/artifactId&gt;
   &lt;version&gt;1.8.4&lt;/version&gt;
&lt;/dependency&gt;</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>&lt;dependency&gt;
   &lt;groupId&gt;com.alibaba.csp&lt;/groupId&gt;
   &lt;artifactId&gt;sentinel-transport-simple-http&lt;/artifactId&gt;
   &lt;version&gt;1.8.4&lt;/version&gt;
&lt;/dependency&gt;</code>

Include the JVM argument

-Dcsp.sentinel.dashboard.server=localhost:8081

when 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.

JavamicroservicesSentinelSpringBootrate limitingQPS
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.