一、簡介
ServerList就是server的列表,我們知道,IRule做負載均衡的關鍵參數就是server列表,就是從這裏獲取的。
二、接口
其接口比較簡單,定義了兩個方法,如下:
public interface ServerList<T extends Server> {
public List<T> getInitialListOfServers();
public List<T> getUpdatedListOfServers();
}
- getInitialListOfServers獲取初始化列表
- getUpdatedListOfServers獲取更新的列表
其實現如下:
三、實現
- 大概的實現類流程如下:
- ConfigurationBasedServerList
對於這個類,看名字就能猜出大概,基於配置的server類別。
它主要作用就是從配置文件獲取listOfServers參數,組裝成server列表,配置項類似如下:
xxxx: #xxxx代表服務名
ribbon:
listOfServers: aa.com,bb.com
- DiscoveryEnabledNIWSServerList
這個類實現了從EurekaClient獲取server的信息,一般使用Eureka做註冊發現,默認使用此類DiscoveryEnabledNIWSServerList。
從Eureka獲取server地址這個不多說,屬於EurekaClient的範疇,獲取到的信息組裝爲DiscoveryEnabledServer,此類繼承自Server,多了一些Eureka的信息,比如instanceId,狀態信息,UP表示可用。 - DomainExtractingServerList僅僅是對DiscoveryEnabledNIWSServerList進行包裝,內部其實就是委託DiscoveryEnabledNIWSServerList實現。