如何理解NacosRibbonClientConfiguration
今天就跟大家聊聊有关如何理解NacosRibbonClientConfiguration,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
公司主营业务:成都做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出仪陇免费做网站回馈大家。
序
本文主要研究一下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
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{ private NacosDiscoveryProperties discoveryProperties; private String serviceId; public NacosServerList(NacosDiscoveryProperties discoveryProperties) { this.discoveryProperties = discoveryProperties; } @Override public List getInitialListOfServers() { return getServers(); } @Override public List getUpdatedListOfServers() { return getServers(); } private List getServers() { try { List 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 instancesToServerList(List instances) { List 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来获取服务实例信息
NacosServer
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 Mapmetadata; 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 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有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
新闻标题:如何理解NacosRibbonClientConfiguration
分享网址:http://pwwzsj.com/article/psgios.html