Dynamic Configuration Capability for SpringBoot Application

Overview

Spring Boot Dynamic Config


codebeat badge

Hot-reload your SpringBoot configurations, with just a '@DynamicConfig' annotation, the simplest solution, ever.

English 简体中文

  • ❤️ Non-intrusive, compatible with SpringBoot native ways (@Value, @ConfigurationProperties)
  • Lightweight & Blazing Fast, depend on nothing but SpringBoot core libs
  • 😀 Extremely easy to use, only provide an annotation: @DynamicConfig, an event: ConfigurationChangedEvent
  • Perfect solution for hot-reloading configuration of SpringBoot application on Kubernetes, with K8S ConfigMap

Compare with spring-cloud-starter-config

  • No need for config server
  • No SpringCloud dependency and @RefreshScope annotation, won't destroy and rebuild beans

Compare with Alibaba Nacos / Ctripcorp Apollo

  • No need for Nacos/Apollo server
  • No need for learning Annotations, Client APIs, etc.

Demo

Demo

Getting Started

Step1. Add spring-boot-dynamic-config Dependency

Maven

<dependency>
    <groupId>top.code2life</groupId>
   <artifactId>spring-boot-dynamic-config</artifactId>
   <version>1.0.7</version>
</dependency>

Gradle

implementation 'top.code2life:spring-boot-dynamic-config:1.0.7'

Step2. Add @DynamicConfig Annotation

Option1: Add @DynamicConfig annotation on class which contains @Value field.

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import top.code2life.config.DynamicConfig;

import java.util.Set;

@Data
@Component
@DynamicConfig // add annotation here !
public class DynamicFeatures {

    @Value("${dynamic-test-plain:default}")
    private String plainValue;

    @Value("#{@featureGate.convert('${dynamic-feature-conf}')}")
    private Set<String> someBetaFeatureConfig;

    // @DynamicConfig // adding annotation here also works!
    @Value("#{@testComponent.transform(${dynamic.transform-a:20}, ${dynamic.transform-b:10})} ")
    private double transformBySpEL;


    public double transform(double t1, double t2) {
        return t1 / t2;
    }
}

// file: application-profile.yml
// ============================
// dynamic-test-plain: someVal # kebab-case is recommended
// dynamicFeatureConf: a,b,c  # camelCase compatible
// dynamic:
//   transform-a: 100
//   transform-b: 10

Option2: Add @ConfigurationProperties annotation on configuration class.

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import top.code2life.config.DynamicConfig;

import java.util.Map;

@Data
@DynamicConfig  // add annotation here !
@Configuration
@ConfigurationProperties(prefix = "my-prop")
public class TestConfigurationProperties {

    private String str;

    private Double doubleVal;

    private Map<String, Object> mapVal;
}

// file: application-another-profile.yml
// ============================
// my-prop:  # or myProp, relax binding supported 
//   str: someVal
//   double-val: 100.0
//   mapVal:
//     k: v

Step3. Run Application with Configuration Location

java -jar your-spring-boot-app.jar --spring.config.location=/path/to/config

Then, modifications on /path/to/config/application-.yml will take effect and reflect on @DynamicConfig beans immediately.

Best Practices

  • Configuration as Code, Everything as Code
  • Configurations should be maintained in Git, rather than any GUI system.
  • Configurations should be applied to dev/production environments by Continuous Integration system.
  • Git-Based DevOps workflow is the modern way of operating services, at scale.

Implementation

  1. Bean 'DynamicConfigPropertiesWatcher' will be initialized if 'spring.config.location' is specified
  2. Bean 'DynamicConfigBeanPostProcessor' will be initialized if 'DynamicConfigPropertiesWatcher' exists
  3. DynamicConfigBeanPostProcessor collects beans' metadata after initializing
  4. DynamicConfigPropertiesWatcher watches configuration directory, then replace PropertySource in Environment on changes
  5. DynamicConfigPropertiesWatcher publishes 'ConfigurationChangedEvent'
  6. DynamicConfigBeanPostProcessor listens 'ConfigurationChangedEvent', calculate diff
  7. For each changed key, DynamicConfigBeanPostProcessor will use preserved bean metadata to check if it's related
  8. After filtering related beans, it will use reflect API or ConfigurationPropertiesBindingPostProcessor API to modify fields of existing bean

Compatibility

Any SpringBoot/SpringCloud application within following SpringBoot version can use this lib.

  • √ SpringBoot 2.5.x and Above
  • √ SpringBoot 2.4.x
  • √ SpringBoot 2.3.x
  • √ SpringBoot 2.2.x
  • √ SpringBoot 2.1.x
  • √ SpringBoot 2.0.x
  • X SpringBoot 1.5.x and Lower

NOTES:

  • For SpringBoot 2.0.x, use Junit4 rather than Junit5.
  • This lib does not depend on ANY other libs except SpringBoot core libs.

License

Spring Boot Dynamic Config is Open Source software released under the Apache 2.0 license.

You might also like...

ReDoSHunter: A Combined Static and Dynamic Approach for Regular Expression DoS Detection

