您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java動態線程池插件dynamic-tp集成zookeeper怎么配置的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java動態線程池插件dynamic-tp集成zookeeper怎么配置文章都會有所收獲,下面我們一起來看看吧。
dynamic-tp
是一個輕量級的動態線程池插件,它是一個基于配置中心的動態線程池,線程池的參數可以通過配置中心配置進行動態的修改,在配置中心的支持上最開始的時候支持Nacos
和Apollo
。
dynamic-tp
提供了一個刷新配置的接口Refresher
,抽象類AbstractRefresher
實現刷新配置接口的刷新配置方法refresh
,該方法能根據配置類型內容和配置解析配置并刷新動態線程池的相關配置,由DtpRegistry
負責刷新線程池配置,事件發布訂閱模式操作Web容器參數,代碼如下:
public interface Refresher { /** * Refresh with specify content. * @param content content * @param fileType file type */ void refresh(String content, ConfigFileTypeEnum fileType); }
@Slf4j public abstract class AbstractRefresher implements Refresher { @Resource private DtpProperties dtpProperties; @Resource private ApplicationEventMulticaster applicationEventMulticaster; @Override public void refresh(String content, ConfigFileTypeEnum fileTypeEnum) { if (StringUtils.isBlank(content) || Objects.isNull(fileTypeEnum)) { return; } try { // 根據配置內容和配置類型將配置內容轉成Map val prop = ConfigHandler.getInstance().parseConfig(content, fileTypeEnum); doRefresh(prop); } catch (IOException e) { log.error("DynamicTp refresh error, content: {}, fileType: {}", content, fileTypeEnum, e); } } private void doRefresh(Map<Object, Object> properties) { // 將Map中的配置轉換成DtpProperties ConfigurationPropertySource sources = new MapConfigurationPropertySource(properties); Binder binder = new Binder(sources); ResolvableType type = ResolvableType.forClass(DtpProperties.class); Bindable<?> target = Bindable.of(type).withExistingValue(dtpProperties); binder.bind(MAIN_PROPERTIES_PREFIX, target); // 刷新動態線程池配置 DtpRegistry.refresh(dtpProperties); // 發布刷新實現,該事件用于控制Web容器線程池參數控制 publishEvent(); } private void publishEvent() { RefreshEvent event = new RefreshEvent(this, dtpProperties); applicationEventMulticaster.multicastEvent(event); } }
基于AbstractRefresher
就可以實現Zookeeper
配置中心的擴展了,Zookeeper
的擴展實現繼承AbstractRefresher
,Zookeeper
的擴展實現只需要監聽配置中心的配置變更即可拿到配置內容,然后通過refresh
刷新配置即可。代碼如下:
ZookeeperRefresher
繼承AbstractRefresher
,實現InitializingBean
,afterPropertiesSet
方法邏輯從配置DtpProperties
獲取Zookeeper
的配置信息,CuratorFrameworkFactory
創建客戶端,設置監聽器,這里有兩種監聽器,一個是連接監聽ConnectionStateListener
,一個是節點變動監聽CuratorListener
,出發監聽后loadNode
負責從Zookeeper
獲取配置文件配置并組裝配置內容,然后通過refresh
刷新配置,注意,Zookeeper
配置目前配置類型僅支持properties
。
@Slf4j public class ZookeeperRefresher extends AbstractRefresher implements InitializingBean { @Resource private DtpProperties dtpProperties; private CuratorFramework curatorFramework; @Override public void afterPropertiesSet() throws Exception { DtpProperties.Zookeeper zookeeper = dtpProperties.getZookeeper(); curatorFramework = CuratorFrameworkFactory.newClient(zookeeper.getZkConnectStr(), new ExponentialBackoffRetry(1000, 3)); String nodePath = ZKPaths.makePath(ZKPaths.makePath(zookeeper.getRootNode(), zookeeper.getConfigVersion()), zookeeper.getNode()); final ConnectionStateListener connectionStateListener = (client, newState) -> { if (newState == ConnectionState.CONNECTED || newState == ConnectionState.RECONNECTED) { loadNode(nodePath); }}; final CuratorListener curatorListener = (client, curatorEvent) -> { final WatchedEvent watchedEvent = curatorEvent.getWatchedEvent(); if (null != watchedEvent) { switch (watchedEvent.getType()) { case NodeChildrenChanged: case NodeDataChanged: loadNode(nodePath); break; default: break; } }}; curatorFramework.getConnectionStateListenable().addListener(connectionStateListener); curatorFramework.getCuratorListenable().addListener(curatorListener); curatorFramework.start(); log.info("DynamicTp refresher, add listener success, nodePath: {}", nodePath); } /** * load config and refresh * @param nodePath config path */ public void loadNode(String nodePath) { try { final GetChildrenBuilder childrenBuilder = curatorFramework.getChildren(); final List<String> children = childrenBuilder.watched().forPath(nodePath); StringBuilder content = new StringBuilder(); children.forEach(c -> { String n = ZKPaths.makePath(nodePath, c); final String nodeName = ZKPaths.getNodeFromPath(n); final GetDataBuilder data = curatorFramework.getData(); String value = ""; try { value = new String(data.watched().forPath(n), StandardCharsets.UTF_8); } catch (Exception e) { log.error("zk config value watched exception.", e); } content.append(nodeName).append("=").append(value).append("\n"); }); refresh(content.toString(), ConfigFileTypeEnum.PROPERTIES); } catch (Exception e) { log.error("load zk node error, nodePath is {}", nodePath, e); } } }
關于“Java動態線程池插件dynamic-tp集成zookeeper怎么配置”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java動態線程池插件dynamic-tp集成zookeeper怎么配置”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。