Search in sources :

Example 1 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint 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);
    } else {
        zone = instanceInfo.getMetadata().get("zone");
    }
    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 2 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint in project eureka by Netflix.

the class ResolverUtils method splitByZone.

/**
 * @return returns two element array with first item containing list of endpoints from client's zone,
 *         and in the second list all the remaining ones
 */
public static List<AwsEndpoint>[] splitByZone(List<AwsEndpoint> eurekaEndpoints, String myZone) {
    if (eurekaEndpoints.isEmpty()) {
        return new List[] { Collections.emptyList(), Collections.emptyList() };
    }
    if (myZone == null) {
        return new List[] { Collections.emptyList(), new ArrayList<>(eurekaEndpoints) };
    }
    List<AwsEndpoint> myZoneList = new ArrayList<>(eurekaEndpoints.size());
    List<AwsEndpoint> remainingZonesList = new ArrayList<>(eurekaEndpoints.size());
    for (AwsEndpoint endpoint : eurekaEndpoints) {
        if (myZone.equalsIgnoreCase(endpoint.getZone())) {
            myZoneList.add(endpoint);
        } else {
            remainingZonesList.add(endpoint);
        }
    }
    return new List[] { myZoneList, remainingZonesList };
}
Also used : AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint in project eureka by Netflix.

the class EurekaHttpClients method defaultBootstrapResolver.

/**
 * @return a bootstrap resolver that resolves eureka server endpoints based on either DNS or static config,
 *         depending on configuration for one or the other. This resolver will warm up at the start.
 */
static ClosableResolver<AwsEndpoint> defaultBootstrapResolver(final EurekaClientConfig clientConfig, final InstanceInfo myInstanceInfo, final EndpointRandomizer randomizer) {
    String[] availZones = clientConfig.getAvailabilityZones(clientConfig.getRegion());
    String myZone = InstanceInfo.getZone(availZones, myInstanceInfo);
    ClusterResolver<AwsEndpoint> delegateResolver = new ZoneAffinityClusterResolver(new ConfigClusterResolver(clientConfig, myInstanceInfo), myZone, true, randomizer);
    List<AwsEndpoint> initialValue = delegateResolver.getClusterEndpoints();
    if (initialValue.isEmpty()) {
        String msg = "Initial resolution of Eureka server endpoints failed. Check ConfigClusterResolver logs for more info";
        logger.error(msg);
        failFastOnInitCheck(clientConfig, msg);
    }
    return new AsyncResolver<>(EurekaClientNames.BOOTSTRAP, delegateResolver, initialValue, 1, clientConfig.getEurekaServiceUrlPollIntervalSeconds() * 1000);
}
Also used : AsyncResolver(com.netflix.discovery.shared.resolver.AsyncResolver) AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) ConfigClusterResolver(com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver) ZoneAffinityClusterResolver(com.netflix.discovery.shared.resolver.aws.ZoneAffinityClusterResolver)

Example 4 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint in project eureka by Netflix.

the class EurekaHttpClients method compositeQueryResolver.

/**
 * @return a composite resolver that resolves eureka server endpoints for query operations, given two resolvers:
 *         a resolver that can resolve targets via a remote call to a remote source, and a resolver that
 *         can resolve targets via data in the local registry.
 */
/* testing */
static ClosableResolver<AwsEndpoint> compositeQueryResolver(final ClusterResolver<AwsEndpoint> remoteResolver, final ClusterResolver<AwsEndpoint> localResolver, final EurekaClientConfig clientConfig, final EurekaTransportConfig transportConfig, final InstanceInfo myInstanceInfo, final EndpointRandomizer randomizer) {
    String[] availZones = clientConfig.getAvailabilityZones(clientConfig.getRegion());
    String myZone = InstanceInfo.getZone(availZones, myInstanceInfo);
    ClusterResolver<AwsEndpoint> compositeResolver = new ClusterResolver<AwsEndpoint>() {

        @Override
        public String getRegion() {
            return clientConfig.getRegion();
        }

        @Override
        public List<AwsEndpoint> getClusterEndpoints() {
            List<AwsEndpoint> result = localResolver.getClusterEndpoints();
            if (result.isEmpty()) {
                result = remoteResolver.getClusterEndpoints();
            }
            return result;
        }
    };
    return new AsyncResolver<>(EurekaClientNames.QUERY, new ZoneAffinityClusterResolver(compositeResolver, myZone, true, randomizer), transportConfig.getAsyncExecutorThreadPoolSize(), transportConfig.getAsyncResolverRefreshIntervalMs(), transportConfig.getAsyncResolverWarmUpTimeoutMs());
}
Also used : AsyncResolver(com.netflix.discovery.shared.resolver.AsyncResolver) ClusterResolver(com.netflix.discovery.shared.resolver.ClusterResolver) ConfigClusterResolver(com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver) ZoneAffinityClusterResolver(com.netflix.discovery.shared.resolver.aws.ZoneAffinityClusterResolver) AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) ZoneAffinityClusterResolver(com.netflix.discovery.shared.resolver.aws.ZoneAffinityClusterResolver)

