一、简介
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实现。