您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何理解NacosRibbonClientConfiguration,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
本文主要研究一下NacosRibbonClientConfiguration
spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfiguration.java
@Configuration @ConditionalOnRibbonNacos public class NacosRibbonClientConfiguration { @Bean @ConditionalOnMissingBean public ServerList<?> ribbonServerList(IClientConfig config, NacosDiscoveryProperties nacosDiscoveryProperties) { NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties); serverList.initWithNiwsConfig(config); return serverList; } }
NacosRibbonClientConfiguration會創建NacosServerList
spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java
public class NacosServerList extends AbstractServerList<NacosServer> { private NacosDiscoveryProperties discoveryProperties; private String serviceId; public NacosServerList(NacosDiscoveryProperties discoveryProperties) { this.discoveryProperties = discoveryProperties; } @Override public List<NacosServer> getInitialListOfServers() { return getServers(); } @Override public 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; } @Override public void initWithNiwsConfig(IClientConfig iClientConfig) { this.serviceId = iClientConfig.getClientName(); } }
NacosServerList繼承了com.netflix.loadbalancer.AbstractServerList,其getInitialListOfServers及getUpdatedListOfServers方法都是調用了getServers;getServers方法則是通過NamingService.selectInstances來獲取服務實例信息
spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java
public class NacosServer extends Server { private final MetaInfo metaInfo; private final Instance instance; private final Map<String, String> metadata; public NacosServer(final Instance instance) { super(instance.getIp(), instance.getPort()); this.instance = instance; this.metaInfo = new MetaInfo() { @Override public String getAppName() { return instance.getServiceName(); } @Override public String getServerGroup() { return null; } @Override public String getServiceIdForDiscovery() { return null; } @Override public String getInstanceId() { return instance.getInstanceId(); } }; this.metadata = instance.getMetadata(); } @Override public MetaInfo getMetaInfo() { return metaInfo; } public Instance getInstance() { return instance; } public Map<String, String> getMetadata() { return metadata; } }
NacosServer繼承了com.netflix.loadbalancer.Server;它主要有metaInfo、instance、metadata三個屬性
NacosRibbonClientConfiguration會創建NacosServerList;NacosServerList繼承了com.netflix.loadbalancer.AbstractServerList,其getInitialListOfServers及getUpdatedListOfServers方法都是調用了getServers;getServers方法則是通過NamingService.selectInstances來獲取服務實例信息
看完上述內容,你們對如何理解NacosRibbonClientConfiguration有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。