十二、soul源碼學(xué)習(xí)-divide插件探活機(jī)制

在前面講過,在fetchConfig中,有一部分DataRefresh邏輯,我們看下SelectorDataRefresh邏輯

//org.dromara.soul.sync.data.http.refresh.SelectorDataRefresh#refresh
@Override
protected void refresh(final List<SelectorData> data) {
  if (CollectionUtils.isEmpty(data)) {
    log.info("clear all selector cache, old cache");
    data.forEach(pluginDataSubscriber::unSelectorSubscribe);
    pluginDataSubscriber.refreshSelectorDataAll();
  } else {
    // update cache for UpstreamCacheManager
    pluginDataSubscriber.refreshSelectorDataAll();
    data.forEach(pluginDataSubscriber::onSelectorSubscribe);
  }
}

重點(diǎn)看下onSelectorSubscribe。

//org.dromara.soul.plugin.base.cache.CommonPluginDataSubscriber#subscribeDataHandler
if (data instanceof SelectorData) {
  SelectorData selectorData = (SelectorData) data;
  if (dataType == DataEventTypeEnum.UPDATE) {
    BaseDataCache.getInstance().cacheSelectData(selectorData);
    Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData));
  } else if (dataType == DataEventTypeEnum.DELETE) {
    BaseDataCache.getInstance().removeSelectData(selectorData);
    Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.removeSelector(selectorData));
  }

我們關(guān)注SelectorData這里,最后會(huì)走到handler.handlerSelector(selectorData)。handlerSelector實(shí)現(xiàn)暫時(shí)只有DividePluginHandler的一種實(shí)現(xiàn)。

image.png
//org.dromara.soul.plugin.divide.handler.DividePluginDataHandler
public class DividePluginDataHandler implements PluginDataHandler {
    
  @Override
  public void handlerSelector(final SelectorData selectorData) {
    UpstreamCacheManager.getInstance().submit(selectorData);
  }

  @Override
  public void removeSelector(final SelectorData selectorData) {
    UpstreamCacheManager.getInstance().removeByKey(selectorData.getId());
  }

  @Override
  public String pluginNamed() {
    return PluginEnum.DIVIDE.getName();
  }
}

主要用到了UpstreamCacheManager。該對(duì)象是一個(gè)單例的實(shí)現(xiàn)

//org.dromara.soul.plugin.divide.cache.UpstreamCacheManager
public final class UpstreamCacheManager {

    private static final UpstreamCacheManager INSTANCE = new UpstreamCacheManager();
     
    private static final Map<String, List<DivideUpstream>> UPSTREAM_MAP = Maps.newConcurrentMap();

    private static final Map<String, List<DivideUpstream>> UPSTREAM_MAP_TEMP = Maps.newConcurrentMap();


    /**
     * 構(gòu)造方法創(chuàng)建了一個(gè)調(diào)度線程池,并根據(jù)soul.upstream.scheduledTime配置來獲取調(diào)度時(shí)間,每次執(zhí)行scheduled方法
     */
    private UpstreamCacheManager() {
        boolean check = Boolean.parseBoolean(System.getProperty("soul.upstream.check", "false"));
        if (check) {
            new ScheduledThreadPoolExecutor(1, SoulThreadFactory.create("scheduled-upstream-task", false))
                    .scheduleWithFixedDelay(this::scheduled,
                            30, Integer.parseInt(System.getProperty("soul.upstream.scheduledTime", "30")), TimeUnit.SECONDS);
        }
    }
}

我們?cè)趆andlerSelector看到這里會(huì)調(diào)用UpstreamCacheManager.getInstance().submit(selectorData);,我們?cè)诳聪聅ubmit具體做了什么

//org.dromara.soul.plugin.divide.cache.UpstreamCacheManager#submit
public void submit(final SelectorData selectorData) {
  final List<DivideUpstream> upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), DivideUpstream.class);
  //如果上游配置不為空,則將當(dāng)前上游的配置放入到MAP中
  if (null != upstreamList && upstreamList.size() > 0) {
    UPSTREAM_MAP.put(selectorData.getId(), upstreamList);
    UPSTREAM_MAP_TEMP.put(selectorData.getId(), upstreamList);
    //如果上游配置列表為空,則直接將當(dāng)前SelectData對(duì)應(yīng)的id從MAP清空
  } else {
    UPSTREAM_MAP.remove(selectorData.getId());
    UPSTREAM_MAP_TEMP.remove(selectorData.getId());
  }
}

我們?cè)賮砜聪聅heduled

//org.dromara.soul.plugin.divide.cache.UpstreamCacheManager#scheduled
private void scheduled() {
  if (UPSTREAM_MAP.size() > 0) {
    UPSTREAM_MAP.forEach((k, v) -> {
      // 對(duì) 上游 進(jìn)行探活檢查
      List<DivideUpstream> result = check(v);
      // 將探活后的結(jié)果更新到UPSTREAM_MAP_TEMP
      if (result.size() > 0) {
        UPSTREAM_MAP_TEMP.put(k, result);
      } else {
        UPSTREAM_MAP_TEMP.remove(k);
      }
    });
  }
}

這里看到,我們的UPSTREAM_MAP相當(dāng)于一個(gè)原始的配置,而UPSTREAM_MAP_TEMP相當(dāng)于 當(dāng)前真正存活的數(shù)據(jù)

在看下check

//org.dromara.soul.plugin.divide.cache.UpstreamCacheManager#check
private List<DivideUpstream> check(final List<DivideUpstream> upstreamList) {
  List<DivideUpstream> resultList = Lists.newArrayListWithCapacity(upstreamList.size());
  for (DivideUpstream divideUpstream : upstreamList) {
    //通過 UpstreamCheckUtils 來 判斷url狀態(tài) 1.1
    final boolean pass = UpstreamCheckUtils.checkUrl(divideUpstream.getUpstreamUrl());
    if (pass) {
      //存活
      if (!divideUpstream.isStatus()) {
        divideUpstream.setTimestamp(System.currentTimeMillis());
        divideUpstream.setStatus(true);
        log.info("UpstreamCacheManager detect success the url: {}, host: {} ", divideUpstream.getUpstreamUrl(), divideUpstream.getUpstreamHost());
      }
      resultList.add(divideUpstream);
      //已經(jīng)掛掉
    } else {
      divideUpstream.setStatus(false);
      log.error("check the url={} is fail ", divideUpstream.getUpstreamUrl());
    }
  }
  return resultList;

}

1.1 checkUrl 具體的檢查機(jī)制

//org.dromara.soul.common.utils.UpstreamCheckUtils#checkUrl
public static boolean checkUrl(final String url) {
  if (StringUtils.isBlank(url)) {
    return false;
  }
  if (checkIP(url)) {
    String[] hostPort;
    if (url.startsWith(HTTP)) {
      final String[] http = StringUtils.split(url, "\\/\\/");
      hostPort = StringUtils.split(http[1], Constants.COLONS);
    } else {
      hostPort = StringUtils.split(url, Constants.COLONS);
    }
    return isHostConnector(hostPort[0], Integer.parseInt(hostPort[1]));
  } else {
    return isHostReachable(url);
  }
}

這里我們就看到了Divide插件具體的探活機(jī)制。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容