Store Spring Cloud Alibaba Sentinel Flow Rules in Apollo – Step‑by‑Step Guide
This tutorial walks you through configuring Spring Cloud Alibaba Sentinel to persist its flow‑control rules in Apollo, covering required dependencies, property files, datasource settings, a sample REST endpoint, rule JSON format, and verification via logs and the Sentinel dashboard.
Using Apollo to Store Sentinel Flow Rules
After introducing how to store flow‑control rules with Nacos, we now demonstrate how to persist Sentinel rules in Apollo, a widely used configuration center in China.
Supported Sentinel Data Sources
File configuration
Nacos configuration
ZooKeeper configuration
Apollo configuration
We will focus on the Apollo option.
Preparation
Make sure both Apollo and the Sentinel Dashboard are running.
Step 1 – Add Maven Dependencies
<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-apollo</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>Step 2 – Create Apollo Environment File
local.meta=http://192.168.0.201:8080
dev.meta=http://192.168.0.202:8080Adjust the addresses to match your environment.
Step 3 – Add Spring Cloud Configuration
spring.application.name=sentinel-datasource-apollo
server.port=8002
# Apollo configuration
app.id=${spring.application.name}
# Sentinel dashboard address
spring.cloud.sentinel.transport.dashboard=localhost:8080
# Apollo datasource settings
spring.cloud.sentinel.datasource.ds.apollo.namespaceName=application
spring.cloud.sentinel.datasource.ds.apollo.flowRulesKey=sentinel.flowRulesStep 4 – Create Application Main Class and REST Endpoint
@EnableApolloConfig
@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";
}
}
}The @EnableApolloConfig annotation activates Apollo configuration loading.
Step 5 – Define Flow Rule in Apollo
[
{
"resource": "/hello",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]Field explanations:
resource : the protected resource name.
limitApp : caller source; default means all callers.
grade : 0 for concurrency, 1 for QPS.
count : threshold value.
strategy : relationship‑based limiting strategy.
controlBehavior : 0‑direct reject, 1‑warm‑up, 2‑rate‑limit queue.
clusterMode : whether cluster mode is enabled.
Step 6 – Run the Application
When the application starts you should see logs similar to:
INFO SentinalDataSourceHandler: datasource start to loadConfig
INFO SentinalDataSourceHandler: datasource load 1 FlowRuleTest the endpoint with curl localhost:8002/hello – it returns didispace.com. The rule will appear in the Sentinel Dashboard under the Apollo datasource.
Important Note
Modifying rules in the Sentinel console only changes the in‑memory state; Apollo’s persisted configuration remains unchanged and will be restored after a restart.
Changes made in Apollo (or Nacos) are loaded into the service and survive restarts.
Reference Code Repository
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
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.
