How to Configure Alibaba Sentinel Dynamic Rules with Nacos in Spring Cloud
This guide walks through adding the required Maven dependencies, setting up application.yml, and configuring Nacos as a dynamic data source for Alibaba Sentinel, enabling flow control rules and other rule types to be managed centrally in a Spring Cloud application.
For details on the data source types supported by Sentinel, refer to the Alibaba Sentinel dynamic rules (dynamic file data source) link.
1. Add Maven dependencies in pom.xml
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<spring-cloud-alibaba-dependencies.version>0.9.0.RELEASE</spring-cloud-alibaba-dependencies.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>This dependency is required.
2. Configure application.yml
server:
port: 9400
---
spring:
application:
name: my-app
---
management:
endpoints:
web:
exposure:
include: '*'
---
spring:
cloud:
sentinel:
eager: true # disable lazy loading
filter:
enabled: true # enable/disable protection of Spring MVC endpoints
transport:
port: 8730
dashboard: localhost:8080 # Sentinel dashboard address
heartbeatIntervalMs: 3000
clientIp: 127.0.0.1
---
spring:
cloud:
sentinel:
datasource:
ds:
nacos:
serverAddr: localhost:8848
dataId: ${spring.application.name}.sentinel.rule
groupId: study
namespace: 3d857485-5f81-4f1b-9a7e-86586d05261f
username: dev
password: 123123
ruleType: flowExplanation: ds is the name of the ReadableDataSource and can be any identifier; nacos denotes the specific data source, and the following fields configure that source.
serverAddr : address of the Nacos server
dataId : identifier of the configuration item in Nacos
groupId : group identifier in Nacos
namespace : namespace ID for separating environments
username and password : credentials for Nacos access
ruleType : type of Sentinel rule (flow, degrade, authority, system, param-flow, gw-flow, gw-api-group)
Note: If the same rule exists in multiple data sources, only one will take effect; in the example, the Nacos‑based rule overrides a file‑based rule.
Example Nacos configuration (image):
After starting the service, the configuration from Nacos appears in the Sentinel dashboard (images):
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
