0
# Configuration Management
1
2
Centralized configuration management with real-time updates, change listeners, atomic CAS operations, and pattern-based watching. Supports multiple configuration formats with filtering mechanisms for request/response processing.
3
4
## Capabilities
5
6
### Core Configuration Operations
7
8
Basic configuration retrieval, publishing, and removal operations for managing application configurations.
9
10
```java { .api }
11
public interface ConfigService {
12
// Get configuration content
13
String getConfig(String dataId, String group, long timeoutMs) throws NacosException;
14
15
// Get configuration and register listener simultaneously
16
String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException;
17
18
// Publish configuration
19
boolean publishConfig(String dataId, String group, String content) throws NacosException;
20
21
// Publish configuration with type specification
22
boolean publishConfig(String dataId, String group, String content, String type) throws NacosException;
23
24
// Atomic compare-and-swap publish operation
25
boolean publishConfigCas(String dataId, String group, String content, String casMd5) throws NacosException;
26
27
// Remove configuration
28
boolean removeConfig(String dataId, String group) throws NacosException;
29
30
// Get server status
31
String getServerStatus();
32
33
// Shutdown service
34
void shutDown() throws NacosException;
35
}
36
```
37
38
#### Usage Examples
39
40
```java
41
import com.alibaba.nacos.api.config.ConfigService;
42
import com.alibaba.nacos.api.config.ConfigFactory;
43
import com.alibaba.nacos.api.exception.NacosException;
44
45
// Create config service
46
Properties props = new Properties();
47
props.setProperty("serverAddr", "localhost:8848");
48
ConfigService configService = ConfigFactory.createConfigService(props);
49
50
// Get configuration with timeout
51
String config = configService.getConfig("database.properties", "DEFAULT_GROUP", 5000);
52
53
// Publish configuration
54
boolean success = configService.publishConfig(
55
"database.properties",
56
"DEFAULT_GROUP",
57
"url=jdbc:mysql://localhost:3306/mydb\nusername=admin"
58
);
59
60
// CAS operation for atomic updates
61
String currentMd5 = "abc123"; // MD5 of current config
62
boolean updated = configService.publishConfigCas(
63
"database.properties",
64
"DEFAULT_GROUP",
65
"url=jdbc:mysql://newhost:3306/mydb\nusername=admin",
66
currentMd5
67
);
68
```
69
70
### Configuration Listeners
71
72
Real-time configuration change notifications through listener pattern.
73
74
```java { .api }
75
public interface ConfigService {
76
// Add configuration change listener
77
void addListener(String dataId, String group, Listener listener) throws NacosException;
78
79
// Remove configuration change listener
80
void removeListener(String dataId, String group, Listener listener);
81
}
82
83
// Base listener interface
84
public interface Listener {
85
// Receive configuration updates
86
void receiveConfigInfo(String configInfo);
87
88
// Get executor for async processing (optional)
89
Executor getExecutor();
90
}
91
92
// Abstract base class for listeners
93
public abstract class AbstractListener implements Listener {
94
public abstract void receiveConfigInfo(String configInfo);
95
96
@Override
97
public Executor getExecutor() {
98
return null; // Use default executor
99
}
100
}
101
```
102
103
#### Usage Examples
104
105
```java
106
import com.alibaba.nacos.api.config.listener.Listener;
107
import com.alibaba.nacos.api.config.listener.AbstractListener;
108
109
// Simple listener implementation
110
Listener listener = new AbstractListener() {
111
@Override
112
public void receiveConfigInfo(String configInfo) {
113
System.out.println("Configuration updated: " + configInfo);
114
// Reload application configuration
115
reloadConfig(configInfo);
116
}
117
};
118
119
// Register listener
120
configService.addListener("database.properties", "DEFAULT_GROUP", listener);
121
122
// Custom executor listener
123
Listener asyncListener = new AbstractListener() {
124
@Override
125
public void receiveConfigInfo(String configInfo) {
126
// Handle configuration change
127
handleConfigChange(configInfo);
128
}
129
130
@Override
131
public Executor getExecutor() {
132
return Executors.newSingleThreadExecutor();
133
}
134
};
135
```
136
137
### Fuzzy Watching
138
139
Pattern-based configuration watching for monitoring multiple configurations with wildcards.
140
141
```java { .api }
142
public interface ConfigService {
143
// Start fuzzy watching with group pattern only
144
void fuzzyWatch(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
145
146
// Start fuzzy watching with both dataId and group patterns
147
void fuzzyWatch(String dataIdPattern, String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
148
149
// Start fuzzy watching and return matching group keys
150
Future<Set<String>> fuzzyWatchWithGroupKeys(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
151
152
// Cancel fuzzy watching
153
void cancelFuzzyWatch(String dataIdPattern, String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
154
}
155
156
// Fuzzy watch event watcher interface
157
public interface FuzzyWatchEventWatcher {
158
// Handle set of configuration change events
159
void onEvent(Set<ConfigChangeEvent> events);
160
}
161
```
162
163
#### Usage Examples
164
165
```java
166
import com.alibaba.nacos.api.config.listener.FuzzyWatchEventWatcher;
167
import com.alibaba.nacos.api.config.ConfigChangeEvent;
168
169
// Watch all database-related configurations
170
FuzzyWatchEventWatcher watcher = new FuzzyWatchEventWatcher() {
171
@Override
172
public void onEvent(Set<ConfigChangeEvent> events) {
173
for (ConfigChangeEvent event : events) {
174
System.out.println("Config changed: " + event.getDataId() +
175
" in group: " + event.getGroup());
176
177
if (event.getChangeType() == PropertyChangeType.MODIFIED) {
178
System.out.println("Old: " + event.getOldContent());
179
System.out.println("New: " + event.getContent());
180
}
181
}
182
}
183
};
184
185
// Watch pattern: all .properties files in DATABASE_GROUP
186
configService.fuzzyWatch("*.properties", "DATABASE_GROUP", watcher);
187
188
// Watch all configurations in a group (group pattern only)
189
configService.fuzzyWatch("CACHE_GROUP", watcher);
190
191
// Watch with both patterns - all configs in groups matching pattern
192
configService.fuzzyWatch("*", "CACHE_*", watcher);
193
```
194
195
### Configuration Filters
196
197
Request and response filtering for configuration operations.
198
199
```java { .api }
200
// Configuration filter interface
201
public interface IConfigFilter {
202
void init(FilterConfig filterConfig);
203
void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain filterChain) throws NacosException;
204
}
205
206
// Abstract filter base class
207
public abstract class AbstractConfigFilter implements IConfigFilter {
208
@Override
209
public void init(FilterConfig filterConfig) {
210
// Default implementation
211
}
212
213
public abstract void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain filterChain) throws NacosException;
214
}
215
216
// Filter request interface
217
public interface IConfigRequest {
218
String getDataId();
219
String getGroup();
220
String getContent();
221
IConfigContext getConfigContext();
222
}
223
224
// Filter response interface
225
public interface IConfigResponse {
226
String getContent();
227
void setContent(String content);
228
IConfigContext getConfigContext();
229
}
230
```
231
232
## Types
233
234
### Configuration Events and Changes
235
236
```java { .api }
237
// Configuration change event
238
public class ConfigChangeEvent {
239
private String dataId;
240
private String group;
241
private String namespace;
242
private String content;
243
private String oldContent;
244
private PropertyChangeType changeType;
245
246
public String getDataId();
247
public String getGroup();
248
public String getNamespace();
249
public String getContent();
250
public String getOldContent();
251
public PropertyChangeType getChangeType();
252
}
253
254
// Individual configuration item change
255
public class ConfigChangeItem {
256
private String key;
257
private String oldValue;
258
private String newValue;
259
private PropertyChangeType type;
260
261
public String getKey();
262
public String getOldValue();
263
public String getNewValue();
264
public PropertyChangeType getType();
265
}
266
```
267
268
### Configuration Types and Constants
269
270
```java { .api }
271
// Configuration content types
272
public enum ConfigType {
273
PROPERTIES("properties"),
274
XML("xml"),
275
JSON("json"),
276
TEXT("text"),
277
HTML("html"),
278
YAML("yaml"),
279
TOML("toml"),
280
UNSET("unset");
281
282
public String getType();
283
}
284
285
// Property change types
286
public enum PropertyChangeType {
287
ADDED, // Property was added
288
MODIFIED, // Property was changed
289
DELETED // Property was removed
290
}
291
292
// Configuration property keys
293
public class PropertyKeyConst {
294
// Server configuration
295
public static final String SERVER_ADDR = "serverAddr";
296
public static final String CONTEXT_PATH = "contextPath";
297
public static final String CLUSTER_NAME = "clusterName";
298
public static final String ENCODE = "encode";
299
300
// Authentication
301
public static final String USERNAME = "username";
302
public static final String PASSWORD = "password";
303
public static final String ACCESS_KEY = "accessKey";
304
public static final String SECRET_KEY = "secretKey";
305
306
// Configuration specific
307
public static final String CONFIG_LONG_POLL_TIMEOUT = "configLongPollTimeout";
308
public static final String CONFIG_RETRY_TIME = "configRetryTime";
309
public static final String MAX_RETRY = "maxRetry";
310
public static final String ENABLE_REMOTE_SYNC_CONFIG = "enableRemoteSyncConfig";
311
}
312
```
313
314
### Annotation Support
315
316
```java { .api }
317
// Configuration value injection
318
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
319
@Retention(RetentionPolicy.RUNTIME)
320
public @interface NacosValue {
321
String value();
322
boolean autoRefreshed() default false;
323
}
324
325
// Configuration properties binding
326
@Target(ElementType.TYPE)
327
@Retention(RetentionPolicy.RUNTIME)
328
public @interface NacosConfigurationProperties {
329
String dataId();
330
String groupId() default "DEFAULT_GROUP";
331
boolean autoRefreshed() default false;
332
String type() default "properties";
333
}
334
335
// Configuration change listener
336
@Target(ElementType.METHOD)
337
@Retention(RetentionPolicy.RUNTIME)
338
public @interface NacosConfigListener {
339
String dataId();
340
String groupId() default "DEFAULT_GROUP";
341
}
342
343
// Ignore field during configuration binding
344
@Target(ElementType.FIELD)
345
@Retention(RetentionPolicy.RUNTIME)
346
public @interface NacosIgnore {
347
}
348
```
349
350
### Configuration Converters
351
352
```java { .api }
353
// Configuration content converter interface
354
public interface NacosConfigConverter<T> {
355
// Check if can convert to target type
356
boolean canConvert(Class<T> targetType);
357
358
// Convert configuration content to target type
359
T convert(String config);
360
}
361
```