【问题标题】:spring cloud refresh ConfigurationProperties春天云刷新配置属性
【发布时间】:2018-11-29 03:49:45
【问题描述】:

我想重新绑定ConfigurationProperties 数据。阅读用户文档。 post http://localhost:8080/env,它工作。 但是post http://localhost:8080/env/reset,无法刷新所有配置。 只能刷新访问过/env 的键。 我想刷新所有配置怎么办?

http://projects.spring.io/spring-cloud/spring-cloud.html#_endpoints

【问题讨论】:

    标签: spring refresh spring-cloud endpoints configurationproperties


    【解决方案1】:

    将键值对发布到/env 将触发rebinding。 发布到/env/reset 也会在manager propertysource 不为空的情况下触发。

    如果你不是通过发布/env来更新环境,你可以使用端点/refresh

    @ManagedOperation
    public Map<String, Object> reset() {
        Map<String, Object> result = new LinkedHashMap<String, Object>(map);
        if (!map.isEmpty()) {
            map.clear();
            publish(new EnvironmentChangeEvent(publisher, result.keySet()));
        }
        return result;
    }
    
    @ManagedOperation
    public void setProperty(String name, String value) {
    
        if (!environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {
            synchronized (map) {
                if (!environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {
                    MapPropertySource source = new MapPropertySource(
                            MANAGER_PROPERTY_SOURCE, map);
                    environment.getPropertySources().addFirst(source);
                }
            }
        }
    
        if (!value.equals(environment.getProperty(name))) {
            map.put(name, value);
            publish(new EnvironmentChangeEvent(publisher, Collections.singleton(name)));
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2018-08-16
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多