栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

配置中心@ConfigurationProperties+@RefreshScope 动态配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

配置中心@ConfigurationProperties+@RefreshScope 动态配置

文章目录
  • 前言
  • 实现方案
  • 原理

前言

在之前写了一篇

  • 配置中心Component + @Value动态配置实现

本质就是缓存实例以及field,还有@Value的key,然后有变动的时候去刷新field

开发中会遇到@ConfigurationProperties去拿配置,如果按照上一篇去刷新,有点困难的,因为类里面的属性并不是每个都是拿配置的。

实现方案

@ConfigurationProperties+@RefreshScope 动态配置

  1. 写动态配置类
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
@Component
public class TestConfig {

    @Resource
    public TestCinfig2 cinfig2;

}
  1. application.yml配置项
com:
  a: test
@Data
@Component
@ConfigurationProperties(prefix = "com")
public class TestCinfig2 {

    private String a;

}
  1. 刷新spring environment
@Autowired
    private ConfigurableEnvironment environment;

    public void a(){
       
        Iterator> iterator = environment.getPropertySources().iterator();
        while (iterator.hasNext()){
            PropertySource propertySources = iterator.next();
            System.out.println(propertySources.getName()+" "+propertySources.getSource());
        }
        environment.getPropertySources().remove("com.a");
        environment.getPropertySources().addFirst((new PropertySource("com.a", "refresh") {
            // 重点
            @Override
            public Object getProperty(String s) {
                if (s.equals("com.a")) {//
                    return source;// 返回构造方法中的 source : refresh
                }
                return null;
            }
        }));
        System.out.println("------------------------------------------");
        Iterator> iterator1 = environment.getPropertySources().iterator();
        while (iterator1.hasNext()){
            PropertySource propertySources = iterator1.next();
            System.out.println(propertySources.getName()+" "+propertySources.getSource());
        }
    }

重点:在ops推送的时候将key “com.a” ,value塞到“refresh”这一块

environment.getPropertySources().remove("com.a");
environment.getPropertySources().addFirst((new PropertySource("com.a", "refresh") {
            // 重点
            @Override
            public Object getProperty(String s) {
                if (s.equals("com.a")) {//
                    return source;// 返回构造方法中的 source : refresh
                }
                return null;
            }
        }));
  1. 运行程序
public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ConfigServer.class, args);
        //System.out.println(context.getBean(TestConfig.class).port);
        System.out.println(context.getBean(TestConfig.class).cinfig2.getA());
        context.getBean(TestConfig.class).a();
        context.getBean(ContextRefresher.class).refresh();
        //System.out.println(context.getBean(TestConfig.class).port);
        System.out.println(context.getBean(TestConfig.class).cinfig2.getA());
    }
原理

ConfigurableEnvironment去操作spring配置,然后通过@RefreshScope 在getBean的时候去刷新代理类,重新拿到值,促发机制ContextRefresher.refresh()或者Environment change Listener

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/488971.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号