您好,登录后才能下订单哦!
今天就跟大家聊聊有关如何理解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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。