ReDoSHunter ReDoSHunter is a combined static and dynamic approach for regular expression DoS detection. LATEST NOTE (updated at 2021.09.13): ReDoSHunt

Dec 23, 2022

Generate a dynamic PAC script that will route traffic to your Burp proxy only if it matches the scope defined in your Burp target.

Generate a dynamic PAC script that will route traffic to your Burp proxy only if it matches the scope defined in your Burp target.

Burp PAC Server This Burp Extension generates a dynamic Proxy Auto-Configuration (PAC) script that will route traffic to your Burp proxy only if it ma

Jun 13, 2022

A quiz app with great layout design, dynamic questions using firebase and what not....

A quiz app with great layout design, dynamic questions using firebase and what not....

AndroidQuizApp An android quiz app created using Android Studio with our language JAVA that has great layout design, dynamic questions using firebase

Dec 30, 2022

This is the VapeCloud project, it is a Minecraft Dynamic CloudSystem based on Nio-Networking.

This is the VapeCloud project, it is a Minecraft Dynamic CloudSystem based on Nio-Networking.

This is the VapeCloud project, it is a Minecraft Dynamic CloudSystem based on Nio-Networking. IMPORTENT: this Cloudsystem is still in Development Requ

Dec 19, 2022

Dynamic detection of likely invariants

This is the distribution of the Daikon invariant detector, Daikon version 5.8.11, released November 2, 2021. If you are working with a Daikon distrib

Dec 28, 2022

A light-weight and dynamic dependency injection framework

⚠️ This project is now part of the EE4J initiative. This repository has been archived as all activities are now happening in the corresponding Eclipse

Dec 23, 2022

Dynamic Reports using Jasper Reports

DynamicJasper DynamicJasper (DJ) is an API that hides the complexity of JasperReports, it helps developers to save time when designing simple/medium c

Dec 28, 2022

A Fully Code Integrated Dynamic DataBase Management System for the Java Platform

dynamic-database A fully code integrated minimal database management system for Java, Scala, Kotlin or Groovy projects. It is written in Java and can

Jun 8, 2022

The KubeJS data dumper and dynamic typing generator.

The KubeJS data dumper and dynamic typing generator.

ProbeJS A data dumper and typing generator for the KubeJS functions, constants and classes. Great thanks to @DAmNRelentless, @LatvianModder and @yeste

Dec 8, 2022
Releases(v1.0.8)
Owner
Joey Yang
Amateur bug maker
Joey Yang
Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.

English | 中文 Apollo - A reliable configuration management system Apollo is a reliable configuration management system. It can centrally manage the con

Apollo 27.6k Jan 5, 2023
dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务

一个基于springboot的快速集成多数据源的启动器 简介 dynamic-datasource-spring-boot-starter 是一个基于springboot的快速集成多数据源的启动器。 其支持 Jdk 1.7+, SpringBoot 1.4.x 1.5.x 2.x.x。 文档 | D

baomidou 3.8k Dec 31, 2022
SpringBoot show case application for reactive-pulsar library (Reactive Streams adapter for Apache Pulsar Java Client)

Reactive Pulsar Client show case application Prerequisites Cloning reactive-pulsar Running this application requires cloning https://github.com/lhotar

Lari Hotari 9 Nov 10, 2022
Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Hayden Hanson 0 May 21, 2022
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)

OpenAPI Generator Master (5.4.x): 6.0.x (6.0.x): ⭐ ⭐ ⭐ If you would like to contribute, please refer to guidelines and a list of open tasks. ⭐ ⭐ ⭐ ‼️

OpenAPI Tools 14.8k Dec 30, 2022
Ethylene is a open-source, lightweight, general-purpose compatibility layer standing between the developer and the chaotic world of configuration file formats.

Ethylene Ethylene is a open-source, lightweight, general-purpose compatibility layer standing between the developer and the chaotic world of configura

Steank 7 Aug 9, 2022
Super Configuration Management System (SCMS)

Super Configuration Management System (SCMS) Intro Este es un servicio web REST que permite administrar la configuración de distintas aplicaciones en

Gastón Pini 1 Feb 5, 2022
A library that automatically generates and manages configuration files based on classes.

sc-cfg SC-CFG is a simple, yet powerful library that automatically generate configuration files based on your classes. Compatible with Java 8+ and Kot

null 10 Nov 28, 2022
YetAnotherConfigLib (yacl) is just that. A builder-based configuration library for Minecraft.

YetAnotherConfigLib Yet Another Config Lib, like, what were you expecting? Why does this mod even exist? This mod was made to fill a hole in this area

Xander 36 Dec 29, 2022
🔥 强大的动态线程池,并附带监控报警功能(没有依赖中间件),完全遵循阿里巴巴编码规范。Powerful dynamic thread pool, does not rely on any middleware, with monitoring and alarm function.

?? 动态线程池(DTP)系统,包含 Server 端及 SpringBoot Client 端需引入的 Starter. 这个项目做什么? 动态线程池(Dynamic-ThreadPool),下面简称 DTP 系统 美团线程池文章 介绍中,因为业务对线程池参数没有合理配置,触发过几起生产事故,进而

longtai 3.4k Dec 30, 2022