目录
RibbonEurekaAutoConfiguration类
EurekaRibbonClientConfiguration类
DiscoveryEnabledNIWSServerList-ribbon返回serviceList列表实现
概况
版本2.1.2.RELEASE
从spring-cloud-netflix-erurka-client:2.1.2.RELEASSE的META-INF/spring-factories中发现RibbonEurekaAutoConfiguration自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaClientConfigServerAutoConfiguration,\
org.springframework.cloud.netflix.eureka.config.EurekaDiscoveryClientConfigServiceAutoConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaDiscoveryClientConfigServiceBootstrapConfiguration
RibbonEurekaAutoConfiguration类
@Configuration
@EnableConfigurationProperties
@ConditionalOnRibbonAndEurekaEnabled
@AutoConfigureAfter({RibbonAutoConfiguration.class})//spring-cloud-netflix-ribbon中
@RibbonClients( //spring-cloud-netflix-ribbon中 RibbonClients初始化Eureka serviceId
defaultConfiguration = {EurekaRibbonClientConfiguration.class}
)
public class RibbonEurekaAutoConfiguration {
public RibbonEurekaAutoConfiguration() {
}
}
EurekaRibbonClientConfiguration类
@Configuration
public class EurekaRibbonClientConfiguration {
private static final Log log = LogFactory.getLog(EurekaRibbonClientConfiguration.class);
@Value("${ribbon.eureka.approximateZoneFromHostname:false}")
private boolean approximateZoneFromHostname = false;
@RibbonClientName
private String serviceId = "client";
@Autowired(
required = false
)
private EurekaClientConfig clientConfig;
@Autowired(
required = false
)
private EurekaInstanceConfig eurekaConfig;
@Autowired
private PropertiesFactory propertiesFactory;
public EurekaRibbonClientConfiguration() {
}
public EurekaRibbonClientConfiguration(EurekaClientConfig clientConfig, String serviceId, EurekaInstanceConfig eurekaConfig, boolean approximateZoneFromHostname) {
this.clientConfig = clientConfig;
this.serviceId = serviceId;
this.eurekaConfig = eurekaConfig;
this.approximateZoneFromHostname = approximateZoneFromHostname;
}
//ping 检查
@Bean
@ConditionalOnMissingBean
public IPing ribbonPing(IClientConfig config) {
if (this.propertiesFactory.isSet(IPing.class, this.serviceId)) {
return (IPing)this.propertiesFactory.get(IPing.class, config, this.serviceId);
} else {
NIWSDiscoveryPing ping = new NIWSDiscoveryPing();
ping.initWithNiwsConfig(config);
return ping;
}
}
//实例化获取serverList接口
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider) {
if (this.propertiesFactory.isSet(ServerList.class, this.serviceId)) {
return (ServerList)this.propertiesFactory.get(ServerList.class, config, this.serviceId);
} else {
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerList(config, eurekaClientProvider);
DomainExtractingServerList serverList = new DomainExtractingServerList(discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
}
}
//可通过instanceInfo获取metadata
@Bean
public ServerIntrospector serverIntrospector() {
return new EurekaServerIntrospector();
}
@PostConstruct
public void preprocess() {
String zone = ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone);
if (this.clientConfig != null && StringUtils.isEmpty(zone)) {
String availabilityZone;
if (this.approximateZoneFromHostname && this.eurekaConfig != null) {
availabilityZone = ZoneUtils.extractApproximateZone(this.eurekaConfig.getHostName(false));
log.debug("Setting Zone To " + availabilityZone);
ConfigurationManager.getDeploymentContext().setValue(ContextKey.zone, availabilityZone);
} else {
availabilityZone = this.eurekaConfig == null ? null : (String)this.eurekaConfig.getMetadataMap().get("zone");
if (availabilityZone == null) {
String[] zones = this.clientConfig.getAvailabilityZones(this.clientConfig.getRegion());
availabilityZone = zones != null && zones.length > 0 ? zones[0] : null;
}
if (availabilityZone != null) {
ConfigurationManager.getDeploymentContext().setValue(ContextKey.zone, availabilityZone);
}
}
}
RibbonUtils.initializeRibbonDefaults(this.serviceId);
}
}
DiscoveryEnabledNIWSServerList-ribbon返回serviceList列表实现
public class DiscoveryEnabledNIWSServerList extends AbstractServerList<DiscoveryEnabledServer> {
...
public DiscoveryEnabledNIWSServerList(String vipAddresses, Provider<EurekaClient> eurekaClientProvider) {
this(createClientConfig(vipAddresses), eurekaClientProvider);
}
public DiscoveryEnabledNIWSServerList(IClientConfig clientConfig, Provider<EurekaClient> eurekaClientProvider) {
this.isSecure = false;
this.prioritizeVipAddressBasedServers = true;
this.overridePort = 7001;
this.shouldUseOverridePort = false;
this.shouldUseIpAddr = false;
this.eurekaClientProvider = eurekaClientProvider;
this.initWithNiwsConfig(clientConfig);
}
//初始化
public void initWithNiwsConfig(IClientConfig clientConfig) {
this.clientName = clientConfig.getClientName();
this.vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses();
if (this.vipAddresses == null && ConfigurationManager.getConfigInstance().getBoolean("DiscoveryEnabledNIWSServerList.failFastOnNullVip", true)) {
throw new NullPointerException("VIP address for client " + this.clientName + " is null");
} else {
this.isSecure = Boolean.parseBoolean("" + clientConfig.getProperty(CommonClientConfigKey.IsSecure, "false"));
this.prioritizeVipAddressBasedServers = Boolean.parseBoolean("" + clientConfig.getProperty(CommonClientConfigKey.PrioritizeVipAddressBasedServers, this.prioritizeVipAddressBasedServers));
this.datacenter = ConfigurationManager.getDeploymentContext().getDeploymentDatacenter();
this.targetRegion = (String)clientConfig.getProperty(CommonClientConfigKey.TargetRegion);
this.shouldUseIpAddr = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.UseIPAddrForServer, DefaultClientConfigImpl.DEFAULT_USEIPADDRESS_FOR_SERVER);
if (clientConfig.getPropertyAsBoolean(CommonClientConfigKey.ForceClientPortConfiguration, false)) {
if (this.isSecure) {
if (clientConfig.containsProperty(CommonClientConfigKey.SecurePort)) {
this.overridePort = clientConfig.getPropertyAsInteger(CommonClientConfigKey.SecurePort, 7001);
this.shouldUseOverridePort = true;
} else {
logger.warn(this.clientName + " set to force client port but no secure port is set, so ignoring");
}
} else if (clientConfig.containsProperty(CommonClientConfigKey.Port)) {
this.overridePort = clientConfig.getPropertyAsInteger(CommonClientConfigKey.Port, 7001);
this.shouldUseOverridePort = true;
} else {
logger.warn(this.clientName + " set to force client port but no port is set, so ignoring");
}
}
}
}
public List<DiscoveryEnabledServer> getInitialListOfServers() {
return this.obtainServersViaDiscovery();
}
public List<DiscoveryEnabledServer> getUpdatedListOfServers() {
return this.obtainServersViaDiscovery();
}
//实例发现 返回DiscoveryEnabledServer
private List<DiscoveryEnabledServer> obtainServersViaDiscovery() {
List<DiscoveryEnabledServer> serverList = new ArrayList();
if (this.eurekaClientProvider != null && this.eurekaClientProvider.get() != null) {
EurekaClient eurekaClient = (EurekaClient)this.eurekaClientProvider.get();
if (this.vipAddresses != null) {
String[] var3 = this.vipAddresses.split(",");
int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) {
String vipAddress = var3[var5];
List<InstanceInfo> listOfInstanceInfo = eurekaClient.getInstancesByVipAddress(vipAddress, this.isSecure, this.targetRegion);
Iterator var8 = listOfInstanceInfo.iterator();
while(var8.hasNext()) {
InstanceInfo ii = (InstanceInfo)var8.next();
if (ii.getStatus().equals(InstanceStatus.UP)) {
if (this.shouldUseOverridePort) {
if (logger.isDebugEnabled()) {
logger.debug("Overriding port on client name: " + this.clientName + " to " + this.overridePort);
}
InstanceInfo copy = new InstanceInfo(ii);
if (this.isSecure) {
ii = (new Builder(copy)).setSecurePort(this.overridePort).build();
} else {
ii = (new Builder(copy)).setPort(this.overridePort).build();
}
}
DiscoveryEnabledServer des = this.createServer(ii, this.isSecure, this.shouldUseIpAddr);
serverList.add(des);
}
}
if (serverList.size() > 0 && this.prioritizeVipAddressBasedServers) {
break;
}
}
}
return serverList;
} else {
logger.warn("EurekaClient has not been initialized yet, returning an empty list");
return new ArrayList();
}
}
protected DiscoveryEnabledServer createServer(InstanceInfo instanceInfo, boolean useSecurePort, boolean useIpAddr) {
DiscoveryEnabledServer server = new DiscoveryEnabledServer(instanceInfo, useSecurePort, useIpAddr);
EurekaClientConfig clientConfig = ((EurekaClient)this.eurekaClientProvider.get()).getEurekaClientConfig();
String[] availZones = clientConfig.getAvailabilityZones(clientConfig.getRegion());
String instanceZone = InstanceInfo.getZone(availZones, instanceInfo);
server.setZone(instanceZone);
return server;
}
...
}