* on any error.
*/
private ClientResponse makeRemoteCall(Action action, int serviceUrlIndex)
throws Throwable {
String urlPath = null;
Stopwatch tracer = null;
String serviceUrl = eurekaServiceUrls.get().get(serviceUrlIndex);
ClientResponse response = null;
logger.debug("Discovery Client talking to the server {}", serviceUrl);
try {
// If the application is unknown do not register/renew/cancel but
// refresh
if ((UNKNOWN.equals(instanceInfo.getAppName())
&& (!Action.Refresh.equals(action)) && (!Action.Refresh_Delta
.equals(action)))) {
return null;
}
WebResource r = discoveryApacheClient.resource(serviceUrl);
String remoteRegionsToFetchStr;
switch (action) {
case Renew:
tracer = RENEW_TIMER.start();
urlPath = "apps/" + appPathIdentifier;
response = r
.path(urlPath)
.queryParam("status",
instanceInfo.getStatus().toString())
.queryParam("lastDirtyTimestamp",
instanceInfo.getLastDirtyTimestamp().toString())
.put(ClientResponse.class);
break;
case Refresh:
tracer = REFRESH_TIMER.start();
final String vipAddress = clientConfig.getRegistryRefreshSingleVipAddress();
urlPath = vipAddress == null ? "apps/" : "vips/" + vipAddress;
remoteRegionsToFetchStr = remoteRegionsToFetch.get();
if (!Strings.isNullOrEmpty(remoteRegionsToFetchStr)) {
urlPath += "?regions=" + remoteRegionsToFetchStr;
}
response = getUrl(serviceUrl + urlPath);
break;
case Refresh_Delta:
tracer = REFRESH_DELTA_TIMER.start();
urlPath = "apps/delta";
remoteRegionsToFetchStr = remoteRegionsToFetch.get();
if (!Strings.isNullOrEmpty(remoteRegionsToFetchStr)) {
urlPath += "?regions=" + remoteRegionsToFetchStr;
}
response = getUrl(serviceUrl + urlPath);
break;
case Register:
tracer = REGISTER_TIMER.start();
urlPath = "apps/" + instanceInfo.getAppName();
response = r.path(urlPath)
.type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, instanceInfo);
break;
case Cancel:
tracer = CANCEL_TIMER.start();
urlPath = "apps/" + appPathIdentifier;
response = r.path(urlPath).delete(ClientResponse.class);
// Return without during de-registration if it is not registered
// already and if we get a 404
if ((!isRegisteredWithDiscovery)
&& (response.getStatus() == Status.NOT_FOUND
.getStatusCode())) {
return response;
}
break;
}
if (logger.isDebugEnabled()) {
logger.debug("Finished a call to service url {} and url path {} with status code {}.",
new String[] {serviceUrl, urlPath, String.valueOf(response.getStatus())});
}
if (isOk(action, response.getStatus())) {
return response;
} else {
logger.warn("Action: " + action + " => returned status of "
+ response.getStatus() + " from " + serviceUrl
+ urlPath);
throw new RuntimeException("Bad status: "
+ response.getStatus());
}
} catch (Throwable t) {
closeResponse(response);
String msg = "Can't get a response from " + serviceUrl + urlPath;
if (eurekaServiceUrls.get().size() > (++serviceUrlIndex)) {
logger.warn(msg, t);
logger.warn("Trying backup: "
+ eurekaServiceUrls.get().get(serviceUrlIndex));
SERVER_RETRY_COUNTER.increment();
return makeRemoteCall(action, serviceUrlIndex);
} else {
ALL_SERVER_FAILURE_COUNT.increment();
logger.error(
msg
+ "\nCan't contact any eureka nodes - possibly a security group issue?",
t);
throw t;
}
} finally {
if (tracer != null) {
tracer.stop();
}
}
}