欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Soul网关:初探数据同步的奥秘

最编程 2024-08-09 18:35:30
...

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

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

DataChangedListener的主要实现

根据上面所示。获取数据是由HttpLongPollingDataChangedListener来实现的,这不禁使我想要去看看这个DataChangedListener的实现。后续我们可以发现一个实现了Spring事件接口ApplicationListener的类。

@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的事件处理机制以及设计模式中相关联的观察者模式等设计模式

Soul网关同步数据逻辑初探_spring_03