Example 5 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint in project eureka by Netflix.

the class ResolverUtilsTest method testInstanceInfoToEndpoint.

@Test
public void testInstanceInfoToEndpoint() throws Exception {
    EurekaClientConfig clientConfig = mock(EurekaClientConfig.class);
    when(clientConfig.getEurekaServerURLContext()).thenReturn("/eureka");
    when(clientConfig.getRegion()).thenReturn("region");
    EurekaTransportConfig transportConfig = mock(EurekaTransportConfig.class);
    when(transportConfig.applicationsResolverUseIp()).thenReturn(false);
    AmazonInfo amazonInfo = AmazonInfo.Builder.newBuilder().addMetadata(AmazonInfo.MetaDataKey.availabilityZone, "us-east-1c").build();
    InstanceInfo instanceWithAWSInfo = InstanceInfo.Builder.newBuilder().setAppName("appName").setHostName("hostName").setPort(8080).setDataCenterInfo(amazonInfo).build();
    AwsEndpoint awsEndpoint = ResolverUtils.instanceInfoToEndpoint(clientConfig, transportConfig, instanceWithAWSInfo);
    assertEquals("zone not equals.", "us-east-1c", awsEndpoint.getZone());
    MyDataCenterInfo myDataCenterInfo = new MyDataCenterInfo(DataCenterInfo.Name.MyOwn);
    InstanceInfo instanceWithMyDataInfo = InstanceInfo.Builder.newBuilder().setAppName("appName").setHostName("hostName").setPort(8080).setDataCenterInfo(myDataCenterInfo).add("zone", "us-east-1c").build();
    awsEndpoint = ResolverUtils.instanceInfoToEndpoint(clientConfig, transportConfig, instanceWithMyDataInfo);
    assertEquals("zone not equals.", "us-east-1c", awsEndpoint.getZone());
}
Also used : MyDataCenterInfo(com.netflix.appinfo.MyDataCenterInfo) EurekaClientConfig(com.netflix.discovery.EurekaClientConfig) AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) EurekaTransportConfig(com.netflix.discovery.shared.transport.EurekaTransportConfig) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Aggregations

AwsEndpoint (com.netflix.discovery.shared.resolver.aws.AwsEndpoint)11 AsyncResolver (com.netflix.discovery.shared.resolver.AsyncResolver)6 ConfigClusterResolver (com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver)6 ZoneAffinityClusterResolver (com.netflix.discovery.shared.resolver.aws.ZoneAffinityClusterResolver)6 ClusterResolver (com.netflix.discovery.shared.resolver.ClusterResolver)4 ApplicationsResolver (com.netflix.discovery.shared.resolver.aws.ApplicationsResolver)3 Test (org.junit.Test)3 AmazonInfo (com.netflix.appinfo.AmazonInfo)2 EurekaHttpResolver (com.netflix.discovery.shared.resolver.aws.EurekaHttpResolver)2 List (java.util.List)2 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 MyDataCenterInfo (com.netflix.appinfo.MyDataCenterInfo)1 EurekaClientConfig (com.netflix.discovery.EurekaClientConfig)1 Applications (com.netflix.discovery.shared.Applications)1 EurekaEndpoint (com.netflix.discovery.shared.resolver.EurekaEndpoint)1 EurekaTransportConfig (com.netflix.discovery.shared.transport.EurekaTransportConfig)1 ArrayList (java.util.ArrayList)1 Matchers.anyString (org.mockito.Matchers.anyString)1