Build a Distributed Configuration Center with Spring Boot and the Observer Pattern

This tutorial walks through the design and implementation of a distributed configuration center using Spring Boot, demonstrating how to define configuration entities, apply the observer pattern for dynamic updates, and integrate a thread‑pooled scheduler to keep configuration in sync without restarting services.

Programmer DD
Programmer DD
Programmer DD
Build a Distributed Configuration Center with Spring Boot and the Observer Pattern

Introduction

Configuration centers solve the problem of changing application parameters without restarting services, especially in large‑scale microservice architectures where manual restarts become impractical.

Configuration Entity Definition

A SysConfig class is defined with fields such as configId, configKey, configValue, configDesc, and metadata like projectId, moduleId, and md5Value to support version control.

Core Components

The core of the system consists of several components:

ConfigHolder : a singleton that stores all configurations in a ConcurrentHashMap and provides static get, set, and remove methods.

Config : a client‑side accessor offering get(key) and get(key, defaultValue) methods.

ConfigCommandLineRunner : a CommandLineRunner that creates a scheduled thread pool and starts a ConfigSubject to periodically pull configuration updates.

Observer Pattern

The system uses the classic observer pattern. Observable defines methods to add, delete, and notify observers. Observer defines an update(List<Object> context) method.

Two concrete observers are provided:

ConfigDirectUpdateObserver : adds a new configuration when the key does not exist.

ConfigMD5UpdateObserver : replaces an existing configuration when the MD5 hash of the value changes.

ConfigSubject

ConfigSubject

implements Observable and IConfigPullHandler. It holds a list of observers, a scheduled executor, and parameters for initial delay, period, and time unit. Its runExec() method schedules a recurring task that fetches all configurations from the database via ConfigRepository, compares them with the local cache, and notifies the appropriate observer for additions or MD5‑based updates.

public void doWork() {
    // fetch remote config map
    // compare with local ConfigHolder
    // notify observers for new or changed entries
}

Supporting Classes

SpringConfigTool implements ApplicationContextAware to allow non‑Spring classes to obtain Spring beans.

ConfigRepository uses JdbcTemplate to query the sys_config table, builds SysConfig objects, computes their MD5 values, and returns a Map<String, SysConfig>.

SQL provides the SQL statement to retrieve all active configurations.

Usage

1. Create the database and import the SysConfig.sql script.

2. Build the core module with Maven: mvn clean install -DskipTests.

3. Add the core dependency to your Spring Boot project and include the package in @ComponentScan.

4. Retrieve configuration values in code via Config.get("yourKey").

Conclusion

The tutorial demonstrates how the observer pattern enables decoupled, dynamic configuration updates, turning a static configuration file approach into a flexible, runtime‑modifiable service suitable for distributed systems.

Configuration Center Architecture
Configuration Center Architecture
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootObserver PatternConfiguration Center
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.