共计 3279 个字符,预计需要花费 9 分钟才能阅读完成。
序
本文主要研究一下 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<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 来获取服务实例信息
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 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 来获取服务实例信息
doc
- NacosRibbonClientConfiguration