2021-01-23

Soul网关同步数据逻辑初探

Http同步数据

按照前面两个同步数据的分析,可以看到Http同步跟其他的同步的加载基本一样。不同的地方主要是加载数据的操作
加载数据的过程主要是

 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);  } }

可以看到上述代码中,项目创建了一个服务列表大小的线程池用来加载数据,用来提高性能和灵活性
file
这个加载的过程,基本就是获取数据的过程。
根据全局查询接口可以看到。config/fecth和config/listener接口的相关内容
file

DataChangedListener的主要实现

根据上面所示。获取数据是由HttpLongPollingDataChangedListener来实现的,这不禁使我想要去看看这个DataChangedListener的实现。后续我们可以发现一个实现了Spring事件接口ApplicationListener的类。关于这个类的简单实用,可以参考https://blog.csdn.net/liyantianmin/article/details/81017960 这篇文章。用来分别处理不同的事件的处理

 @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就发布了事件

  eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.APP_AUTH, DataEventTypeEnum.CREATE,    Collections.singletonList(data)));

截止目前关于Reactor中一些概念的理解

  • Mono 数据流中要么是空,要么是一个对象
  • Flux 数据流中可能是一个对象,也有可能是空,也有可能是多个对象。即可能推测。Flux表达的范围比Mono更大
  • Operator操作符map,filter,flatMap即类似Java8中的一些流处理数据操作符。只不过在Reactor中是响应式的操作

关于subscribe和webflux相关后续会再研究

总结和问题

  • 目前来说,由于事件的处理机制是依赖于Spring的事件处理。那么非Spring做个性化修改就比较麻烦了
  • 个人之前对于这种事件处理机制的了解不够,还需要深入学习Spring的事件处理机制以及设计模式中相关联的观察者模式等设计模式

欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】,以及总结的分类面试题https://github.com/zhendiao/JavaInterview

file
file









原文转载:http://www.shaoqun.com/a/512628.html

跨境电商:https://www.ikjzd.com/

olive:https://www.ikjzd.com/w/2025

marks spencer:https://www.ikjzd.com/w/2385


Http同步数据按照前面两个同步数据的分析,可以看到Http同步跟其他的同步的加载基本一样。不同的地方主要是加载数据的操作加载数据的过程主要是privatevoidstart(){//Itcouldbeinitializedmultipletimes,soyouneedtocontrolthat.if(RUNNING.compareAndSet(false,true)){//fetchallgro
菜鸟网:菜鸟网
易麦:易麦
老挝塔銮景点介绍 - :老挝塔銮景点介绍 -
2020年中秋是什么时候?几月几号?:2020年中秋是什么时候?几月几号?
加拿大的夜生活 :加拿大的夜生活

No comments:

Post a Comment