今天就跟大家聊聊有關(guān)如何理解Ribbon中的ServerList,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為綏化企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站建設(shè),綏化網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
ServerList是存數(shù)服務(wù)實(shí)例的對象。
ServerList
public interface ServerList{ public List getInitialListOfServers(); /** * Return updated list of servers. This is called say every 30 secs * (configurable) by the Loadbalancer's Ping cycle * */ public List getUpdatedListOfServers(); }
StaticServerList
通過靜態(tài)配置來維護(hù)服務(wù)列表。
public class StaticServerListimplements ServerList { private final List servers; public StaticServerList(T... servers) { this.servers = Arrays.asList(servers); } @Override public List getInitialListOfServers() { return servers; } @Override public List getUpdatedListOfServers() { return servers; } }
AbstractServerList
ServerList攔截器,被LoadBalancer使用。
public abstract class AbstractServerListimplements ServerList , IClientConfigAware { /** * Get a ServerListFilter instance. It uses {@link ClientFactory#instantiateInstanceWithClientConfig(String, IClientConfig)} * which in turn uses reflection to initialize the filter instance. * The filter class name is determined by the value of {@link CommonClientConfigKey#NIWSServerListFilterClassName} * in the {@link IClientConfig}. The default implementation is {@link ZoneAffinityServerListFilter}. */ public AbstractServerListFilter getFilterImpl(IClientConfig niwsClientConfig) throws ClientException{ try { String niwsServerListFilterClassName = niwsClientConfig .getProperty( CommonClientConfigKey.NIWSServerListFilterClassName, ZoneAffinityServerListFilter.class.getName()) .toString(); AbstractServerListFilter abstractNIWSServerListFilter = (AbstractServerListFilter ) ClientFactory.instantiateInstanceWithClientConfig(niwsServerListFilterClassName, niwsClientConfig); return abstractNIWSServerListFilter; } catch (Throwable e) { throw new ClientException( ClientException.ErrorType.CONFIGURATION, "Unable to get an instance of CommonClientConfigKey.NIWSServerListFilterClassName. Configured class:" + niwsClientConfig .getProperty(CommonClientConfigKey.NIWSServerListFilterClassName), e); } } }
ConfigurationBasedServerList
通過配置文件參數(shù)listOfservers,來實(shí)現(xiàn)ServerList.多個(gè)用逗號分隔。
public class ConfigurationBasedServerList extends AbstractServerList{ private IClientConfig clientConfig; @Override public List getInitialListOfServers() { return getUpdatedListOfServers(); } @Override public List getUpdatedListOfServers() { String listOfServers = clientConfig.get(CommonClientConfigKey.ListOfServers); return derive(listOfServers); } @Override public void initWithNiwsConfig(IClientConfig clientConfig) { this.clientConfig = clientConfig; } protected List derive(String value) { List list = Lists.newArrayList(); if (!Strings.isNullOrEmpty(value)) { for (String s: value.split(",")) { list.add(new Server(s.trim())); } } return list; } }
DiscoveryEnabledNIWSServerList
通過Eureka的服務(wù)發(fā)現(xiàn),實(shí)現(xiàn)的ServerList.
public class DiscoveryEnabledNIWSServerList extends AbstractServerList{ @Override public List getInitialListOfServers(){ return obtainServersViaDiscovery(); } @Override public List getUpdatedListOfServers(){ return obtainServersViaDiscovery(); } private List obtainServersViaDiscovery() { List serverList = new ArrayList (); if (eurekaClientProvider == null || eurekaClientProvider.get() == null) { logger.warn("EurekaClient has not been initialized yet, returning an empty list"); return new ArrayList (); } EurekaClient eurekaClient = eurekaClientProvider.get(); if (vipAddresses!=null){ for (String vipAddress : vipAddresses.split(",")) { // if targetRegion is null, it will be interpreted as the same region of client List listOfInstanceInfo = eurekaClient.getInstancesByVipAddress(vipAddress, isSecure, targetRegion); for (InstanceInfo ii : listOfInstanceInfo) { if (ii.getStatus().equals(InstanceStatus.UP)) { if(shouldUseOverridePort){ if(logger.isDebugEnabled()){ logger.debug("Overriding port on client name: " + clientName + " to " + overridePort); } // copy is necessary since the InstanceInfo builder just uses the original reference, // and we don't want to corrupt the global eureka copy of the object which may be // used by other clients in our system InstanceInfo copy = new InstanceInfo(ii); if(isSecure){ ii = new InstanceInfo.Builder(copy).setSecurePort(overridePort).build(); }else{ ii = new InstanceInfo.Builder(copy).setPort(overridePort).build(); } } DiscoveryEnabledServer des = new DiscoveryEnabledServer(ii, isSecure, shouldUseIpAddr); des.setZone(DiscoveryClient.getZone(ii)); serverList.add(des); } } if (serverList.size()>0 && prioritizeVipAddressBasedServers){ break; // if the current vipAddress has servers, we dont use subsequent vipAddress based servers } } } return serverList; } }
看完上述內(nèi)容,你們對如何理解Ribbon中的ServerList有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。