Spring Cloud Consul实时动态更新配置

项目地址

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

代码剖析

/**
 * 注解@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配置中心配置

在这里插入图片描述
修改配置时,等待1s后,请求配置时会查看到新的配置值。

猜你喜欢

转载自blog.csdn.net/fomeiherz/article/details/103525020