Persist Sentinel Rate‑Limiting Rules with Nacos in Spring Cloud Alibaba

This tutorial shows how to integrate Sentinel with Nacos to persist interface rate‑limiting rules in a Spring Cloud Alibaba application, covering dependency setup, configuration properties, sample code, Nacos rule definition, and verification steps.

Programmer DD
Programmer DD
Programmer DD
Persist Sentinel Rate‑Limiting Rules with Nacos in Spring Cloud Alibaba

After reading the previous article on using Sentinel for interface rate limiting, you should already have a basic understanding of Sentinel. With Spring Cloud Alibaba integration, adding rate limiting to a Spring Cloud application is straightforward, but persisting the rules remains a challenge; this guide explains how to achieve rule persistence using Nacos.

Using Nacos to Store Rate‑Limiting Rules

Sentinel supports multiple data sources for rule persistence, including file configuration, Nacos, ZooKeeper, and Apollo. This article demonstrates using Spring Cloud Alibaba's integrated configuration center Nacos to store Sentinel rules.

Preparation

Ensure both Nacos and Sentinel Dashboard are running. Default access URLs are:

Nacos: http://localhost:8848/

Sentinel Dashboard: http://localhost:8080/

Application Configuration

Step 1: Add Sentinel and Nacos datasource dependencies to 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>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
        <version>1.4.0</version>
    </dependency>
</dependencies>

Step 2: Add the following properties to application.properties (replace ${spring.application.name} with your app name):

spring.application.name=alibaba-sentinel-datasource-nacos
server.port=8003
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.datasource.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.nacos.dataId=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.nacos.groupId=DEFAULT_GROUP

These properties configure Sentinel to read rules from Nacos using the specified dataId and groupId.

Code Example

Step 3: Create the main application class with a simple REST endpoint:

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @RestController
    static class TestController {
        @GetMapping("/hello")
        public String hello() {
            return "didispace.com";
        }
    }
}

Define Rules in Nacos

Step 4: In the Nacos console, create a new configuration with the following JSON content (choose JSON format, set Data ID and Group ID to match the properties above):

[
  {
    "resource": "/hello",
    "limitApp": "default",
    "grade": 1,
    "count": 5,
    "strategy": 0,
    "controlBehavior": 0,
    "clusterMode": false
  }
]

The fields mean:

resource : the protected resource (e.g., the URL path).

limitApp : the caller source; default means no distinction.

grade : 0 for concurrency, 1 for QPS.

count : the threshold value.

strategy : flow control strategy.

controlBehavior : effect (reject, warm‑up, queue).

clusterMode : whether to use cluster mode.

Run and Verify

Step 5: Start the application. You should see logs indicating that Sentinel loaded the rule from Nacos, for example:

2019-04-16 14:24:42.919 INFO 89484 --- [main] o.s.c.a.s.datasource.SentinelDataSourceHandler: SentinelStarter datasource start to loadConfig
2019-04-16 14:24:42.938 INFO 89484 --- [main] o.s.c.a.s.datasource.SentinelDataSourceHandler: SentinelStarter datasource load 1 FlowRule

Send a request to http://localhost:8003/hello (e.g., using curl or Postman). The response should be didispace.com, and the Sentinel Dashboard will display the loaded rule under the flow‑control section.

Important Note

Modifying rules in the Sentinel Dashboard only changes the in‑memory configuration and will be lost after a restart. Changing the rule in the Nacos console updates both the persisted configuration and the in‑memory rule, surviving restarts.

For more advanced configurations and additional data‑source options, refer to the official Spring Cloud Alibaba and Sentinel documentation.

backendNacosSentinelDataSourcerate-limitingspring-cloud-alibaba
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.