Search in sources :

Example 1 with DataCenterInfo

use of com.netflix.appinfo.DataCenterInfo in project eureka by Netflix.

the class DiscoveryClientRegistryTest method testApplyDeltaWithBadInstanceInfoDataCenterInfoAsNull.

@Test
public void testApplyDeltaWithBadInstanceInfoDataCenterInfoAsNull() throws Exception {
    InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(2, "testApp").build();
    // Full fetch with one item
    InstanceInfo first = instanceGen.first();
    Applications initial = toApplications(first);
    when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(anEurekaHttpResponse(200, initial).type(MediaType.APPLICATION_JSON_TYPE).build());
    EurekaClient client = discoveryClientResource.getClient();
    assertThat(client.getApplications().getAppsHashCode(), is(equalTo("UP_1_")));
    // Delta with one add
    InstanceInfo second = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo1").setStatus(InstanceStatus.DOWN).setDataCenterInfo(null).build();
    InstanceInfo third = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo2").setStatus(InstanceStatus.UP).setDataCenterInfo(new DataCenterInfo() {

        @Override
        public Name getName() {
            return null;
        }
    }).build();
    Applications delta = toApplications(second, third);
    delta.setAppsHashCode("DOWN_1_UP_2_");
    when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(anEurekaHttpResponse(200, delta).type(MediaType.APPLICATION_JSON_TYPE).build());
    assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
    assertThat(client.getApplications().getAppsHashCode(), is(equalTo("DOWN_1_UP_2_")));
}
Also used : Applications(com.netflix.discovery.shared.Applications) EurekaEntityFunctions.toApplications(com.netflix.discovery.util.EurekaEntityFunctions.toApplications) EurekaEntityFunctions.mergeApplications(com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications) EurekaEntityFunctions.copyApplications(com.netflix.discovery.util.EurekaEntityFunctions.copyApplications) InstanceInfoGenerator(com.netflix.discovery.util.InstanceInfoGenerator) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 2 with DataCenterInfo

use of com.netflix.appinfo.DataCenterInfo in project eureka by Netflix.

the class ResolverUtils method instanceInfoToEndpoint.

public static AwsEndpoint instanceInfoToEndpoint(EurekaClientConfig clientConfig, EurekaTransportConfig transportConfig, InstanceInfo instanceInfo) {
    String zone = null;
    DataCenterInfo dataCenterInfo = instanceInfo.getDataCenterInfo();
    if (dataCenterInfo instanceof AmazonInfo) {
        zone = ((AmazonInfo) dataCenterInfo).get(AmazonInfo.MetaDataKey.availabilityZone);
    }
    String networkAddress;
    if (transportConfig.applicationsResolverUseIp()) {
        if (instanceInfo.getDataCenterInfo() instanceof AmazonInfo) {
            networkAddress = ((AmazonInfo) instanceInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.localIpv4);
        } else {
            networkAddress = instanceInfo.getIPAddr();
        }
    } else {
        networkAddress = instanceInfo.getHostName();
    }
    if (networkAddress == null) {
        // final check
        logger.error("Cannot resolve InstanceInfo {} to a proper resolver endpoint, skipping", instanceInfo);
        return null;
    }
    return new AwsEndpoint(networkAddress, instanceInfo.getPort(), false, clientConfig.getEurekaServerURLContext(), clientConfig.getRegion(), zone);
}
Also used : AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) AmazonInfo(com.netflix.appinfo.AmazonInfo)

Example 3 with DataCenterInfo

use of com.netflix.appinfo.DataCenterInfo in project eureka by Netflix.

the class DiscoveryClientDisableRegistryTest method setUp.

@Before
public void setUp() throws Exception {
    mockLocalEurekaServer = new MockRemoteEurekaServer();
    mockLocalEurekaServer.start();
    ConfigurationManager.getConfigInstance().setProperty("eureka.registration.enabled", "false");
    ConfigurationManager.getConfigInstance().setProperty("eureka.shouldFetchRegistry", "false");
    ConfigurationManager.getConfigInstance().setProperty("eureka.serviceUrl.default", "http://localhost:" + mockLocalEurekaServer.getPort() + MockRemoteEurekaServer.EUREKA_API_BASE_PATH);
    InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder();
    builder.setIPAddr("10.10.101.00");
    builder.setHostName("Hosttt");
    builder.setAppName("EurekaTestApp-" + UUID.randomUUID());
    builder.setDataCenterInfo(new DataCenterInfo() {

        @Override
        public Name getName() {
            return Name.MyOwn;
        }
    });
    ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(new MyDataCenterInstanceConfig(), builder.build());
    client = new DiscoveryClient(applicationInfoManager, new DefaultEurekaClientConfig());
}
Also used : MyDataCenterInstanceConfig(com.netflix.appinfo.MyDataCenterInstanceConfig) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) ApplicationInfoManager(com.netflix.appinfo.ApplicationInfoManager) Before(org.junit.Before)

Example 4 with DataCenterInfo

use of com.netflix.appinfo.DataCenterInfo in project eureka by Netflix.

the class EurekaUtilsTest method testIsInVpc.

