use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class ApplicationsXmlBeanSerializer method serializeFields.
@Override
protected void serializeFields(Object bean, JsonGenerator jgen0, SerializerProvider provider) throws IOException {
super.serializeFields(bean, jgen0, provider);
Applications applications = (Applications) bean;
if (applications.getVersion() != null) {
jgen0.writeStringField(versionKey, Long.toString(applications.getVersion()));
}
if (applications.getAppsHashCode() != null) {
jgen0.writeStringField(appsHashCodeKey, applications.getAppsHashCode());
}
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class DiscoveryClient method fetchRegistryFromBackup.
/**
* Fetch the registry information from back up registry if all eureka server
* urls are unreachable.
*/
private void fetchRegistryFromBackup() {
try {
@SuppressWarnings("deprecation") BackupRegistry backupRegistryInstance = newBackupRegistryInstance();
if (null == backupRegistryInstance) {
// backward compatibility with the old protected method, in case it is being used.
backupRegistryInstance = backupRegistryProvider.get();
}
if (null != backupRegistryInstance) {
Applications apps = null;
if (isFetchingRemoteRegionRegistries()) {
String remoteRegionsStr = remoteRegionsToFetch.get();
if (null != remoteRegionsStr) {
apps = backupRegistryInstance.fetchRegistry(remoteRegionsStr.split(","));
}
} else {
apps = backupRegistryInstance.fetchRegistry();
}
if (apps != null) {
final Applications applications = this.filterAndShuffle(apps);
applications.setAppsHashCode(applications.getReconcileHashCode());
localRegionApps.set(applications);
logTotalInstances();
logger.info("Fetched registry successfully from the backup");
}
} else {
logger.warn("No backup registry instance defined & unable to find any discovery servers.");
}
} catch (Throwable e) {
logger.warn("Cannot fetch applications from apps although backup registry was specified", e);
}
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class DiscoveryClient method reconcileAndLogDifference.
/**
* Reconcile the eureka server and client registry information and logs the differences if any.
* When reconciling, the following flow is observed:
*
* make a remote call to the server for the full registry
* calculate and log differences
* if (update generation have not advanced (due to another thread))
* atomically set the registry to the new registry
* fi
*
* @param delta
* the last delta registry information received from the eureka
* server.
* @param reconcileHashCode
* the hashcode generated by the server for reconciliation.
* @return ClientResponse the HTTP response object.
* @throws Throwable
* on any error.
*/
private void reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable {
logger.debug("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry", reconcileHashCode, delta.getAppsHashCode());
RECONCILE_HASH_CODES_MISMATCH.increment();
long currentUpdateGeneration = fetchRegistryGeneration.get();
EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null ? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get()) : eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
Applications serverApps = httpResponse.getEntity();
if (serverApps == null) {
logger.warn("Cannot fetch full registry from the server; reconciliation failure");
return;
}
if (logger.isDebugEnabled()) {
try {
Map<String, List<String>> reconcileDiffMap = getApplications().getReconcileMapDiff(serverApps);
StringBuilder reconcileBuilder = new StringBuilder("");
for (Map.Entry<String, List<String>> mapEntry : reconcileDiffMap.entrySet()) {
reconcileBuilder.append(mapEntry.getKey()).append(": ");
for (String displayString : mapEntry.getValue()) {
reconcileBuilder.append(displayString);
}
reconcileBuilder.append('\n');
}
String reconcileString = reconcileBuilder.toString();
logger.debug("The reconcile string is {}", reconcileString);
} catch (Throwable e) {
logger.error("Could not calculate reconcile string ", e);
}
}
if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
localRegionApps.set(this.filterAndShuffle(serverApps));
getApplications().setVersion(delta.getVersion());
logger.debug("The Reconcile hashcodes after complete sync up, client : {}, server : {}.", getApplications().getReconcileHashCode(), delta.getAppsHashCode());
} else {
logger.warn("Not setting the applications map as another thread has advanced the update generation");
}
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class AbstractJersey2EurekaHttpClient method getApplicationsInternal.
private EurekaHttpResponse<Applications> getApplicationsInternal(String urlPath, String[] regions) {
Response response = null;
try {
WebTarget webTarget = jerseyClient.target(serviceUrl).path(urlPath);
if (regions != null && regions.length > 0) {
webTarget = webTarget.queryParam("regions", StringUtil.join(regions));
}
Builder requestBuilder = webTarget.request();
addExtraProperties(requestBuilder);
addExtraHeaders(requestBuilder);
response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get();
Applications applications = null;
if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
applications = response.readEntity(Applications.class);
}
return anEurekaHttpResponse(response.getStatus(), applications).headers(headersOf(response)).build();
} finally {
if (logger.isDebugEnabled()) {
logger.debug("Jersey2 HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
}
if (response != null) {
response.close();
}
}
}
use of com.netflix.discovery.shared.Applications in project eureka by Netflix.
the class EurekaEventListenerTest method testCacheRefreshEvent.
@Test
public void testCacheRefreshEvent() throws Exception {
CapturingEurekaEventListener listener = new CapturingEurekaEventListener();
Applications initialApps = toApplications(discoveryClientResource.getMyInstanceInfo());
when(requestHandler.getApplications()).thenReturn(anEurekaHttpResponse(200, initialApps).type(MediaType.APPLICATION_JSON_TYPE).build());
DiscoveryClient client = (DiscoveryClient) discoveryClientResource.getClient();
client.registerEventListener(listener);
client.refreshRegistry();
assertNotNull(listener.event);
assertThat(listener.event, is(instanceOf(CacheRefreshedEvent.class)));
}
Aggregations