按照前面两个同步数据的分析,能够看到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小程序