当前位置: 首页 > 工具软件 > dynamic-tp > 使用案例 >

动态线程池dynamic-tp接入Consul配置中心

贲功
2023-12-01

前言

    自从笔者给dynamic-tp接入了Zookeeper配置中心,就想着再扩展其他的配置中心,恰好笔者近期也在调研Consul配置中心,所以就想着将Consul配置中心接入到dynamic-tp

dynamic-tp快速接入:

dynamic-tp官网:

接入Consul配置中心具体实现

    Consul配置中心是通过定时任务做的配置变更,为了屏蔽底层实现,这里我选择对SpringBoot程序和SpringCloud应用进行接入,使用的包是spring-cloud-starter-consul-config,对于SpringBoot程序来说,集成起来相当容易,因为spring-cloud-starter-consul-config刷新配置的原理是刷新Spring容器中的配置,并且Spring提供了原生的监听接口SmartApplicationListener,实现该接口并监听RefreshScopeRefreshedEvent事件,刷新完毕后Spring发布该事件,我们就可以通过新的配置刷新线程池配置了。

实现代码

    配置刷新完毕后DtpProperties已经是最新的配置了,直接去刷新dynamic-tp的配置即可。

   

@Slf4j
public class CloudConsulRefresher extends AbstractRefresher implements SmartApplicationListener {

    @Resource
    private DtpProperties dtpProperties;

    @Override
    public boolean supportsEventType(@NonNull Class<? extends ApplicationEvent> eventType) {
        return RefreshScopeRefreshedEvent.class.isAssignableFrom(eventType);
    }

    @Override
    public void onApplicationEvent(@NonNull ApplicationEvent event) {
        if (event instanceof RefreshScopeRefreshedEvent) {
            doRefresh(dtpProperties);
        }
    }
}

总结

    不得不说SpringCloud提供的配置中心客户端简直太简单了,同样的SpringCloud也为Zookeeper,Nacos提供了相应的config-starter,之前笔者提供了基于CuratorFramework的实现,为的是非SpringBoot的程序接入,针对SpringCloud笔者也对spring-cloud-starter-zookeeper-config进行了实现,实现与Consul完全一样。

 类似资料: