dawdler-config-center-core
Module Introduction
Unified configuration center core module.
1. The FieldConfig annotation
FieldConfig is used on global variables of classes that support configuration.
FieldConfig source:
@Retention(value = RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD })
public @interface FieldConfig {
//Path in the configuration center, e.g. a Consul path
String path();
//Expression, e.g. name or user.username. Note this differs from SpEL; it is implemented with Jexl.
String value() default "";
}
Example of using FieldConfig:
The path in Consul is: orderConfig
Content of the yml file
order:
payOutTime: 3000
queueName: orderChangeStatusQueue
Configuration in the controller:
@RequestMapping(value="/order")
public class OrderController{
@RemoteService
OrderService orderService;
@FieldConfig(path="orderConfig",value = "order.queueName")
private String queueName;
}
Another example:
Multiple levels are supported, and mapping to entity beans is supported as well.
order:
queueName: orderChangeStatusQueue
payConfig:
payOutTime: 3000
payKey: UFFSA32FJJFJF
@FieldConfig(path="orderConfig",value = "order.payConfig.payKey")
private String payKey;
2. Strict mode and refresh-write semantics
Injection and refresh-write of @FieldConfig fields use volatile write semantics (based on VarHandle), so other threads reading the field see the new value immediately after the configuration center pushes a change.
Startup injection (singleton service creation, listener/filter injection, web component loading) enables strict mode by default: if a field refresh fails (e.g. the configuration center path does not exist, or expression evaluation fails) or the configuration is missing so the field is not written, startup fails immediately with a ConfigRefreshException, preventing an object with missing configuration from going live. Refreshes triggered by runtime configuration pushes do not use strict mode; failures are only logged and do not affect the running service.
final fields do not support configuration refresh and cause a startup error in strict mode (remove the final modifier or do not use @FieldConfig on final fields).
To disable strict mode (e.g. some fields are intentionally left unconfigured), use the JVM parameter:
-Ddawdler.config.refresh.strict=false
When disabled, injection failures at startup are only logged and no longer block startup.
3. Local snapshot disaster recovery
Every successful configuration fetch writes the raw content (before placeholder resolution and decryption; plain-text secrets are never persisted) to a local snapshot file. If the configuration center is unreachable at startup, the service starts by replaying the last snapshot (same mechanism as the local snapshots of Nacos/Apollo), a background watcher keeps retrying, and once the configuration center recovers the service automatically returns to live configuration after comparing versions.
Overall flow (blue nodes are the snapshot disaster-recovery paths):
flowchart TD
subgraph BOOT["① Startup path · ConfigInit.init()"]
S0["Service startup"] --> S1["preload(): read snapshot.yml · activate write channel"]
S1 -->|"File missing / corrupted"| S2["Skip replay · start persisting on first successful fetch"]
S1 -->|"Data present"| S3["Replay raw content entry by entry via addConfigData<br/>re-run placeholder resolution + decryption"]
S2 --> S4["ConsulConfigClient.start()<br/>initial flushCache fetch of KVs"]
S3 --> S4
S4 -->|"consul reachable"| S5["Update in-memory cache after version comparison<br/>(old snapshot values overwritten)"]
S4 -->|"unreachable"| S6{"In-memory cache non-empty?<br/>(= snapshot preloaded)"}
S6 -->|"No · no snapshot"| S7["Rethrow as-is → startup failure<br/>(fail-fast)"]
S6 -->|"Yes"| S8["error log: configuration center unreachable<br/>starting from snapshot"]
S5 --> W["Submit watcher thread"]
S8 --> W
W --> S9{"Inject @FieldConfig<br/>strict mode (on by default)"}
S9 -->|"path has a value (remote or snapshot)"| S10["Service goes live"]
S9 -->|"path missing"| S11["ConfigRefreshException<br/>startup failure · never go live with empty config"]
end
subgraph RUN["② Runtime writes · on every successful fetch"]
R0["watcher detects a KV change<br/>flushCache"] --> R1{"version ≠ modifyIndex?"}
R1 -->|"Equal"| R2["Skip · no rebind churn"]
R1 -->|"Different"| R3["addConfigData(path, raw content, version)"]
R3 --> R4["Persist: temp file + ATOMIC_MOVE<br/>write failure degrades · stop writing after two failures"]
R3 --> R5["Memory: resolve + decrypt → cache put"]
R5 --> R6["rebind refreshes @FieldConfig fields"]
end
subgraph REC["③ Snapshot-mode operation → automatic recovery"]
E0["Running in snapshot mode<br/>health check info() throws → visible as DOWN"] --> E1["watcher retries every 10s"]
E1 -->|"consul recovered"| E2["flushCache follows ②'s version comparison:<br/>equal → no-op · different → update+persist+rebind, back to live configuration"]
end
W -.-> R0
S8 -.->|"stays alive on the snapshot until recovery"| E0
classDef snapshot fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#0c1a2b
class S1,S2,S3,S6,S8,R4 snapshot
- Snapshot file: defaults to
${user.home}/.dawdler/config-snapshot/snapshot.yml, adjustable via the JVM parameter-Ddawdler.config.snapshot.dir=<directory>. - Isolation of multiple services on one machine: when several services run under the same OS user on the same machine, configure a reserved key in dawdler-config.yml (e.g.
snapshot-id: order-service), and each service writes its snapshot tobase/<snapshot-id>/snapshot.ymlwithout overwriting the others. Single-service / containerized deployment (independent file system) / separate OS-user deployment (user.home is naturally separate) require no configuration. - Caveat when snapshot-id is not configured: without it, a single shared file is used (backward compatible, same as existing behavior). The risk only exists in the multi-service, same-machine, same-user scenario — the snapshot is rewritten as a whole file and each process only holds the paths it fetched, so a later-started process overwrites earlier entries when persisting (that service loses its disaster-recovery copy and, when the configuration center is unreachable, cannot replay its own configuration and falls back to fail-fast). If multiple services watch the same path but connect to different configuration centers, the later write overrides the earlier one and disaster-recovery startup may replay another cluster's configuration for that path. Rule of thumb: if multiple JVMs connecting to different configuration centers will run under the same user.home, configure a separate snapshot-id for each service (or give each service its own directory via
-Ddawdler.config.snapshot.dir). - Successful fetch with a version change: persist (temp file + atomic move; on file systems that do not support atomic move it degrades to a normal move; writing stops after two consecutive failures, so a disk failure does not affect the main configuration-refresh path).
- Remote deletion propagation: after a successful full listing by the watcher, paths that are cached locally but deleted remotely (under the watchKey prefix) are removed from the in-memory cache / mapped derived cache / local snapshot entries; when a whole prefix is deleted (the listing returns null) all entries under that prefix are removed. Already-injected @FieldConfig fields retain their last value and are not rewritten. Propagation happens only after a successful full listing; a failed fetch (exception path) does not clean up, so partial failures never cause incorrect deletions.
- Startup preload: if the file is missing or corrupted, replay is simply skipped and the write channel is still activated — on a brand-new machine the first successful fetch starts persisting immediately, and a corrupted snapshot file is overwritten with good data on the next fetch, healing itself.
- Initial fetch failure: if the in-memory cache has data (the snapshot was preloaded), an error is logged and startup continues in snapshot disaster-recovery mode; without a snapshot, the error is rethrown as-is, preserving fail-fast.
- Paths not covered by the snapshot are still subject to strict mode (see above); the service never goes live with missing configuration. While running in snapshot mode the health check (ConfigIndicator) is DOWN, which can be recognized in monitoring.
- Note: unencrypted configuration content is persisted in plain text (same as Nacos/Apollo). In security-sensitive environments, restrict file system permissions on the snapshot directory.
4. Extending with other configuration centers
To extend with another configuration center such as ZooKeeper or Apollo, implement the ConfigClient interface and plug it in via SPI.
public interface ConfigClient {
void init(Map<String, Object> conf);
void start();
void stop();
String info() throws Exception;
String type();
}
Refer to ConsulConfigClient for an implementation. It is recommended to implement snapshot disaster-recovery degradation on initial fetch failure following ConsulConfigClient's pattern (fall back to snapshot startup if the in-memory cache has data; rethrow as-is and keep fail-fast if it has none). If you run into difficulties extending another configuration center, feel free to open an issue.