八、soul源码学习-SoulAdmin事件同步机制源码解析-1

上一节讲了从我们SpringBoot项目同步数据到SoulAdmin并将数据持久化到数据库中,这一节

在数据持久化到数据库中之后,SoulAdmin会通过Spring的ApplicationEventPublisher发送一个事件变更事件,继承自org.springframework.context.ApplicationEvent

//org.dromara.soul.admin.listener.DataChangedEvent#DataChangedEvent
public class DataChangedEvent extends ApplicationEvent {

    private DataEventTypeEnum eventType;

    private ConfigGroupEnum groupKey;

    /**
     * Instantiates a new Data changed event.
     *
     * @param groupKey the group key
     * @param type     the type
     * @param source   the source
     */
    public DataChangedEvent(final ConfigGroupEnum groupKey, final DataEventTypeEnum type, final List source) {
        super(source);
        this.eventType = type;
        this.groupKey = groupKey;
    }

    /**
     * Gets event type.
     *
     * @return the event type
     */
    DataEventTypeEnum getEventType() {
        return eventType;
    }

    @Override
    public List getSource() {
        return (List) super.getSource();
    }

    /**
     * Gets group key.
     *
     * @return the group key
     */
    public ConfigGroupEnum getGroupKey() {
        return this.groupKey;
    }

}

DataChangedEvent存在两个属性,eventType

//org.dromara.soul.common.enums.DataEventTypeEnum
//对应着事件的类型
public enum DataEventTypeEnum {

    /**
     * delete event.
     */
    DELETE,

    /**
     * insert event.
     */
    CREATE,

    /**
     * update event.
     */
    UPDATE,

    /**
     * REFRESH data event type enum.
     */
    REFRESH,

    /**
     * Myself data event type enum.
     */
    MYSELF;
}
//org.dromara.soul.common.enums.ConfigGroupEnum
//对应着配置分组,现在总共支持 APP_AUTH:配置,PLUGIN:插件,RULE:规则,SELECTOR:选择器,META_DATA:元数据 
public enum ConfigGroupEnum {

    /**
     * App auth config group enum.
     */
    APP_AUTH,

    /**
     * Plugin config group enum.
     */
    PLUGIN,

    /**
     * Rule config group enum.
     */
    RULE,

    /**
     * Selector config group enum.
     */
    SELECTOR,

    /**
     * Meta data config group enum.
     */
    META_DATA;
}

接下来通过查询org.dromara.soul.admin.listener.DataChangedEvent调用他的地方。定位到DataChangedEventDispatcher

DataChangedEventDispatcher 实现了ApplicationListener并且接收DataChangedEvent事件,通过GroupKey的类型做不同处理

//org.dromara.soul.admin.listener.DataChangedEventDispatcher
public class DataChangedEventDispatcher implements ApplicationListener, InitializingBean {
    private ApplicationContext applicationContext;

    private List listeners;

    public DataChangedEventDispatcher(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

  // 监听DataChangedEvent,并根据ConfigGroupEnum类型进行不同的处理
    @Override
    @SuppressWarnings("unchecked")
    public void onApplicationEvent(final DataChangedEvent event) {
        for (DataChangedListener listener : listeners) {
            switch (event.getGroupKey()) {
                case APP_AUTH:
                    listener.onAppAuthChanged((List) event.getSource(), event.getEventType());
                    break;
                case PLUGIN:
                    listener.onPluginChanged((List) event.getSource(), event.getEventType());
                    break;
                case RULE:
                    listener.onRuleChanged((List) event.getSource(), event.getEventType());
                    break;
                case SELECTOR:
                    listener.onSelectorChanged((List) event.getSource(), event.getEventType());
                    break;
                case META_DATA:
                    listener.onMetaDataChanged((List) event.getSource(), event.getEventType());
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + event.getGroupKey());
            }
        }
    }

  // 将Spring代理的DataChangedListener注入到当前类的listeners
    @Override
    public void afterPropertiesSet() {
        Collection listenerBeans = applicationContext.getBeansOfType(DataChangedListener.class).values();
        this.listeners = Collections.unmodifiableList(new ArrayList<>(listenerBeans));
    }
}

接口DataChangedListener不同实现类有如下:

image.png

即对应了现在Soul的四种数据同步方式:http长轮训,Websocket,Nacos,Zookeeper

1.Zookeeper方式很简单,就是依赖Zookeeper的watch机制,SoulAdmin在启动的时候将数据全量同步到Zookeeper,之后变化增量更新数据。

image.png
  1. WebsocketZookeeper机制有点类似,将网关与admin建立好websocket连接时,admin会推送一次全量数据,后续如果配置数据发生变更,则将增量数据通过websocket主动推送给soul-web。

你可能感兴趣的:(八、soul源码学习-SoulAdmin事件同步机制源码解析-1)