上一節(jié)講了從我們SpringBoot項目同步數(shù)據(jù)到SoulAdmin并將數(shù)據(jù)持久化到數(shù)據(jù)庫中二拐,這一節(jié)
在數(shù)據(jù)持久化到數(shù)據(jù)庫中之后,SoulAdmin會通過Spring的ApplicationEventPublisher發(fā)送一個事件變更事件凳兵,繼承自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
//對應(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
//對應(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 實現(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類型進行不同的處理
@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注入到當前類的listeners
@Override
public void afterPropertiesSet() {
Collection<DataChangedListener> listenerBeans = applicationContext.getBeansOfType(DataChangedListener.class).values();
this.listeners = Collections.unmodifiableList(new ArrayList<>(listenerBeans));
}
}
接口DataChangedListener不同實現(xiàn)類有如下:
即對應(yīng)了現(xiàn)在Soul的四種數(shù)據(jù)同步方式:http長輪訓(xùn)巷挥,Websocket桩卵,Nacos,Zookeeper
1.Zookeeper方式很簡單倍宾,就是依賴Zookeeper的watch機制雏节,SoulAdmin在啟動的時候?qū)?shù)據(jù)全量同步到Zookeeper,之后變化增量更新數(shù)據(jù)高职。
- Websocket
和
Zookeeper機制有點類似钩乍,將網(wǎng)關(guān)與
admin建立好
websocket連接時,
admin會推送一次全量數(shù)據(jù)怔锌,后續(xù)如果配置數(shù)據(jù)發(fā)生變更寥粹,則將增量數(shù)據(jù)通過
websocket主動推送給
soul-web。