* @return - The list of all eureka service urls for the eureka client to
* talk to.
*/
public List<String> getServiceUrlsFromDNS(String instanceZone,
boolean preferSameZone) {
Stopwatch t = GET_SERVICE_URLS_DNS_TIMER.start();
String region = getRegion();
// Get zone-specific DNS names for the given region so that we can get a
// list of available zones
Map<String, List<String>> zoneDnsNamesMap = getZoneBasedDiscoveryUrlsFromRegion(region);
Set<String> availableZones = zoneDnsNamesMap.keySet();
List<String> zones = new ArrayList<String>(availableZones);
if (zones.isEmpty()) {
throw new RuntimeException("No available zones configured for the instanceZone " + instanceZone);
}
int zoneIndex = 0;
boolean zoneFound = false;
for (String zone : zones) {
logger.debug(
"Checking if the instance zone {} is the same as the zone from DNS {}",
instanceZone, zone);
if (preferSameZone) {
if (instanceZone.equalsIgnoreCase(zone)) {
zoneFound = true;
}
} else {
if (!instanceZone.equalsIgnoreCase(zone)) {
zoneFound = true;
}
}
if (zoneFound) {
Object[] args = {zones, instanceZone, zoneIndex};
logger.debug(
"The zone index from the list {} that matches the instance zone {} is {}",
args);
break;
}
zoneIndex++;
}
if (zoneIndex >= zones.size()) {
logger.warn(
"No match for the zone {} in the list of available zones {}",
instanceZone, Arrays.toString(zones.toArray()));
} else {
// Rearrange the zones with the instance zone first
for (int i = 0; i < zoneIndex; i++) {
String zone = zones.remove(0);
zones.add(zone);
}
}
// Now get the eureka urls for all the zones in the order and return it
List<String> serviceUrls = new ArrayList<String>();
for (String zone : zones) {
for (String zoneCname : zoneDnsNamesMap.get(zone)) {
List<String> ec2Urls = new ArrayList<String>(
getEC2DiscoveryUrlsFromZone(zoneCname,
DiscoveryUrlType.CNAME));
// Rearrange the list to distribute the load in case of
// multiple servers
if (ec2Urls.size() > 1) {
this.arrangeListBasedonHostname(ec2Urls);
}
for (String ec2Url : ec2Urls) {
String serviceUrl = "http://" + ec2Url + ":"
+ clientConfig.getEurekaServerPort()
+ "/" + clientConfig.getEurekaServerURLContext()
+ "/";
logger.debug("The EC2 url is {}", serviceUrl);
serviceUrls.add(serviceUrl);
}
}
}
// Rearrange the fail over server list to distribute the load
String primaryServiceUrl = serviceUrls.remove(0);
arrangeListBasedonHostname(serviceUrls);
serviceUrls.add(0, primaryServiceUrl);
logger.debug(
"This client will talk to the following serviceUrls in order : {} ",
Arrays.toString(serviceUrls.toArray()));
t.stop();
return serviceUrls;
}