按照前面兩個同步數據的分析,能夠看到Http同步跟其餘的同步的加載基本同樣。不一樣的地方主要是加載數據的操做
加載數據的過程主要是react
private void start() { // It could be initialized multiple times, so you need to control that. if (RUNNING.compareAndSet(false, true)) { // fetch all group configs. this.fetchGroupConfig(ConfigGroupEnum.values()); int threadSize = serverList.size(); this.executor = new ThreadPoolExecutor(threadSize, threadSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), SoulThreadFactory.create("http-long-polling", true)); // start long polling, each server creates a thread to listen for changes. this.serverList.forEach(server -> this.executor.execute(new HttpLongPollingTask(server))); } else { log.info("soul http long polling was started, executor=[{}]", executor); } }
能夠看到上述代碼中,項目建立了一個服務列表大小的線程池用來加載數據,用來提升性能和靈活性
這個加載的過程,基本就是獲取數據的過程。
根據全局查詢接口能夠看到。config/fecth和config/listener接口的相關內容
git
根據上面所示。獲取數據是由HttpLongPollingDataChangedListener來實現的,這不由使我想要去看看這個DataChangedListener的實現。後續咱們能夠發現一個實現了Spring事件接口ApplicationListener的類。關於這個類的簡單實用,能夠參考https://blog.csdn.net/liyantianmin/article/details/81017960 這篇文章。用來分別處理不一樣的事件的處理github
@Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { for (DataChangedListener listener : listeners) { switch (event.getGroupKey()) { case APP_AUTH: listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }
根據Spring的ApplicationListener可知,這裏只是事件的處理。那麼事件是如何被觸發的。我又開始了全局查找publishEvent的過程。這個聯想可知。應該是在修改數據的時候進行的改變,果然如咱們所想。能夠看到AppAuthServiceImpl的applyCreate就發佈了事件web
eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.APP_AUTH, DataEventTypeEnum.CREATE, Collections.singletonList(data)));
關於subscribe和webflux相關後續會再研究面試
歡迎搜索關注本人與朋友共同開發的微信面經小程序【大廠面試助手】和公衆號【微瞰技術】,以及總結的分類面試題https://github.com/zhendiao/JavaInterview小程序