Spring Cloud Consul dynamically updates the configuration in real time

project address

Github: https://github.com/fomeiherz/spring-cloud-consul-example

Code analysis

/**
 * 注解@RefreshScope必须的,实时更新配置
 */
@RefreshScope
@Configuration
public class KVConfig {
    
    

    // Consul控制台配置参数:config/spring-cloud-consul-example/timeout
    // 只能通过Getter方法获取
    @Value("${timeout:3000}")
    private Integer timeout;


    // 必须为变量声明Getter和Setter方法,否则无法实时更新变量值
    // 只能通过该方法获取配置变量
    public Integer getTimeout() {
    
    
        return timeout;
    }
    public void setTimeout(Integer timeout) {
    
    
        this.timeout = timeout;
    }
}

Consul configuration center configuration

Insert picture description here
When modifying the configuration, after waiting for 1 second, the new configuration value will be viewed when the configuration is requested.

Guess you like

Origin blog.csdn.net/fomeiherz/article/details/103525020