@ConfigurationProperties 读取Apollo 修改配置不生效

今天做一个项目的时候用到了@ConfigurationProperties注解用来初始化一个Map形式的配置,配置格式如下(实际代码和配置有出入,仅作参考),主要作用是根据不同的ip进行不同的配置

file.pathMap[127.0.0.1] = [{"path": "/videofile1"}]
file.pathMap[127.0.0.2] = [{"path": "/videofile2"}]
file.pathMap[127.0.0.3] = [{"path": "/videofile3"}]

在代码中使用@ConfigurationProperties进行读取,代码如下

@Configuration
@ConfigurationProperties(prefix = "file")
public class FilePathConfigurationProperties {
    private final YLogger logger = YLogger.getLogger(this.getClass());
    //ip与配置的关系<ip,"path1,path2">
    private Map<String, String> pathMap;

    public Map<String, String> getPathMap() {
        return pathMap;
    }

    public void setPathMap(Map<String, String> pathMap) {
        this.pathMap = pathMap;
    }
}

由于项目使用了Apollo,于是修改Apollo配置的时候想当然的认为会动态加载,但是实际测试时发现并不会自动更新,查阅资料后发现@ConfigurationProperties如果需要在Apollo配置变化时自动更新注入的值,需要配合使用EnvironmentChangeEvent或RefreshScope,有人给出的解决代码如下

 
import com.ctrip.framework.apollo.model.ConfigChange;
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.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApolloConfigChanged implements ApplicationContextAware {
    private ApplicationContext applicationContext;
 
    @ApolloConfigChangeListener
    private void someChangeHandler(ConfigChangeEvent changeEvent) {
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Found change - {}", change.toString());
        }
 
        // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
    }
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

代码来自:https://blog.csdn.net/My_Way666/article/details/121952729

测试后我发现,虽然@ApolloConfigChangeListener监听事件可以正常触发,但是这位兄弟给出的解决代码中包含了org.springframework.cloud.context.environment.EnvironmentChangeEvent这样的一个cloud的事件类,但我的项目并没有应用cloud怎么办呢?我转念一想,既然这个@ApolloConfigChangeListener这个监听事件是可用的,也可以拿到修改前后的信息,那么我完全可以把监听到的修改信息自己写逻辑同步到我的配置类里呀,并不一定需要他的事件驱动,于是,有了以下代码

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.sinolink.framework.ylogger.YLogger;
import com.sinolink.local2s3job.entity.ScanFileConfiguration;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "file")
public class FilePathConfigurationProperties {
    private final YLogger logger = YLogger.getLogger(this.getClass());
    //ip与扫描文件夹的关系<ip,"path1,path2">
    private Map<String, String> pathMap;

    public Map<String, String> getPathMap() {
        return pathMap;
    }

    public void setPathMap(Map<String, String> pathMap) {
        this.pathMap = pathMap;
    }

    public void put(String key, String path) {
        this.pathMap.put(key, path);
    }

    public void remove(String key) {
        this.pathMap.remove(key);
    }
    
    /**
     * Apollo配置修改触发事件,主要是针对Apollo配置修改时更新ConfigurationProperties的值
     * @param configChangeEvent Apollo配置修改触发事件
     */
    @ApolloConfigChangeListener
    private void someChangeHandler(ConfigChangeEvent configChangeEvent) {
        try {
            for (String key : configChangeEvent.changedKeys()) {
                ConfigChange configChange = configChangeEvent.getChange(key);
                logger.info("found change - {}", configChange.toString());
                //如果改的是配置类的参数,则手动修改配置
                if (key.startsWith("file")) {
                    String ip = key.substring(key.indexOf('[') + 1, key.indexOf(']'));
                    //根据变更类型进行添加或删除操作
                    switch (configChange.getChangeType()) {
                        case ADDED:
                        case MODIFIED:
                            this.put(ip, configChange.getNewValue());
                            break;
                        case DELETED:
                            this.remove(ip);
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("刷新Apollo扫描路径配置时异常", ex);
        }
    }
}

到此,问题完美解决,测试以后发现修改Apollo配置也可以同步修改@ConfigurationProperties里的Map了

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值