javascript
SpringCloud Ribbon(五)之服务实例列表ServerList
一、服務(wù)實(shí)例列表ServerList
服務(wù)實(shí)例列表(ServerList)為負(fù)載均衡器(Loadbalancer)提供服務(wù)的可用實(shí)例列表。
負(fù)載均衡器(Loadbalancer)通過服務(wù)實(shí)例列表(ServerList)從注冊中心(register)或者配置文件(yaml或properties)上讀取全部服務(wù)實(shí)例(server),然后以服務(wù)實(shí)例過濾器(ServerListFilter)的過濾方式進(jìn)行篩選留下滿足條件的服務(wù)實(shí)例,進(jìn)而借助負(fù)載均衡策略(IRule)選擇出一個合適的服務(wù)實(shí)例。
?
二、ServerList實(shí)現(xiàn)類
NacosServerList? ?nacos服務(wù)發(fā)現(xiàn)提供的ServerList
ConfigurationBasedServerList? ?從配置文件讀取靜態(tài)的服務(wù)實(shí)例定義
StaticServerList? 靜態(tài)ServerList創(chuàng)建包含不變的服務(wù)實(shí)例(server)
?
三、具體代碼實(shí)現(xiàn)
(1)NacosServerList? ?
public class NacosServerList extends AbstractServerList<NacosServer> {private NacosDiscoveryProperties discoveryProperties;private String serviceId;public NacosServerList(NacosDiscoveryProperties discoveryProperties) {this.discoveryProperties = discoveryProperties;}@Overridepublic List<NacosServer> getInitialListOfServers() {return getServers();}@Overridepublic List<NacosServer> getUpdatedListOfServers() {return getServers();}private List<NacosServer> getServers() {try {List<Instance> instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, true);return instancesToServerList(instances);}catch (Exception e) {throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId,e);}}private List<NacosServer> instancesToServerList(List<Instance> instances) {List<NacosServer> result = new ArrayList<>();if (null == instances) {return result;}for (Instance instance : instances) {result.add(new NacosServer(instance));}return result;}public String getServiceId() {return serviceId;}@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {this.serviceId = iClientConfig.getClientName();} }(2)ConfigurationBasedServerList?
public class ConfigurationBasedServerList extends AbstractServerList<Server> {private IClientConfig clientConfig;@Overridepublic List<Server> getInitialListOfServers() {return getUpdatedListOfServers();}@Overridepublic List<Server> getUpdatedListOfServers() {String listOfServers = clientConfig.get(CommonClientConfigKey.ListOfServers);return derive(listOfServers);}@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {this.clientConfig = clientConfig;}protected List<Server> derive(String value) {List<Server> list = Lists.newArrayList();if (!Strings.isNullOrEmpty(value)) {for (String s: value.split(",")) {list.add(new Server(s.trim()));}}return list;} }(3)StaticServerList?
public class StaticServerList<T extends Server> implements ServerList<T> {private final List<T> servers;public StaticServerList(T... servers) {this.servers = Arrays.asList(servers);}@Overridepublic List<T> getInitialListOfServers() {return servers;}@Overridepublic List<T> getUpdatedListOfServers() {return servers;}}?
總結(jié)
以上是生活随笔為你收集整理的SpringCloud Ribbon(五)之服务实例列表ServerList的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud Ribbon(四
- 下一篇: SpringCloud Ribbon(六