0
# Rule Management
1
2
Core functionality for defining, loading, and managing parameter flow control rules. The `ParamFlowRuleManager` provides centralized rule configuration with support for dynamic updates and rule validation.
3
4
## Capabilities
5
6
### Load Rules
7
8
Loads parameter flow rules, replacing any previously configured rules. This is the primary method for configuring parameter flow control.
9
10
```java { .api }
11
/**
12
* Load parameter flow rules. Former rules will be replaced.
13
* @param rules new rules to load
14
*/
15
public static void loadRules(List<ParamFlowRule> rules);
16
```
17
18
**Usage Example:**
19
20
```java
21
import com.alibaba.csp.sentinel.slots.block.flow.param.*;
22
import java.util.Arrays;
23
24
// Create multiple rules
25
ParamFlowRule userRule = new ParamFlowRule("userService")
26
.setParamIdx(0)
27
.setCount(10)
28
.setGrade(RuleConstant.FLOW_GRADE_QPS);
29
30
ParamFlowRule productRule = new ParamFlowRule("productService")
31
.setParamIdx(1)
32
.setCount(20)
33
.setGrade(RuleConstant.FLOW_GRADE_QPS);
34
35
// Load all rules at once
36
ParamFlowRuleManager.loadRules(Arrays.asList(userRule, productRule));
37
```
38
39
### Register Property Listener
40
41
Registers a property listener for dynamic rule updates from external configuration sources.
42
43
```java { .api }
44
/**
45
* Listen to the SentinelProperty for ParamFlowRules. The property is the source of ParamFlowRules.
46
* Parameter flow rules can also be set by loadRules(List) directly.
47
* @param property the property to listen
48
*/
49
public static void register2Property(SentinelProperty<List<ParamFlowRule>> property);
50
```
51
52
**Usage Example:**
53
54
```java
55
import com.alibaba.csp.sentinel.property.DynamicSentinelProperty;
56
import com.alibaba.csp.sentinel.property.SentinelProperty;
57
58
// Create a dynamic property for rule updates
59
SentinelProperty<List<ParamFlowRule>> ruleProperty = new DynamicSentinelProperty<>();
60
61
// Register the property with the rule manager
62
ParamFlowRuleManager.register2Property(ruleProperty);
63
64
// Rules can now be updated through the property
65
ruleProperty.updateValue(newRules);
66
```
67
68
### Get Rules for Resource
69
70
Retrieves all parameter flow rules configured for a specific resource.
71
72
```java { .api }
73
/**
74
* Get rules for a specific resource
75
* @param resourceName the resource name
76
* @return list of rules for the resource, never null
77
*/
78
public static List<ParamFlowRule> getRulesOfResource(String resourceName);
79
```
80
81
**Usage Example:**
82
83
```java
84
// Get all rules for a specific resource
85
List<ParamFlowRule> userServiceRules = ParamFlowRuleManager.getRulesOfResource("userService");
86
87
// Check if any rules exist
88
if (!userServiceRules.isEmpty()) {
89
System.out.println("Found " + userServiceRules.size() + " rules for userService");
90
for (ParamFlowRule rule : userServiceRules) {
91
System.out.println("Parameter index: " + rule.getParamIdx() + ", Count: " + rule.getCount());
92
}
93
}
94
```
95
96
### Check if Resource Has Rules
97
98
Checks whether a resource has any configured parameter flow rules.
99
100
```java { .api }
101
/**
102
* Check if a resource has any parameter flow rules configured
103
* @param resourceName the resource name
104
* @return true if rules exist, false otherwise
105
*/
106
public static boolean hasRules(String resourceName);
107
```
108
109
**Usage Example:**
110
111
```java
112
// Check if resource has rules before processing
113
if (ParamFlowRuleManager.hasRules("userService")) {
114
// Parameter flow control is enabled for this resource
115
System.out.println("Parameter flow control active for userService");
116
} else {
117
System.out.println("No parameter flow rules configured for userService");
118
}
119
```
120
121
### Get All Rules
122
123
Retrieves a copy of all currently loaded parameter flow rules.
124
125
```java { .api }
126
/**
127
* Get a copy of all the rules
128
* @return a new copy of all the rules
129
*/
130
public static List<ParamFlowRule> getRules();
131
```
132
133
**Usage Example:**
134
135
```java
136
// Get all configured rules
137
List<ParamFlowRule> allRules = ParamFlowRuleManager.getRules();
138
139
// Display rule summary
140
System.out.println("Total rules configured: " + allRules.size());
141
for (ParamFlowRule rule : allRules) {
142
System.out.println("Resource: " + rule.getResource() +
143
", Param Index: " + rule.getParamIdx() +
144
", Count: " + rule.getCount());
145
}
146
```
147
148
## Rule Validation
149
150
The rule manager automatically validates rules during loading:
151
152
- Resource name must not be blank
153
- Count must be non-negative
154
- Grade (QPS/Thread) must be valid
155
- Parameter index must not be null
156
- Control behavior must be valid
157
- Cluster configuration must be valid if cluster mode is enabled
158
159
Invalid rules are logged and ignored during the loading process.
160
161
## Thread Safety
162
163
All `ParamFlowRuleManager` methods are thread-safe and can be called concurrently from multiple threads. Rule updates are atomic and consistent across all threads.