Design and Implementation of a Simple Single‑Node Configuration Center in Spring Boot
This article explains how to build a lightweight, single‑machine configuration center for Spring Boot micro‑services, detailing the core classes, file scanning, variable pooling, environment initialization, YAML conversion, controller endpoints, and runtime property updates, while also discussing current limitations and future improvements.
Overview
The author presents a minimal configuration‑center implementation for Spring Boot micro‑service projects, focusing on the essential feature of real‑time configuration refresh after file changes.
Core Classes
The design revolves around eight classes in the core package. ScanRunner implements CommandLineRunner to run after the Spring context is ready, scanning all compiled .class files with FileScanner . Classes annotated with @Component , @Controller or @Service are collected into VariablePool , where fields marked with @Value are recorded.
File Scanning
FileScanner recursively walks a given root directory, filters files by suffix (e.g., .class or .yml ), and normalizes Windows paths to obtain fully‑qualified class names for reflection.
Variable Pool
VariablePool stores a map of configuration keys to the classes and field names that depend on them, using a regular expression to validate ${…} placeholders before stripping the delimiters.
Environment Initialization
EnvInitializer loads all .yml resources found by FileScanner , parses them with Spring’s YamlMapFactoryBean , and merges the resulting maps into a single‑layer envMap via YamlConverter.doConvert .
YAML Conversion
YamlConverter provides three utilities: doConvert flattens nested maps, monoToMultiLayer reconstructs a hierarchical map from dot‑separated keys, and convert parses a YAML string back into a flat map for change detection.
Controller and Property Trigger
ConfigController exposes /get and /save endpoints. /get returns the current configuration as a YAML string, while /save receives edited YAML, invokes PropertyTrigger.change , which compares old and new values, updates envMap , and uses reflection together with SpringContextUtil to set the new values on the affected beans.
Limitations
Does not support @ConfigurationProperties annotation.
Only processes YAML files, not .properties .
Works only with singleton‑scoped beans; prototype beans are not handled.
Reflection introduces performance overhead for heavily used properties.
Currently embedded in the application and lacks independent deployment or remote registration capabilities.
Conclusion
While the prototype demonstrates the core mechanics of a dynamic configuration center, many features such as broader annotation support, property‑file handling, prototype scope, performance optimisation, and distributed deployment remain to be implemented.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.