use of com.netflix.discovery.shared.Application in project eureka by Netflix.
the class AbstractInstanceRegistry method getApplicationsFromMultipleRegions.
/**
* This method will return applications with instances from all passed remote regions as well as the current region.
* Thus, this gives a union view of instances from multiple regions. <br/>
* The application instances for which this union will be done can be restricted to the names returned by
* {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} for every region. In case, there is no whitelist
* defined for a region, this method will also look for a global whitelist by passing <code>null</code> to the
* method {@link EurekaServerConfig#getRemoteRegionAppWhitelist(String)} <br/>
* If you are not selectively requesting for a remote region, use {@link #getApplicationsFromAllRemoteRegions()}
* or {@link #getApplicationsFromLocalRegionOnly()}
*
* @param remoteRegions The remote regions for which the instances are to be queried. The instances may be limited
* by a whitelist as explained above. If <code>null</code> or empty no remote regions are
* included.
*
* @return The applications with instances from the passed remote regions as well as local region. The instances
* from remote regions can be only for certain whitelisted apps as explained above.
*/
public Applications getApplicationsFromMultipleRegions(String[] remoteRegions) {
boolean includeRemoteRegion = null != remoteRegions && remoteRegions.length != 0;
logger.debug("Fetching applications registry with remote regions: {}, Regions argument {}", includeRemoteRegion, Arrays.toString(remoteRegions));
if (includeRemoteRegion) {
GET_ALL_WITH_REMOTE_REGIONS_CACHE_MISS.increment();
} else {
GET_ALL_CACHE_MISS.increment();
}
Applications apps = new Applications();
apps.setVersion(1L);
for (Entry<String, Map<String, Lease<InstanceInfo>>> entry : registry.entrySet()) {
Application app = null;
if (entry.getValue() != null) {
for (Entry<String, Lease<InstanceInfo>> stringLeaseEntry : entry.getValue().entrySet()) {
Lease<InstanceInfo> lease = stringLeaseEntry.getValue();
if (app == null) {
app = new Application(lease.getHolder().getAppName());
}
app.addInstance(decorateInstanceInfo(lease));
}
}
if (app != null) {
apps.addApplication(app);
}
}
if (includeRemoteRegion) {
for (String remoteRegion : remoteRegions) {
RemoteRegionRegistry remoteRegistry = regionNameVSRemoteRegistry.get(remoteRegion);
if (null != remoteRegistry) {
Applications remoteApps = remoteRegistry.getApplications();
for (Application application : remoteApps.getRegisteredApplications()) {
if (shouldFetchFromRemoteRegistry(application.getName(), remoteRegion)) {
logger.info("Application {} fetched from the remote region {}", application.getName(), remoteRegion);
Application appInstanceTillNow = apps.getRegisteredApplications(application.getName());
if (appInstanceTillNow == null) {
appInstanceTillNow = new Application(application.getName());
apps.addApplication(appInstanceTillNow);
}
for (InstanceInfo instanceInfo : application.getInstances()) {
appInstanceTillNow.addInstance(instanceInfo);
}
} else {
logger.debug("Application {} not fetched from the remote region {} as there exists a " + "whitelist and this app is not in the whitelist.", application.getName(), remoteRegion);
}
}
} else {
logger.warn("No remote registry available for the remote region {}", remoteRegion);
}
}
}
apps.setAppsHashCode(apps.getReconcileHashCode());
return apps;
}
use of com.netflix.discovery.shared.Application in project eureka by Netflix.
the class InstanceRegistryTest method testGetAppsFromBothRegions.
@Test
public void testGetAppsFromBothRegions() throws Exception {
registerInstanceLocally(createRemoteInstance(LOCAL_REGION_INSTANCE_2_HOSTNAME));
registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME));
Applications apps = registry.getApplicationsFromAllRemoteRegions();
List<Application> registeredApplications = apps.getRegisteredApplications();
Assert.assertEquals("Apps size from both regions do not match", 2, registeredApplications.size());
Application locaApplication = null;
Application remApplication = null;
for (Application registeredApplication : registeredApplications) {
if (registeredApplication.getName().equalsIgnoreCase(LOCAL_REGION_APP_NAME)) {
locaApplication = registeredApplication;
}
if (registeredApplication.getName().equalsIgnoreCase(REMOTE_REGION_APP_NAME)) {
remApplication = registeredApplication;
}
}
Assert.assertNotNull("Did not find local registry app", locaApplication);
Assert.assertEquals("Local registry app instance count not as expected.", 1, locaApplication.getInstances().size());
Assert.assertNotNull("Did not find remote registry app", remApplication);
Assert.assertEquals("Remote registry app instance count not as expected.", 2, remApplication.getInstances().size());
}
use of com.netflix.discovery.shared.Application in project eureka by Netflix.
the class InstanceRegistryTest method testGetAppsDeltaFromAllRemoteRegions.
@Test
public void testGetAppsDeltaFromAllRemoteRegions() throws Exception {
/// local delta
registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_2_HOSTNAME));
waitForDeltaToBeRetrieved();
Applications appDelta = registry.getApplicationDeltasFromMultipleRegions(null);
List<Application> registeredApplications = appDelta.getRegisteredApplications();
Assert.assertEquals("Apps size from remote regions do not match", 2, registeredApplications.size());
Application localApplication = null;
Application remApplication = null;
for (Application registeredApplication : registeredApplications) {
if (registeredApplication.getName().equalsIgnoreCase(LOCAL_REGION_APP_NAME)) {
localApplication = registeredApplication;
}
if (registeredApplication.getName().equalsIgnoreCase(REMOTE_REGION_APP_NAME)) {
remApplication = registeredApplication;
}
}
Assert.assertNotNull("Did not find local registry app in delta.", localApplication);
Assert.assertEquals("Local registry app instance count in delta not as expected.", 1, localApplication.getInstances().size());
Assert.assertNotNull("Did not find remote registry app in delta", remApplication);
Assert.assertEquals("Remote registry app instance count in delta not as expected.", 1, remApplication.getInstances().size());
}
use of com.netflix.discovery.shared.Application in project eureka by Netflix.
the class InstanceRegistryTest method testGetAppsFromLocalRegionOnly.
@Test
public void testGetAppsFromLocalRegionOnly() throws Exception {
registerInstanceLocally(createLocalInstance(LOCAL_REGION_INSTANCE_1_HOSTNAME));
Applications apps = registry.getApplicationsFromLocalRegionOnly();
List<Application> registeredApplications = apps.getRegisteredApplications();
Assert.assertEquals("Apps size from local region do not match", 1, registeredApplications.size());
Application app = registeredApplications.iterator().next();
Assert.assertEquals("Added app did not return from local registry", LOCAL_REGION_APP_NAME, app.getName());
Assert.assertEquals("Returned app did not have the instance", 1, app.getInstances().size());
}
use of com.netflix.discovery.shared.Application in project eureka by Netflix.
the class AbstractVIPResourceTest method testMiniVipGet.
@Test
public void testMiniVipGet() throws Exception {
Response response = resource.getVipResponse(Version.V2.name(), vipName, MediaType.APPLICATION_JSON, EurekaAccept.compact, Key.EntityType.VIP);
String json = String.valueOf(response.getEntity());
DecoderWrapper decoder = CodecWrappers.getDecoder(CodecWrappers.LegacyJacksonJson.class);
Applications decodedApps = decoder.decode(json, Applications.class);
Application decodedApp = decodedApps.getRegisteredApplications(testApplication.getName());
// assert false as one is mini, so should NOT equal
assertThat(EurekaEntityComparators.equal(testApplication, decodedApp), is(false));
for (InstanceInfo instanceInfo : testApplication.getInstances()) {
InstanceInfo decodedInfo = decodedApp.getByInstanceId(instanceInfo.getId());
assertThat(EurekaEntityComparators.equalMini(instanceInfo, decodedInfo), is(true));
}
}
Aggregations