八、soul源碼學(xué)習(xí)-SoulAdmin事件同步機(jī)制源碼解析-1

上一節(jié)講了從我們SpringBoot項(xiàng)目同步數(shù)據(jù)到SoulAdmin并將數(shù)據(jù)持久化到數(shù)據(jù)庫中,這一節(jié)

在數(shù)據(jù)持久化到數(shù)據(jù)庫中之后,SoulAdmin會(huì)通過Spring的ApplicationEventPublisher發(fā)送一個(gè)事件變更事件,繼承自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存在兩個(gè)屬性,eventType

//org.dromara.soul.common.enums.DataEventTypeEnum
//對(duì)應(yīng)著事件的類型
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
//對(duì)應(yīng)著配置分組,現(xiàn)在總共支持 APP_AUTH:配置,PLUGIN:插件,RULE:規(guī)則,SELECTOR:選擇器,META_DATA:元數(shù)據(jù) 
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調(diào)用他的地方。定位到DataChangedEventDispatcher

DataChangedEventDispatcher 實(shí)現(xiàn)了ApplicationListener并且接收DataChangedEvent事件,通過GroupKey的類型做不同處理

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

    private List<DataChangedListener> listeners;

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

  // 監(jiān)聽DataChangedEvent,并根據(jù)ConfigGroupEnum類型進(jìn)行不同的處理
    @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代理的DataChangedListener注入到當(dāng)前類的listeners
    @Override
    public void afterPropertiesSet() {
        Collection<DataChangedListener> listenerBeans = applicationContext.getBeansOfType(DataChangedListener.class).values();
        this.listeners = Collections.unmodifiableList(new ArrayList<>(listenerBeans));
    }
}

接口DataChangedListener不同實(shí)現(xiàn)類有如下:

image.png

即對(duì)應(yīng)了現(xiàn)在Soul的四種數(shù)據(jù)同步方式:http長(zhǎng)輪訓(xùn),Websocket,Nacos,Zookeeper

1.Zookeeper方式很簡(jiǎn)單,就是依賴Zookeeper的watch機(jī)制,SoulAdmin在啟動(dòng)的時(shí)候?qū)?shù)據(jù)全量同步到Zookeeper,之后變化增量更新數(shù)據(jù)。

image.png
  1. WebsocketZookeeper機(jī)制有點(diǎn)類似,將網(wǎng)關(guān)與admin建立好websocket連接時(shí),admin會(huì)推送一次全量數(shù)據(jù),后續(xù)如果配置數(shù)據(jù)發(fā)生變更,則將增量數(shù)據(jù)通過websocket主動(dòng)推送給soul-web。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容