Search in sources :

Example 1 with AmazonInfo

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

the class InstanceRegionChecker method getInstanceRegion.

@Nullable
public String getInstanceRegion(InstanceInfo instanceInfo) {
    if (instanceInfo.getDataCenterInfo() == null || instanceInfo.getDataCenterInfo().getName() == null) {
        logger.warn("Cannot get region for instance id:{}, app:{} as dataCenterInfo is null. Returning local:{} by default", instanceInfo.getId(), instanceInfo.getAppName(), localRegion);
        return localRegion;
    }
    if (DataCenterInfo.Name.Amazon.equals(instanceInfo.getDataCenterInfo().getName())) {
        AmazonInfo amazonInfo = (AmazonInfo) instanceInfo.getDataCenterInfo();
        Map<String, String> metadata = amazonInfo.getMetadata();
        String availabilityZone = metadata.get(AmazonInfo.MetaDataKey.availabilityZone.getName());
        if (null != availabilityZone) {
            return azToRegionMapper.getRegionForAvailabilityZone(availabilityZone);
        }
    }
    return null;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) Nullable(javax.annotation.Nullable)

Example 2 with AmazonInfo

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

the class Ec2EurekaClientModuleTest method testDI.

@SuppressWarnings("deprecation")
@Test
public void testDI() {
    InstanceInfo instanceInfo = injector.getInstance(InstanceInfo.class);
    Assert.assertEquals(ApplicationInfoManager.getInstance().getInfo(), instanceInfo);
    VipAddressResolver vipAddressResolver = injector.getInstance(VipAddressResolver.class);
    Assert.assertTrue(vipAddressResolver instanceof Archaius2VipAddressResolver);
    EurekaClient eurekaClient = injector.getInstance(EurekaClient.class);
    DiscoveryClient discoveryClient = injector.getInstance(DiscoveryClient.class);
    Assert.assertEquals(DiscoveryManager.getInstance().getEurekaClient(), eurekaClient);
    Assert.assertEquals(DiscoveryManager.getInstance().getDiscoveryClient(), discoveryClient);
    Assert.assertEquals(eurekaClient, discoveryClient);
    EurekaClientConfig eurekaClientConfig = injector.getInstance(EurekaClientConfig.class);
    Assert.assertEquals(DiscoveryManager.getInstance().getEurekaClientConfig(), eurekaClientConfig);
    EurekaInstanceConfig eurekaInstanceConfig = injector.getInstance(EurekaInstanceConfig.class);
    Assert.assertEquals(DiscoveryManager.getInstance().getEurekaInstanceConfig(), eurekaInstanceConfig);
    Assert.assertTrue(eurekaInstanceConfig instanceof Ec2EurekaArchaius2InstanceConfig);
    ApplicationInfoManager applicationInfoManager = injector.getInstance(ApplicationInfoManager.class);
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    Assert.assertTrue(myInfo.getDataCenterInfo() instanceof AmazonInfo);
    Assert.assertEquals(DataCenterInfo.Name.Amazon, myInfo.getDataCenterInfo().getName());
}
Also used : EurekaClientConfig(com.netflix.discovery.EurekaClientConfig) Ec2EurekaArchaius2InstanceConfig(com.netflix.appinfo.Ec2EurekaArchaius2InstanceConfig) Archaius2VipAddressResolver(com.netflix.appinfo.providers.Archaius2VipAddressResolver) VipAddressResolver(com.netflix.appinfo.providers.VipAddressResolver) EurekaInstanceConfig(com.netflix.appinfo.EurekaInstanceConfig) EurekaClient(com.netflix.discovery.EurekaClient) DiscoveryClient(com.netflix.discovery.DiscoveryClient) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Archaius2VipAddressResolver(com.netflix.appinfo.providers.Archaius2VipAddressResolver) ApplicationInfoManager(com.netflix.appinfo.ApplicationInfoManager) Test(org.junit.Test)

Example 3 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo 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 4 with AmazonInfo

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

the class EurekaUtils method isInVpc.

/**
     * check to see if the instanceInfo record is of a server that is deployed within EC2 VPC (best effort check
     * based on assumption of existing vpcId). This check could be for the local server or a remote server.
     *
     * @param instanceInfo
     * @return true if the record contains a non null, non empty AWS vpcId
     */
public static boolean isInVpc(InstanceInfo instanceInfo) {
    if (instanceInfo.getDataCenterInfo() instanceof AmazonInfo) {
        AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo();
        String vpcId = info.get(AmazonInfo.MetaDataKey.vpcId);
        return !isNullOrEmpty(vpcId);
    }
    return false;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo)

Example 5 with AmazonInfo

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

the class InstanceRegionCheckerTest method testDefaults.

@Test
public void testDefaults() throws Exception {
    PropertyBasedAzToRegionMapper azToRegionMapper = new PropertyBasedAzToRegionMapper(new DefaultEurekaClientConfig());
    InstanceRegionChecker checker = new InstanceRegionChecker(azToRegionMapper, "us-east-1");
    azToRegionMapper.setRegionsToFetch(new String[] { "us-east-1" });
    AmazonInfo dcInfo = AmazonInfo.Builder.newBuilder().addMetadata(AmazonInfo.MetaDataKey.availabilityZone, "us-east-1c").build();
    InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("app").setDataCenterInfo(dcInfo).build();
    String instanceRegion = checker.getInstanceRegion(instanceInfo);
    Assert.assertEquals("Invalid instance region.", "us-east-1", instanceRegion);
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Aggregations

AmazonInfo (com.netflix.appinfo.AmazonInfo)30 InstanceInfo (com.netflix.appinfo.InstanceInfo)21 Test (org.junit.Test)7 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)6 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)4 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)2 Builder (com.netflix.appinfo.InstanceInfo.Builder)2 DiscoveryClient (com.netflix.discovery.DiscoveryClient)2 Address (com.amazonaws.services.ec2.model.Address)1 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 Ec2EurekaArchaius2InstanceConfig (com.netflix.appinfo.Ec2EurekaArchaius2InstanceConfig)1 EurekaInstanceConfig (com.netflix.appinfo.EurekaInstanceConfig)1 LeaseInfo (com.netflix.appinfo.LeaseInfo)1 MyDataCenterInstanceConfig (com.netflix.appinfo.MyDataCenterInstanceConfig)1 UniqueIdentifier (com.netflix.appinfo.UniqueIdentifier)1 Archaius2VipAddressResolver (com.netflix.appinfo.providers.Archaius2VipAddressResolver)1 VipAddressResolver (com.netflix.appinfo.providers.VipAddressResolver)1 DefaultEurekaClientConfig (com.netflix.discovery.DefaultEurekaClientConfig)1