Source Code Resolution of Nacos Configuration Center for Micro Services

Nacos Configuration Center Source Parsing

Source Entry

ConfigFactory.createConfigService

ConfigService configService = NacosFactory.createConfigService(properties);
String  content = configService.getConfig(dataId,groupId,3000);

Constructing ConfigService through Factory

Decorator mode Metrics HttpAgent wraps http requests, adding monitoring

Agent - > Agent http way to initiate requests for client Worker - > Specific work related

NacosConfigService.NacosConfigService

this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
this.agent.start();
this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties);
  • Create a proxy for http Agent - > Initiate http requests

  • Create a client Worker - > asynchronous thread (timing task)

this.executor.scheduleWithFixedDelay(new Runnable() {
   public void run() {
       try {
           ClientWorker.this.checkConfigInfo();
      } catch (Throwable var2) {
           ClientWorker.LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", var2);
      }

  }
}, 1L, 10L, TimeUnit.MILLISECONDS);

What if there are many requests for long polling?

Batch processing

public void checkConfigInfo() {
   //Sub-tasks
   int listenerSize = ((Map)this.cacheMap.get()).size();
   //Upward rectification
   int longingTaskCount = (int)Math.ceil((double)listenerSize / ParamUtil.getPerTaskConfigSize());
   if ((double)longingTaskCount > this.currentLongingTaskCount) {
       for(int i = (int)this.currentLongingTaskCount; i < longingTaskCount; ++i) {
           //To determine whether a task is executed or not, you need to think carefully about the fact that the task list is now a disorderly process of change that may be problematic.
           this.executorService.execute(new ClientWorker.LongPollingRunnable(i));
      }

       this.currentLongingTaskCount = (double)longingTaskCount;
  }

}

Check local configuration listening - >

Check cached MD5

Check file update time

ClientWorker.this.checkLocalConfig(cacheData);
if (cacheData.isUseLocalConfigInfo()) {
  cacheData.checkListenerMd5();
}

Check remote

List<String> changedGroupKeys = ClientWorker.this.checkUpdateDataIds(cacheDatas, inInitializingCacheList);
Iterator var16 = changedGroupKeys.iterator();

groupId+dataId+tenant remote check

ClientWorker.this.getServerConfig(dataId, group, tenant, 3000L);
void checkListenerMd5() {
   Iterator var1 = this.listeners.iterator();

   while(var1.hasNext()) {
       ManagerListenerWrap wrap = (ManagerListenerWrap)var1.next();
       if (!this.md5.equals(wrap.lastCallMd5)) {
           this.safeNotifyListener(this.dataId, this.group, this.content, this.md5, wrap);
      }
  }

}

Get configuration

Get it locally first, if not remotely

content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant);
if (content != null) {
   cr.setContent(content);
} else {
       content = this.worker.getServerConfig(dataId, group, tenant, timeoutMs);
     
}

Tags: PHP

Posted on Tue, 08 Oct 2019 14:06:58 -0700 by NTGr