@Configuration(proxyBeanMethods = false)
@ConditionalOnRibbonNacos
public class NacosRibbonClientConfiguration {
@Autowired
private PropertiesFactory propertiesFactory;
@Bean
@ConditionalOnMissingBean
public ServerList> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties) {
if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
config.getClientName());
return serverList;
}
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}
@Bean
@ConditionalOnMissingBean
public NacosServerIntrospector nacosServerIntrospector() {
return new NacosServerIntrospector();
}
}
在这里注册了ServerList,和NacosServerIntrospector。其中,用NacosServerList来覆盖了ribbonServerList的默认实现,在ribbon的DynamicServerListLoadBalancer中提供服务。
在com.netflix.loadbalancer.DynamicServerListLoadBalancer#updateListOfServers方法中有一个servers = serverListImpl.getUpdatedListOfServers(),这里替换成了NacosServerList的方法,用来获取服务列表。
2、更新服务列表//com.alibaba.cloud.nacos.ribbon.NacosServerList#getUpdatedListOfServers public ListgetUpdatedListOfServers() { return getServers(); }
//com.alibaba.cloud.nacos.ribbon.NacosServerList#getServers private ListgetServers() { try { //获取group String group = discoveryProperties.getGroup(); //掉用NamingService获取服务列表 List instances = discoveryProperties.namingServiceInstance() .selectInstances(serviceId, group, true); //返回服务列表 return instancesToServerList(instances); } catch (Exception e) { throw new IllegalStateException( "Can not get service instances from nacos, serviceId=" + serviceId, e); } }
public NamingService namingServiceInstance() {
return nacosServiceManager.getNamingService(this.getNacosProperties());
}
public ListselectInstances(String serviceName, String groupName, boolean healthy) throws NacosException { return selectInstances(serviceName, groupName, healthy, true); }
public ListselectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, groupName, new ArrayList (), healthy, subscribe); }
public ListselectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { ServiceInfo serviceInfo; String clusterString = StringUtils.join(clusters, ","); if (subscribe) { //直接获取服务列表 serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo) { //获取服务信息,参见上一篇 serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); } } else { //获取服务信息,参见上一篇 serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, 0, false); } //过滤 return selectInstances(serviceInfo, healthy); }
public ServiceInfo getServiceInfo(final String serviceName, final String groupName, final String clusters) {
NAMING_LOGGER.debug("failover-mode: " + failoverReactor.isFailoverSwitch());
String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
String key = ServiceInfo.getKey(groupedServiceName, clusters);
if (failoverReactor.isFailoverSwitch()) {
return failoverReactor.getService(key);
}
return serviceInfoMap.get(key);
}
过滤服务列表,将不可用的服务移除
private ListselectInstances(ServiceInfo serviceInfo, boolean healthy) { List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList (); } Iterator iterator = list.iterator(); while (iterator.hasNext()) { Instance instance = iterator.next(); if (healthy != instance.isHealthy() || !instance.isEnabled() || instance.getWeight() <= 0) { iterator.remove(); } } return list; }
将服务包装成NacosServer返回
NacosServer继承了Server
private ListinstancesToServerList(List instances) { List result = new ArrayList<>(); if (CollectionUtils.isEmpty(instances)) { return result; } for (Instance instance : instances) { result.add(new NacosServer(instance)); } return result; }



