springrefreshspring-cloudendpointsconfigurationproperties

spring cloud refresh ConfigurationProperties


I want to rebinding ConfigurationProperties data.Read user documentation. post http://localhost:8080/env,It working . But post http://localhost:8080/env/reset,Cannot refresh all configurations. Can only refresh keys that have visited /env. I want to refresh all the configuration what should I do?

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


Solution

  • Posting key-value pairs to /env will trigger rebinding. Posting to /env/reset will trigger too on condition that manager propertysource not empty.

    If you are not updating environment by posting /env, you can use endpoint /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)));
        }
    
    }