@Test
public void testIsInVpc() {
    InstanceInfo instanceInfo1 = new InstanceInfo.Builder(InstanceInfoGenerator.takeOne()).setDataCenterInfo(new DataCenterInfo() {

        @Override
        public Name getName() {
            return Name.MyOwn;
        }
    }).build();
    Assert.assertFalse(EurekaUtils.isInVpc(instanceInfo1));
    InstanceInfo instanceInfo2 = InstanceInfoGenerator.takeOne();
    Assert.assertFalse(EurekaUtils.isInVpc(instanceInfo2));
    InstanceInfo instanceInfo3 = InstanceInfoGenerator.takeOne();
    ((AmazonInfo) instanceInfo3.getDataCenterInfo()).getMetadata().put(AmazonInfo.MetaDataKey.vpcId.getName(), "vpc-123456");
    Assert.assertTrue(EurekaUtils.isInVpc(instanceInfo3));
}
Also used : DataCenterInfo(com.netflix.appinfo.DataCenterInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 5 with DataCenterInfo

use of com.netflix.appinfo.DataCenterInfo in project eureka by Netflix.

the class ApplicationResource method addInstance.

/**
     * Registers information about a particular instance for an
     * {@link com.netflix.discovery.shared.Application}.
     *
     * @param info
     *            {@link InstanceInfo} information of the instance.
     * @param isReplication
     *            a header parameter containing information whether this is
     *            replicated from other nodes.
     */
@POST
@Consumes({ "application/json", "application/xml" })
public Response addInstance(InstanceInfo info, @HeaderParam(PeerEurekaNode.HEADER_REPLICATION) String isReplication) {
    logger.debug("Registering instance {} (replication={})", info.getId(), isReplication);
    // validate that the instanceinfo contains all the necessary required fields
    if (isBlank(info.getId())) {
        return Response.status(400).entity("Missing instanceId").build();
    } else if (isBlank(info.getHostName())) {
        return Response.status(400).entity("Missing hostname").build();
    } else if (isBlank(info.getAppName())) {
        return Response.status(400).entity("Missing appName").build();
    } else if (!appName.equals(info.getAppName())) {
        return Response.status(400).entity("Mismatched appName, expecting " + appName + " but was " + info.getAppName()).build();
    } else if (info.getDataCenterInfo() == null) {
        return Response.status(400).entity("Missing dataCenterInfo").build();
    } else if (info.getDataCenterInfo().getName() == null) {
        return Response.status(400).entity("Missing dataCenterInfo Name").build();
    }
    // handle cases where clients may be registering with bad DataCenterInfo with missing data
    DataCenterInfo dataCenterInfo = info.getDataCenterInfo();
    if (dataCenterInfo instanceof UniqueIdentifier) {
        String dataCenterInfoId = ((UniqueIdentifier) dataCenterInfo).getId();
        if (isBlank(dataCenterInfoId)) {
            boolean experimental = "true".equalsIgnoreCase(serverConfig.getExperimental("registration.validation.dataCenterInfoId"));
            if (experimental) {
                String entity = "DataCenterInfo of type " + dataCenterInfo.getClass() + " must contain a valid id";
                return Response.status(400).entity(entity).build();
            } else if (dataCenterInfo instanceof AmazonInfo) {
                AmazonInfo amazonInfo = (AmazonInfo) dataCenterInfo;
                String effectiveId = amazonInfo.get(AmazonInfo.MetaDataKey.instanceId);
                if (effectiveId == null) {
                    amazonInfo.getMetadata().put(AmazonInfo.MetaDataKey.instanceId.getName(), info.getId());
                }
            } else {
                logger.warn("Registering DataCenterInfo of type {} without an appropriate id", dataCenterInfo.getClass());
            }
        }
    }
    registry.register(info, "true".equals(isReplication));
    // 204 to be backwards compatible
    return Response.status(204).build();
}
Also used : UniqueIdentifier(com.netflix.appinfo.UniqueIdentifier) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) AmazonInfo(com.netflix.appinfo.AmazonInfo) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

DataCenterInfo (com.netflix.appinfo.DataCenterInfo)16 InstanceInfo (com.netflix.appinfo.InstanceInfo)10 AmazonInfo (com.netflix.appinfo.AmazonInfo)5 Test (org.junit.Test)5 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)2 MyDataCenterInstanceConfig (com.netflix.appinfo.MyDataCenterInstanceConfig)2 UniqueIdentifier (com.netflix.appinfo.UniqueIdentifier)2 Before (org.junit.Before)2 HealthCheckHandler (com.netflix.appinfo.HealthCheckHandler)1 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)1 LeaseInfo (com.netflix.appinfo.LeaseInfo)1 RefreshableInstanceConfig (com.netflix.appinfo.RefreshableInstanceConfig)1 Applications (com.netflix.discovery.shared.Applications)1 AwsEndpoint (com.netflix.discovery.shared.resolver.aws.AwsEndpoint)1 EurekaEntityFunctions.copyApplications (com.netflix.discovery.util.EurekaEntityFunctions.copyApplications)1 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)1 EurekaEntityFunctions.toApplications (com.netflix.discovery.util.EurekaEntityFunctions.toApplications)1 InstanceInfoGenerator (com.netflix.discovery.util.InstanceInfoGenerator)1 Map (java.util.Map)1 Consumes (javax.ws.rs.Consumes)1