├── dynamic-config-spring-boot-starter ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring.factories │ │ └── java │ │ └── com │ │ └── purgeteam │ │ └── dynamic │ │ └── config │ │ └── starter │ │ ├── DynamicConfigConfiguration.java │ │ ├── annotation │ │ └── EnableDynamicConfigEvent.java │ │ ├── DynamicConfigListenerConfiguration.java │ │ ├── event │ │ ├── ActionConfigEvent.java │ │ └── DynamicConfigApplicationListener.java │ │ └── util │ │ └── PropertyUtil.java └── pom.xml ├── nacos-spring-boot-starter ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── spring.factories │ │ └── java │ │ └── com │ │ └── purgeteam │ │ └── nacos │ │ └── starter │ │ ├── NacosConfiguration.java │ │ └── env │ │ └── NacosDefaultPropertySourceEnvironmentPostProcessor.java └── pom.xml ├── dynamic-config-demo ├── dynamic-config-starter-demo │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.properties │ │ │ └── java │ │ │ └── com │ │ │ └── purgeteam │ │ │ └── dynamic │ │ │ └── config │ │ │ └── demo │ │ │ ├── DynamicConfigSpringBootApplication.java │ │ │ └── NacosListener.java │ └── pom.xml ├── dynamic-config-nacos-starter-demo │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.properties │ │ │ └── java │ │ │ └── com │ │ │ └── purgeteam │ │ │ └── dynmic │ │ │ └── config │ │ │ └── nacos │ │ │ └── demo │ │ │ ├── NacosSpringBootApplication.java │ │ │ └── NacosListener.java │ └── pom.xml └── pom.xml ├── .gitignore ├── README.md ├── pom.xml └── LICENSE /dynamic-config-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Bootstrap components 2 | org.springframework.cloud.bootstrap.BootstrapConfiguration=\ 3 | com.purgeteam.dynamic.config.starter.DynamicConfigConfiguration -------------------------------------------------------------------------------- /nacos-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # EnvironmentPostProcessor 2 | org.springframework.boot.env.EnvironmentPostProcessor=\ 3 | com.purgeteam.nacos.starter.env.NacosDefaultPropertySourceEnvironmentPostProcessor -------------------------------------------------------------------------------- /nacos-spring-boot-starter/src/main/java/com/purgeteam/nacos/starter/NacosConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.nacos.starter; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | 5 | /** 6 | * @author purgeyao 7 | * @since 1.0 8 | */ 9 | @Configuration 10 | public class NacosConfiguration { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-starter-demo/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | spring.application.name=dynamic-config-starter-demo 3 | 4 | spring.cloud.nacos.config.server-addr=${NACOS_CONFIG_ADDR:10.1.1.99:8848,10.1.1.97:8848} 5 | spring.cloud.nacos.config.file-extension=properties 6 | #spring.cloud.nacos.config.namespace=${NACOS_CONFIG_NAMESPACE:6e36543a-b81c-4aca-b450-f8f7281c200e} -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-nacos-starter-demo/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | spring.application.name=dynamic-config-nacos-starter-demo 3 | 4 | spring.cloud.nacos.config.server-addr=${NACOS_CONFIG_ADDR:10.1.1.99:8848,10.1.1.97:8848} 5 | spring.cloud.nacos.config.file-extension=properties 6 | #spring.cloud.nacos.config.namespace=${NACOS_CONFIG_NAMESPACE:6e36543a-b81c-4aca-b450-f8f7281c200e} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/DynamicConfigConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter; 2 | 3 | import com.purgeteam.dynamic.config.starter.util.PropertyUtil; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @author purgeyao 9 | * @since 1.0 10 | */ 11 | @Configuration 12 | public class DynamicConfigConfiguration { 13 | 14 | @Bean 15 | public PropertyUtil propertyUtil() { 16 | return new PropertyUtil(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-nacos-starter-demo/src/main/java/com/purgeteam/dynmic/config/nacos/demo/NacosSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynmic.config.nacos.demo; 2 | 3 | import com.purgeteam.dynamic.config.starter.annotation.EnableDynamicConfigEvent; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @EnableDynamicConfigEvent 8 | @SpringBootApplication 9 | public class NacosSpringBootApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(NacosSpringBootApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-starter-demo/src/main/java/com/purgeteam/dynamic/config/demo/DynamicConfigSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.demo; 2 | 3 | import com.purgeteam.dynamic.config.starter.annotation.EnableDynamicConfigEvent; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @EnableDynamicConfigEvent 8 | @SpringBootApplication 9 | public class DynamicConfigSpringBootApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DynamicConfigSpringBootApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-starter-demo/src/main/java/com/purgeteam/dynamic/config/demo/NacosListener.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.demo; 2 | 3 | import com.purgeteam.dynamic.config.starter.event.ActionConfigEvent; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.context.ApplicationListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author purgeyao 10 | * @since 1.0 11 | */ 12 | @Slf4j 13 | @Component 14 | public class NacosListener implements ApplicationListener { 15 | 16 | @Override 17 | public void onApplicationEvent(ActionConfigEvent event) { 18 | log.info("接收事件"); 19 | log.info(event.getPropertyMap().toString()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-nacos-starter-demo/src/main/java/com/purgeteam/dynmic/config/nacos/demo/NacosListener.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynmic.config.nacos.demo; 2 | 3 | import com.purgeteam.dynamic.config.starter.event.ActionConfigEvent; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.context.ApplicationListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author purgeyao 10 | * @since 1.0 11 | */ 12 | @Slf4j 13 | @Component 14 | public class NacosListener implements ApplicationListener { 15 | 16 | @Override 17 | public void onApplicationEvent(ActionConfigEvent event) { 18 | log.info("接收事件"); 19 | log.info(event.getPropertyMap().toString()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /dynamic-config-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | dynamic-config-spring-boot 7 | com.purgeteam 8 | 0.1.1.RELEASE 9 | 10 | 4.0.0 11 | 12 | dynamic-config-demo 13 | pom 14 | 15 | dynamic-config-starter-demo 16 | dynamic-config-nacos-starter-demo 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/annotation/EnableDynamicConfigEvent.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter.annotation; 2 | 3 | import com.purgeteam.dynamic.config.starter.DynamicConfigListenerConfiguration; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | import org.springframework.context.annotation.Import; 11 | 12 | /** 13 | * {@link DynamicConfigListenerConfiguration} 开启配置变化监听器 14 | * 15 | * @author purgeyao 16 | * @since 1.0 17 | */ 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target(ElementType.TYPE) 20 | @Import(DynamicConfigListenerConfiguration.class) 21 | public @interface EnableDynamicConfigEvent { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/DynamicConfigListenerConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter; 2 | 3 | import com.purgeteam.dynamic.config.starter.event.DynamicConfigApplicationListener; 4 | import com.purgeteam.dynamic.config.starter.util.PropertyUtil; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.cloud.context.refresh.ContextRefresher; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | /** 12 | * @author purgeyao 13 | * @since 1.0 14 | */ 15 | @Configuration 16 | public class DynamicConfigListenerConfiguration { 17 | 18 | private static final Logger log = LoggerFactory.getLogger(DynamicConfigListenerConfiguration.class); 19 | 20 | @Bean 21 | public DynamicConfigApplicationListener actionApplicationListener(ContextRefresher contextRefresher, 22 | PropertyUtil propertyUtil) { 23 | log.info("[DynamicConfigApplicationListener] DynamicConfigApplicationListener listener on"); 24 | return new DynamicConfigApplicationListener(contextRefresher, propertyUtil); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-nacos-starter-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | dynamic-config-demo 7 | com.purgeteam 8 | 0.1.1.RELEASE 9 | 10 | 4.0.0 11 | 12 | dynamic-config-nacos-starter-demo 13 | 14 | 15 | 16 | 17 | com.purgeteam 18 | nacos-spring-boot-starter 19 | 0.1.1.RELEASE 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | 29 | org.projectlombok 30 | lombok 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/event/ActionConfigEvent.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter.event; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.springframework.context.ApplicationEvent; 7 | 8 | /** 9 | * @author purgeyao 10 | * @since 1.0 11 | */ 12 | public class ActionConfigEvent extends ApplicationEvent { 13 | 14 | private String eventDesc; 15 | 16 | private Map propertyMap; 17 | 18 | /** 19 | * Create a new ApplicationEvent. 20 | * 21 | * @param source the object on which the event initially occurred (never {@code null}) 22 | */ 23 | public ActionConfigEvent(Object source, String eventDesc, Map propertyMap) { 24 | super(source); 25 | this.eventDesc = eventDesc; 26 | this.propertyMap = propertyMap; 27 | } 28 | 29 | public String getEventDesc() { 30 | return eventDesc; 31 | } 32 | 33 | public void setEventDesc(String eventDesc) { 34 | this.eventDesc = eventDesc; 35 | } 36 | 37 | public Map getPropertyMap() { 38 | return propertyMap; 39 | } 40 | 41 | public void setPropertyMap(Map propertyMap) { 42 | this.propertyMap = propertyMap; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.purgeteam 8 | dynamic-config-spring-boot 9 | 0.1.1.RELEASE 10 | 11 | 12 | 4.0.0 13 | dynamic-config-spring-boot-starter 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-logging 25 | 26 | 27 | 28 | org.springframework 29 | spring-web 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-context 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /dynamic-config-demo/dynamic-config-starter-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | dynamic-config-demo 7 | com.purgeteam 8 | 0.1.1.RELEASE 9 | 10 | 4.0.0 11 | 12 | dynamic-config-starter-demo 13 | 14 | 15 | 16 | 17 | com.purgeteam 18 | dynamic-config-spring-boot-starter 19 | 0.1.1.RELEASE 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | com.alibaba.cloud 29 | spring-cloud-starter-alibaba-nacos-config 30 | 2.1.0.RELEASE 31 | 32 | 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /nacos-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | dynamic-config-spring-boot 7 | com.purgeteam 8 | 0.1.1.RELEASE 9 | 10 | 4.0.0 11 | 12 | nacos-spring-boot-starter 13 | 14 | 15 | 2.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | 21 | com.purgeteam 22 | dynamic-config-spring-boot-starter 23 | 0.1.1.RELEASE 24 | 25 | 26 | 27 | com.alibaba.cloud 28 | spring-cloud-starter-alibaba-nacos-config 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | com.alibaba.cloud 38 | spring-cloud-alibaba-dependencies 39 | ${spring-cloud-alibaba.version} 40 | pom 41 | import 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/event/DynamicConfigApplicationListener.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter.event; 2 | 3 | import com.purgeteam.dynamic.config.starter.util.PropertyUtil; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.Set; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.cloud.context.refresh.ContextRefresher; 12 | import org.springframework.cloud.endpoint.event.RefreshEvent; 13 | import org.springframework.context.ApplicationContext; 14 | import org.springframework.context.ApplicationContextAware; 15 | import org.springframework.context.ApplicationListener; 16 | import org.springframework.core.Ordered; 17 | import org.springframework.core.env.ConfigurableEnvironment; 18 | import org.springframework.core.env.MutablePropertySources; 19 | 20 | /** 21 | * @author purgeyao 22 | * @since 1.0 23 | */ 24 | public class DynamicConfigApplicationListener implements ApplicationListener, 25 | ApplicationContextAware, Ordered { 26 | 27 | private static final Logger log = LoggerFactory.getLogger(DynamicConfigApplicationListener.class); 28 | 29 | private ContextRefresher refresh; 30 | 31 | private ApplicationContext context; 32 | 33 | private PropertyUtil propertyUtil; 34 | 35 | public DynamicConfigApplicationListener(ContextRefresher contextRefresher) { 36 | this.refresh = contextRefresher; 37 | } 38 | 39 | public DynamicConfigApplicationListener(ContextRefresher contextRefresher, PropertyUtil propertyUtil) { 40 | this.refresh = contextRefresher; 41 | this.propertyUtil = propertyUtil; 42 | } 43 | 44 | @Override 45 | public void setApplicationContext(ApplicationContext applicationContext) { 46 | this.context = applicationContext; 47 | } 48 | 49 | @Override 50 | public void onApplicationEvent(RefreshEvent event) { 51 | ConfigurableEnvironment beforeEnv = (ConfigurableEnvironment) context.getEnvironment(); 52 | MutablePropertySources propertySources = beforeEnv.getPropertySources(); 53 | MutablePropertySources beforeSources = new MutablePropertySources(propertySources); 54 | // 刷新上下文 55 | Set refresh = this.refresh.refresh(); 56 | // 获取对比值发布事件 57 | Map contrast = propertyUtil.contrast(beforeSources, propertySources); 58 | context.publishEvent(new ActionConfigEvent(this, "Refresh config", contrast)); 59 | log.info("[ActionApplicationListener] The update is successful {}", refresh); 60 | } 61 | 62 | @Override 63 | public int getOrder() { 64 | return LOWEST_PRECEDENCE - 1; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /nacos-spring-boot-starter/src/main/java/com/purgeteam/nacos/starter/env/NacosDefaultPropertySourceEnvironmentPostProcessor.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.nacos.starter.env; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.env.EnvironmentPostProcessor; 5 | import org.springframework.core.Ordered; 6 | import org.springframework.core.env.*; 7 | import org.springframework.core.io.DefaultResourceLoader; 8 | import org.springframework.core.io.Resource; 9 | import org.springframework.core.io.ResourceLoader; 10 | import org.springframework.core.io.support.EncodedResource; 11 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 12 | import org.springframework.core.io.support.ResourcePatternResolver; 13 | import org.springframework.core.io.support.ResourcePropertySource; 14 | 15 | import java.io.IOException; 16 | 17 | import static org.springframework.core.io.support.ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX; 18 | 19 | /** 20 | * A lowest precedence {@link EnvironmentPostProcessor} implementation to append Core default {@link PropertySource} 21 | * with lowest order in {@link Environment} 22 | * 23 | * @author purgeyao 24 | * @since 1.0.0 25 | */ 26 | public class NacosDefaultPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, 27 | Ordered { 28 | 29 | private static final String PROPERTY_SOURCE_NAME = "nacos"; 30 | 31 | private static final String RESOURCE_LOCATION_PATTERN = CLASSPATH_ALL_URL_PREFIX + "META-INF/nacos/nacos-${spring.profiles.active}.properties"; 32 | 33 | private static final String FILE_ENCODING = "UTF-8"; 34 | 35 | @Override 36 | public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { 37 | 38 | ResourceLoader resourceLoader = getResourceLoader(application); 39 | 40 | processPropertySource(environment, resourceLoader); 41 | } 42 | 43 | private ResourceLoader getResourceLoader(SpringApplication application) { 44 | 45 | ResourceLoader resourceLoader = application.getResourceLoader(); 46 | 47 | if (resourceLoader == null) { 48 | resourceLoader = new DefaultResourceLoader(application.getClassLoader()); 49 | } 50 | 51 | return resourceLoader; 52 | } 53 | 54 | private void processPropertySource(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { 55 | try { 56 | PropertySource coreDefaultPropertySource = buildPropertySource(environment, resourceLoader); 57 | MutablePropertySources propertySources = environment.getPropertySources(); 58 | // append coreDefaultPropertySource as last one in order to be overrided by higher order 59 | propertySources.addLast(coreDefaultPropertySource); 60 | } catch (IOException e) { 61 | throw new IllegalStateException(e.getMessage(), e); 62 | } 63 | } 64 | 65 | private PropertySource buildPropertySource(ConfigurableEnvironment environment, ResourceLoader resourceLoader) throws IOException { 66 | CompositePropertySource propertySource = new CompositePropertySource(PROPERTY_SOURCE_NAME); 67 | appendPropertySource(environment, propertySource, resourceLoader); 68 | return propertySource; 69 | } 70 | 71 | private void appendPropertySource(ConfigurableEnvironment environment, CompositePropertySource propertySource, ResourceLoader resourceLoader) throws IOException { 72 | ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver( 73 | resourceLoader); 74 | // 占位符解析 75 | String resourceLocationPattern = environment.resolvePlaceholders(RESOURCE_LOCATION_PATTERN); 76 | Resource[] resources = resourcePatternResolver.getResources(resourceLocationPattern); 77 | for (Resource resource : resources) { 78 | // Add if exists 79 | if (resource.exists()) { 80 | String internalName = String.valueOf(resource.getURL()); 81 | propertySource.addPropertySource(new ResourcePropertySource(internalName, new EncodedResource(resource, FILE_ENCODING))); 82 | } 83 | } 84 | } 85 | 86 | @Override 87 | public int getOrder() { 88 | return HIGHEST_PRECEDENCE; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dynamic-config-spring-boot SpringCloud-动态配置变化监控-获取变化(支持Config、Nacos) 2 | 3 | [![Maven Central](https://img.shields.io/maven-central/v/com.purgeteam/dynamic-config-spring-boot-starter.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.purgeteam%20AND%20a:dynamic-config-spring-boot-starter) 4 | ![License](https://img.shields.io/badge/SpringBoot-2.1.8RELEASE-green.svg) 5 | ![License](https://img.shields.io/badge/JAVA-1.8+-green.svg) 6 | ![License](https://img.shields.io/badge/maven-3.0+-green.svg) 7 | [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 8 | 9 | 🔥🔥🔥相关文档请访问 [PurgeTeam docs](http://www.purgeteam.com)🔥🔥🔥 10 | 11 | ## 简介 12 | 13 | 配置中心有原生的 `SpringCloud Config` 、国内开源的 阿里 `Nacos`、携程 `Apollo` 等。 14 | 都是配置热加载的基础上增加了其他的功能。 15 | 16 | 配置中心具有配置热加载,修改配置 -> 推送到程序 -> 执行配置更新。 17 | 18 | 而获取配置更新的具体内容,这些组件都没有做到(`SpringCloud Config`、`Nacos`, `Apollo待验证` )。如:获取变化的配置 `test` 更新前为 `123` ,更新后为 `111`。 19 | 20 | `SpringCloud Config` 、`Nacos` 、 `Apollo待验证` 配置更新都是将整个配置文件推送给服务进行配置对比结果进行更新。这个阶段用户无法从程序中获取更新的内容。 21 | 22 | 为了实现这个扩展功能点,更新结果可以让程序感知。 23 | 在这个阶段用户通过code可以实现配置监听, 监听到某个配置变化做其他操作等。 24 | 25 | 示例: 获取到推送配置变化, 感知到 `test` 发生变化, 做更新同步数据操作等等。 26 | 27 | **感知结果集如:** 28 | 29 | ``` 30 | { 31 | `被更新的配置key`:{ 32 | before: `原来的值`, 33 | after: `更新后的值` 34 | }, 35 | `被更新的配置key`:{ 36 | before: `原来的值`, 37 | after: `更新后的值` 38 | } 39 | } 40 | ``` 41 | 42 | ## 功能使用 43 | 44 | ### 添加依赖 45 | 46 | **ps:** 实际version版本请使用最新版 47 | **最新版本:** [![Maven Central](https://img.shields.io/maven-central/v/com.purgeteam/dynamic-config-spring-boot-starter.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.purgeteam%20AND%20a:dynamic-config-spring-boot-starter) 48 | 49 | ``` 50 | 51 | com.purgeteam 52 | dynamic-config-spring-boot-starter 53 | 0.1.0.RELEASE 54 | 55 | ``` 56 | 57 | 当然实际情况要结合添加动态配置依赖包 `SpringCloud Config` 、 `Nacos` 、 `Apollo` 。 58 | 59 | 60 | `dynamic-config-spring-boot-starter` 模块不包含以上依赖, 需要自行添加。 61 | 62 | ### @EnableDynamicConfigEvent 63 | 64 | **简介:** 开启这个特性注解,具备配置推送更新监听能力。 65 | 66 | 67 | 启动类添加 `@EnableDynamicConfigEvent` 注解开启配置变化监听功能。 68 | ``` 69 | @EnableDynamicConfigEvent 70 | @SpringBootApplication 71 | public class DynamicConfigSpringBootApplication { 72 | 73 | public static void main(String[] args) { 74 | SpringApplication.run(DynamicConfigSpringBootApplication.class, args); 75 | } 76 | 77 | } 78 | ``` 79 | 80 | ### 编写事件接收器 81 | 82 | 创建 `NacosListener`(名称随意) 实现 `ApplicationListener#onApplicationEvent` 方法 83 | 84 | ``` 85 | @Slf4j 86 | @Component 87 | public class NacosListener implements ApplicationListener { 88 | 89 | @Override 90 | public void onApplicationEvent(ActionConfigEvent event) { 91 | log.info("接收事件"); 92 | log.info(event.getPropertyMap().toString()); 93 | } 94 | } 95 | ``` 96 | 97 | 在 `NacosListener#onApplicationEvent` 方法里获取目标值, 作相应的逻辑处理。 98 | 99 | **ActionConfigEvent event:** 100 | ``` 101 | public class ActionConfigEvent extends ApplicationEvent { 102 | 103 | // 事件说明 104 | private String eventDesc; 105 | 106 | // 更新变化结果集 107 | private Map propertyMap; 108 | ... 109 | } 110 | ``` 111 | 112 | `ActionConfigEvent` 主要包含 `Map propertyMap;`, 从这里可以获取更新变化结果, propertyMap结构如下: 113 | 114 | ``` 115 | { 116 | `被更新的配置key`:{ 117 | before: `原来的值`, 118 | after: `更新后的值` 119 | }, 120 | `被更新的配置key`:{ 121 | before: `原来的值`, 122 | after: `更新后的值` 123 | } 124 | } 125 | ``` 126 | 127 | ### 更新配置演示 128 | 129 | **ps:** 示例为 `Nacos` 其它配置中心无差别。 130 | 131 | 原始配置: 132 | 133 | ``` 134 | test.age=18 135 | user.name=purgeyao 136 | ``` 137 | 138 | 修改配置: 139 | 140 | ``` 141 | # test.age 将18更新为19 142 | test.age=19 143 | user.name=purgeyao 144 | ``` 145 | 146 | 在 `NacosListener#onApplicationEvent` 方法加入端点调试观察 `ActionConfigEvent` 对象参数。 147 | 148 | ![img](https://raw.githubusercontent.com/purgeyao/purgeyao.github.io/master/img/blog/2019-10-17/config1.png) 149 | 150 | 更新的 `test.age` 已经被记录在了 `ActionConfigEvent.propertyMap` 里了, 从 18 更新为 19。 151 | 152 | 控制台打印: 153 | 154 | ``` 155 | 2019-10-17 10:44:09.221 INFO 54054 --- [-10.1.1.97_8848] c.p.dynamic.config.demo.NacosListener : 接收事件 156 | 2019-10-17 10:45:19.752 INFO 54054 --- [-10.1.1.97_8848] c.p.dynamic.config.demo.NacosListener : {test.age={before=18, after=19}} 157 | ``` 158 | -------------------------------------------------------------------------------- /dynamic-config-spring-boot-starter/src/main/java/com/purgeteam/dynamic/config/starter/util/PropertyUtil.java: -------------------------------------------------------------------------------- 1 | package com.purgeteam.dynamic.config.starter.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Set; 10 | 11 | import org.springframework.core.env.CompositePropertySource; 12 | import org.springframework.core.env.EnumerablePropertySource; 13 | import org.springframework.core.env.MutablePropertySources; 14 | import org.springframework.core.env.PropertySource; 15 | import org.springframework.core.env.StandardEnvironment; 16 | import org.springframework.web.context.support.StandardServletEnvironment; 17 | 18 | /** 19 | * @author purgeyao 20 | * @since 1.0 21 | */ 22 | public class PropertyUtil { 23 | 24 | private static final String BEFORE = "before"; 25 | 26 | private static final String AFTER = "after"; 27 | 28 | private Set standardSources = new HashSet<>( 29 | Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, 30 | StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, 31 | StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME, 32 | StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, 33 | StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, 34 | "configurationProperties")); 35 | 36 | public Map contrast(MutablePropertySources beforeSources, 37 | MutablePropertySources afterSources) { 38 | Map before = extract(beforeSources); 39 | Map propertyMap = changesAll(before, extract(afterSources)); 40 | return propertyMap; 41 | } 42 | 43 | private Map extract(MutablePropertySources propertySources) { 44 | Map result = new HashMap(); 45 | List> sources = new ArrayList>(); 46 | for (PropertySource source : propertySources) { 47 | sources.add(0, source); 48 | } 49 | for (PropertySource source : sources) { 50 | if (!this.standardSources.contains(source.getName())) { 51 | extract(source, result); 52 | } 53 | } 54 | return result; 55 | } 56 | 57 | private void extract(PropertySource parent, Map result) { 58 | if (parent instanceof CompositePropertySource) { 59 | try { 60 | List> sources = new ArrayList>(); 61 | for (PropertySource source : ((CompositePropertySource) parent) 62 | .getPropertySources()) { 63 | sources.add(0, source); 64 | } 65 | for (PropertySource source : sources) { 66 | extract(source, result); 67 | } 68 | } catch (Exception e) { 69 | return; 70 | } 71 | } else if (parent instanceof EnumerablePropertySource) { 72 | for (String key : ((EnumerablePropertySource) parent).getPropertyNames()) { 73 | result.put(key, parent.getProperty(key)); 74 | } 75 | } 76 | } 77 | 78 | private Map changesAll(Map before, 79 | Map after) { 80 | 81 | HashMap result = new HashMap<>(16); 82 | for (String key : before.keySet()) { 83 | HashMap valueMap = new HashMap<>(16); 84 | valueMap.put(BEFORE, String.valueOf(before.get(key))); 85 | // 判断是否有新的 key 加入 86 | if (!after.containsKey(key)) { 87 | valueMap.put(AFTER, null); 88 | result.put(key, valueMap); 89 | // 判断是否有同样 key 数据变更 90 | } else if (!equal(before.get(key), after.get(key))) { 91 | valueMap.put(AFTER, String.valueOf(after.get(key))); 92 | result.put(key, valueMap); 93 | } 94 | } 95 | for (String key : after.keySet()) { 96 | if (!before.containsKey(key)) { 97 | HashMap valueMap = new HashMap<>(16); 98 | valueMap.put(BEFORE, null); 99 | valueMap.put(AFTER, String.valueOf(after.get(key))); 100 | result.put(key, valueMap); 101 | } 102 | } 103 | return result; 104 | } 105 | 106 | private Map changes(Map before, 107 | Map after) { 108 | Map result = new HashMap(); 109 | for (String key : before.keySet()) { 110 | if (!after.containsKey(key)) { 111 | result.put(key, null); 112 | } else if (!equal(before.get(key), after.get(key))) { 113 | result.put(key, after.get(key)); 114 | } 115 | } 116 | for (String key : after.keySet()) { 117 | if (!before.containsKey(key)) { 118 | result.put(key, after.get(key)); 119 | } 120 | } 121 | return result; 122 | } 123 | 124 | private boolean equal(Object one, Object two) { 125 | if (one == null && two == null) { 126 | return true; 127 | } 128 | if (one == null || two == null) { 129 | return false; 130 | } 131 | return one.equals(two); 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.purgeteam 7 | dynamic-config-spring-boot 8 | 0.1.1.RELEASE 9 | 10 | 4.0.0 11 | pom 12 | 13 | dynamic-config-spring-boot 14 | dynamic-config-spring-boot 15 | https://github.com/purgeteam/dynamic-config-spring-boot 16 | 17 | 18 | dynamic-config-demo 19 | nacos-spring-boot-starter 20 | dynamic-config-spring-boot-starter 21 | 22 | 23 | 24 | 25 | 26 | The Apache License, Version 2.0 27 | http://www.apache.org/licenses/LICENSE-2.0.txt 28 | repo 29 | A business-friendly OSS license 30 | 31 | 32 | 33 | 34 | 35 | 36 | purgeyao 37 | 16621377702@163.com 38 | 39 | owner 40 | 41 | +8 42 | 43 | 44 | 45 | 46 | 47 | scm:git:git://github.com/purgeteam/dynamic-config-spring-boot.git 48 | scm:git:ssh://github.com/purgeteam/dynamic-config-spring-boot.git 49 | https://github.com/purgeteam/dynamic-config-spring-boot 50 | 51 | 52 | 53 | 54 | 55 | ossrh 56 | https://oss.sonatype.org/content/repositories/snapshots/ 57 | 58 | 59 | ossrh 60 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 61 | 62 | 63 | 64 | 65 | 66 | 2.1.8.RELEASE 67 | 2.1.2.RELEASE 68 | 69 | 70 | 1.8 71 | ${java.version} 72 | ${java.version} 73 | 74 | 75 | 76 | 77 | 78 | org.springframework.boot 79 | spring-boot-configuration-processor 80 | provided 81 | true 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.springframework.boot 91 | spring-boot-dependencies 92 | ${spring-boot.version} 93 | pom 94 | import 95 | 96 | 97 | 98 | 99 | org.springframework.cloud 100 | spring-cloud-commons-dependencies 101 | ${spring-cloud-commons.version} 102 | pom 103 | import 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | org.apache.maven.plugins 114 | maven-source-plugin 115 | 3.0.1 116 | 117 | 118 | attach-sources 119 | verify 120 | 121 | jar-no-fork 122 | 123 | 124 | 125 | 126 | 127 | 128 | org.apache.maven.plugins 129 | maven-javadoc-plugin 130 | 2.10.3 131 | 132 | 133 | attach-javadocs 134 | 135 | jar 136 | 137 | 138 | -Xdoclint:none 139 | 140 | 141 | 142 | 143 | 144 | 145 | org.apache.maven.plugins 146 | maven-gpg-plugin 147 | 1.5 148 | 149 | 150 | sign-artifacts 151 | verify 152 | 153 | sign 154 | 155 | 156 | 157 | 158 | 159 | 160 | org.sonatype.plugins 161 | nexus-staging-maven-plugin 162 | 1.6.7 163 | true 164 | 165 | ossrh 166 | https://oss.sonatype.org/ 167 | true 168 | 169 | 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------