Config Client 自动刷新

  在一些特殊的服务上,可能不需要服务端推送刷新,而是客户端本身需要每隔一段时间就去刷新一下最新的配置。

  在 Config Client 端通过配合 Actuator 访问 /refresh 接口可以进行刷新配置,最终是调用了 RefreshEndopint.refresh(); 方法。所以这里的实现原理就是通过定时任务,直接调用 refresh() 方法进行定时刷新配置。

新增自动刷新配置配置类

/**
 * @Author:大漠知秋
 * @Description:自动去刷新配置中心的配置
 * @CreateDate:10:31 AM 2018/11/2
 */
@Slf4j
@ConditionalOnClass(RefreshEndpoint.class)
@ConditionalOnProperty(
        name = "spring.cloud.config.auto-refresh-config.enabled",
        havingValue = "true"
)
@AutoConfigureAfter(RefreshAutoConfiguration.class)
@ConfigurationProperties(prefix = "spring.cloud.config.auto-refresh-config")
@Configuration
public class AutoRefreshConfigServerConfiguration implements SchedulingConfigurer {

    /** 间隔刷新时间 */
    private long refreshInterval;

    public long getRefreshInterval() {
        return refreshInterval;
    }

    public void setRefreshInterval(long refreshInterval) {
        this.refreshInterval = refreshInterval;
    }

    /** 刷新的端点 */
    @Resource
    private RefreshEndpoint refreshEndpoint;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        final long interval = getRefreshIntervalInMilliseconds();

        log.info(String.format("Scheduling config refresh task with %s second delay", refreshInterval));
        scheduledTaskRegistrar.addFixedDelayTask(new IntervalTask(
                () -> {
                    refreshEndpoint.refresh();
                }, interval, interval
        ));
    }

    /** 以毫秒为单位返回刷新间隔。 */
    private long getRefreshIntervalInMilliseconds() {
        return refreshInterval;
    }

    /** 如果没有在上下文中注册,则启用调度程序。 */
    @ConditionalOnMissingBean(ScheduledAnnotationBeanPostProcessor.class)
    @EnableScheduling
    @Configuration
    protected static class EnableSchedulingConfigProperties {

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值