一、簡介
顧名思義,這個類用於server列表的更新。
二、接口
這個類定義的方法較多,這裏只看兩個重要的:
public interface ServerListUpdater {
/**
* an interface for the updateAction that actually executes a server list update
*/
public interface UpdateAction {
void doUpdate();
}
/**
* start the serverList updater with the given update action
* This call should be idempotent.
*
* @param updateAction
*/
void start(UpdateAction updateAction);
}
UpdateAction: 更新操作接口
start:啓動更新操作
三、實現
ServerListUpdater的實現如下:
- EurekaNotificationServerListUpdater
這個類從名字就能看出來,來自Eureka的通知進行更新。其實現比較簡單,EurekaClient提供了註冊事件監聽器的方法,那麼它就是通過註冊監聽器來更新的,然後執行更新操作(UpdateAction.doUpdate)
我們知道EurekaClient默認每30秒拉取一次server信息,那麼通知事件的頻率也就是30秒一次。 - PollingServerListUpdater
含義如字名,Polling即輪詢的意思,這個類就是啓動一個定時任務,每30秒拉取一次server信息,然後執行更新操作(UpdateAction.doUpdate) - 那麼默認ribbon使用的那個實現呢,是PollingServerListUpdater,參考RibbonClientConfiguration.ribbonLoadBalancer可知。
ServerListUpdater.UpdateAction的實現如下:
它在DynamicServerListLoadBalancer中new了一個匿名內部類,他會調用ServerList進行更新,這裏先進行跳過,在負載均衡器裏面說。
四、結合
現在知道了ServerListUpdater用於server列表更新,那麼跟ServerList結合,就知道了整個更新的流程,如下圖(虛線框住的是本文涉及的部分):