springboot监听apollo改变
引入springcloudpom.xml<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId
·
引入springcloud
-
pom.xml
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <!--<artifactId>spring-cloud-starter-consul-discovery</artifactId>--> </dependency> ... </dependencies> -
config/TasksConfig
package ... import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; @Data @Component @ConfigurationProperties(prefix = "tasks") @Slf4j public class TasksConfig { List<Item> items; @Data public static class Item { } } -
aspect/ApolloAutoRefresher.java
package ... import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component @Slf4j public class ApolloAutoRefresher implements ApplicationContextAware { private ApplicationContext applicationContext; // value 必须为常量 @ApolloConfigChangeListener(value = {"redis.yml"}) public void onChange(ConfigChangeEvent changeEvent) { refreshTaskScheduleProperties(changeEvent); } private void refreshTaskScheduleProperties(ConfigChangeEvent changeEvent) { this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); log.info("自动刷新apollo配置 namespace: {}", listenNamespace); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }手动配置 apolloConfigListener,添加配置改动监听
package ... import com.ctrip.framework.apollo.ConfigChangeListener; import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component @Slf4j public class ApolloAutoRefresher implements ApplicationContextAware { @Value("${apollo.bootstrap.namespaces:application}") private String listenNamespace; private ApplicationContext applicationContext; // value 必须为常量 // @ApolloConfigChangeListener(value = {"redis.yml"}) public void onChange(ConfigChangeEvent changeEvent) { log.info("apollo配置发生变化 namespace: {}", changeEvent.getNamespace()); // if (listenNamespace.contains(changeEvent.getNamespace())) { refreshTaskScheduleProperties(changeEvent); // } } @PostConstruct public void init() { // 手动配置 apolloConfigListener,添加配置改动监听 ConfigChangeListener configChangeListener = this::onChange; // 也可@ApolloConfig注入 for (String namespace : listenNamespace.split(",")) { ConfigService.getConfig(namespace) .addChangeListener(configChangeListener); } } private void refreshTaskScheduleProperties(ConfigChangeEvent changeEvent) { this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); log.info("自动刷新apollo配置 namespace: {}", changeEvent.getNamespace()); // for (String key : changeEvent.changedKeys()) { // // TODO eg. logging.level.root // } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)