com.netflix.appinfo.AmazonInfo Java Examples

The following examples show how to use com.netflix.appinfo.AmazonInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EurekaController.java    From didi-eureka-server with MIT License 6 votes vote down vote up
private void populateHeader(Map<String, Object> model) {
	model.put("currentTime", StatusResource.getCurrentTimeAsString());
	model.put("upTime", StatusInfo.getUpTime());
	model.put("environment", ConfigurationManager.getDeploymentContext()
			.getDeploymentEnvironment());
	model.put("datacenter", ConfigurationManager.getDeploymentContext()
			.getDeploymentDatacenter());
	PeerAwareInstanceRegistry registry = getRegistry();
	model.put("registry", registry);
	model.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1);
	DataCenterInfo info = applicationInfoManager.getInfo().getDataCenterInfo();
	if (info.getName() == DataCenterInfo.Name.Amazon) {
		AmazonInfo amazonInfo = (AmazonInfo) info;
		model.put("amazonInfo", amazonInfo);
		model.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId));
		model.put("availabilityZone",
				amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone));
		model.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId));
	}
}
 
Example #2
Source File: EurekaController.java    From didi-eureka-server with MIT License 6 votes vote down vote up
private void populateInstanceInfo(Map<String, Object> model, StatusInfo statusInfo) {
	InstanceInfo instanceInfo = statusInfo.getInstanceInfo();
	Map<String, String> instanceMap = new HashMap<>();
	instanceMap.put("ipAddr", instanceInfo.getIPAddr());
	instanceMap.put("status", instanceInfo.getStatus().toString());
	if (instanceInfo.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
		AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo();
		instanceMap.put("availability-zone",
				info.get(AmazonInfo.MetaDataKey.availabilityZone));
		instanceMap.put("public-ipv4", info.get(AmazonInfo.MetaDataKey.publicIpv4));
		instanceMap.put("instance-id", info.get(AmazonInfo.MetaDataKey.instanceId));
		instanceMap.put("public-hostname",
				info.get(AmazonInfo.MetaDataKey.publicHostname));
		instanceMap.put("ami-id", info.get(AmazonInfo.MetaDataKey.amiId));
		instanceMap.put("instance-type",
				info.get(AmazonInfo.MetaDataKey.instanceType));
	}
	model.put("instanceInfo", instanceMap);
}
 
Example #3
Source File: EurekaHostCallerIdResolver.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private void appendApplicationAddresses(Map<String, String> newAddressMap, InstanceInfo ii) {
    String appName = ii.getAppName();
    if (appName == null) {
        return;
    }
    Consumer<String> addNonNull = address -> {
        if (address != null) {
            newAddressMap.put(address, appName);
        }
    };
    if (ii.getDataCenterInfo() instanceof AmazonInfo) {
        AmazonInfo amazonInfo = (AmazonInfo) ii.getDataCenterInfo();
        addNonNull.accept(amazonInfo.get(AmazonInfo.MetaDataKey.localHostname));
        addNonNull.accept(amazonInfo.get(AmazonInfo.MetaDataKey.localIpv4));
        addNonNull.accept(amazonInfo.get(AmazonInfo.MetaDataKey.publicHostname));
        addNonNull.accept(amazonInfo.get(AmazonInfo.MetaDataKey.publicIpv4));
    } else {
        addNonNull.accept(ii.getIPAddr());
        addNonNull.accept(ii.getHostName());
    }
}
 
Example #4
Source File: EurekaHostsSupplierTest.java    From dyno with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetHosts() {
    String zone = "us-east-1c";
    String hostname = "1.2.3.4";
    Application app = new Application();
    AmazonInfo amazonInfo = AmazonInfo.Builder.newBuilder()
            .addMetadata(MetaDataKey.availabilityZone, zone).build();
    InstanceInfo instance = InstanceInfo.Builder.newBuilder()
            .setInstanceId(UUID.randomUUID().toString())
            .setDataCenterInfo(amazonInfo)
            .setHostName(hostname)
            .setAppName(APPLICATION_NAME)
            .build();
    app.addInstance(instance);
    when(eurekaClient.getApplication(APPLICATION_NAME)).thenReturn(app);
    List<Host> hosts = hostsSupplier.getHosts();
    assertFalse(hosts.isEmpty());
    assertEquals(hostname, hosts.get(0).getHostName());
    assertEquals(zone, hosts.get(0).getRack());
}
 
Example #5
Source File: EurekaController.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
private void populateHeader(Map<String, Object> model) {
	model.put("currentTime", StatusResource.getCurrentTimeAsString());
	model.put("upTime", StatusInfo.getUpTime());
	model.put("environment", "N/A"); // FIXME:
	model.put("datacenter", "N/A"); // FIXME:
	PeerAwareInstanceRegistry registry = getRegistry();
	model.put("registry", registry);
	model.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1);
	DataCenterInfo info = applicationInfoManager.getInfo().getDataCenterInfo();
	if (info.getName() == DataCenterInfo.Name.Amazon) {
		AmazonInfo amazonInfo = (AmazonInfo) info;
		model.put("amazonInfo", amazonInfo);
		model.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId));
		model.put("availabilityZone",
				amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone));
		model.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId));
	}
}
 
Example #6
Source File: EurekaController.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
private void populateInstanceInfo(Map<String, Object> model, StatusInfo statusInfo) {
	InstanceInfo instanceInfo = statusInfo.getInstanceInfo();
	Map<String, String> instanceMap = new HashMap<>();
	instanceMap.put("ipAddr", instanceInfo.getIPAddr());
	instanceMap.put("status", instanceInfo.getStatus().toString());
	if (instanceInfo.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
		AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo();
		instanceMap.put("availability-zone",
				info.get(AmazonInfo.MetaDataKey.availabilityZone));
		instanceMap.put("public-ipv4", info.get(AmazonInfo.MetaDataKey.publicIpv4));
		instanceMap.put("instance-id", info.get(AmazonInfo.MetaDataKey.instanceId));
		instanceMap.put("public-hostname",
				info.get(AmazonInfo.MetaDataKey.publicHostname));
		instanceMap.put("ami-id", info.get(AmazonInfo.MetaDataKey.amiId));
		instanceMap.put("instance-type",
				info.get(AmazonInfo.MetaDataKey.instanceType));
	}
	model.put("instanceInfo", instanceMap);
}
 
Example #7
Source File: ArmeriaEurekaClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static com.linecorp.armeria.internal.common.eureka.DataCenterInfo convertDataCenterInfo(
        DataCenterInfo dataCenterInfo) {
    final Map<String, String> metadata;
    if (dataCenterInfo.getName() == Name.Amazon) {
        metadata = ((AmazonInfo) dataCenterInfo).getMetadata();
    } else {
        metadata = ImmutableMap.of();
    }
    return new com.linecorp.armeria.internal.common.eureka.DataCenterInfo(
            dataCenterInfo.getName().name(), metadata);
}
 
Example #8
Source File: EurekaHostsSupplier.java    From dyno with Apache License 2.0 5 votes vote down vote up
private List<Host> getUpdateFromEureka() {
    if (discoveryClient == null) {
        Logger.error("Discovery client cannot be null");
        throw new RuntimeException("EurekaHostsSupplier needs a non-null DiscoveryClient");
    }
    Logger.info("Dyno fetching instance list for app: " + applicationName);
    Application app = discoveryClient.getApplication(applicationName);
    List<Host> hosts = new ArrayList<Host>();

    if (app == null) {
        return hosts;
    }

    List<InstanceInfo> ins = app.getInstances();

    if (ins == null || ins.isEmpty()) {
        return hosts;
    }

    hosts = Lists.newArrayList(Collections2.transform(ins, info -> {
        Host.Status status = info.getStatus() == InstanceStatus.UP ? Host.Status.Up : Host.Status.Down;

        String rack = null;
        try {
            if (info.getDataCenterInfo() instanceof AmazonInfo) {
                AmazonInfo amazonInfo = (AmazonInfo) info.getDataCenterInfo();
                rack = amazonInfo.get(MetaDataKey.availabilityZone);
            }
        } catch (Throwable t) {
            Logger.error("Error getting rack for host " + info.getHostName(), t);
        }
        if (rack == null) {
            Logger.error("Rack wasn't found for host:" + info.getHostName() + " there may be issues matching it up to the token map");
        }
        Host host = new HostBuilder().setHostname(info.getHostName()).setIpAddress(info.getIPAddr()).setRack(rack).setStatus(status).createHost();
        return host;
    }));
    Logger.info("Dyno found hosts from eureka - num hosts: " + hosts.size());
    return hosts;
}
 
Example #9
Source File: DefaultNIWSServerListFilterTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private DiscoveryEnabledServer createServer(String host, int port, String zone) {
    AmazonInfo amazonInfo = AmazonInfo.Builder.newBuilder().addMetadata(AmazonInfo.MetaDataKey.availabilityZone, zone).build();
    
    Builder builder = InstanceInfo.Builder.newBuilder();
    InstanceInfo info = builder.setAppName("l10nservicegeneral")
    .setDataCenterInfo(amazonInfo)
    .setHostName(host)
    .setPort(port)
    .build();
    DiscoveryEnabledServer server = new DiscoveryEnabledServer(info, false, false);
    server.setZone(zone);
    return server;
}
 
Example #10
Source File: RequestAttempt.java    From zuul with Apache License 2.0 5 votes vote down vote up
public RequestAttempt(int attemptNumber, InstanceInfo server, String targetVip, String chosenWarmupLB, int status, String error, String exceptionType,
                      int readTimeout, int connectTimeout, int maxRetries)
{
    if (attemptNumber < 1) {
        throw new IllegalArgumentException("Attempt number must be greater than 0! - " + attemptNumber);
    }
    this.attempt = attemptNumber;
    this.vip = targetVip;

    if (server != null) {
        this.app = server.getAppName().toLowerCase();
        this.asg = server.getASGName();
        this.instanceId = server.getInstanceId();
        this.host = server.getHostName();
        this.port = server.getPort();

        // If targetVip is null, then try to use the actual server's vip.
        if (targetVip == null) {
            this.vip = server.getVIPAddress();
        }

        if (server.getDataCenterInfo() instanceof AmazonInfo) {
            this.availabilityZone = ((AmazonInfo) server.getDataCenterInfo()).getMetadata().get("availability-zone");

            // HACK - get region by just removing the last char from zone.
            String az = getAvailabilityZone();
            if (az != null && az.length() > 0) {
                this.region = az.substring(0, az.length() - 1);
            }
        }
    }
    
    this.status = status;
    this.error = error;
    this.exceptionType = exceptionType;
    this.readTimeout = readTimeout;
    this.connectTimeout = connectTimeout;
    this.maxRetries = maxRetries;
}
 
Example #11
Source File: EurekaHostsSupplier.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<List<Host>> getSupplier(final String clusterName)
{
    return new Supplier<List<Host>>() {

        @Override
        public List<Host> get() {

            if (discoveryClient == null) {
                LOG.error("Discovery client cannot be null");
                throw new RuntimeException("EurekaHostsSupplier needs a non-null DiscoveryClient");
            }

            LOG.debug("Raigad fetching instance list for app: " + clusterName);

            Application app = discoveryClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = new ArrayList<Host>();

            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", clusterName);
                return hosts;
            }

            List<InstanceInfo> ins = app.getInstances();

            if (ins == null || ins.isEmpty()) {
                LOG.warn("Cluster '{}' found in eureka but has no instances", clusterName);
                return hosts;
            }

            hosts = Lists.newArrayList(Collections2.transform(
                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                        @Override
                        public boolean apply(InstanceInfo input) {
                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                        }
                    }), new Function<InstanceInfo, Host>() {
                        @Override
                        public Host apply(InstanceInfo info) {
                            String[] parts = StringUtils.split(
                                    StringUtils.split(info.getHostName(), ".")[0], '-');

                            Host host = new Host(info.getHostName(), info.getPort())
                                    .addAlternateIpAddress(
                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                    parts[4] }, "."))
                                    .addAlternateIpAddress(info.getIPAddr())
                                    .setId(info.getId());

                            try {
                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                }
                            }
                            catch (Throwable t) {
                                LOG.error("Error getting rack for host " + host.getName(), t);
                            }

                            return host;
                        }
                    }));

            LOG.debug("Raigad found hosts from eureka - num hosts: " + hosts.size());

            return hosts;
        }
    };
}
 
Example #12
Source File: EurekaAstyanaxHostSupplier.java    From staash with Apache License 2.0 4 votes vote down vote up
public Supplier<List<Host>> getSupplier(final String clusterName) {
    return new Supplier<List<Host>>() {

        public List<Host> get() {
            Application app = eurekaClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = Lists.newArrayList();
            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", new Object[]{clusterName});
            }
            else {
                List<InstanceInfo> ins = app.getInstances();
                if (ins != null && !ins.isEmpty()) {
                    hosts = Lists.newArrayList(Collections2.transform(
                                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                                        public boolean apply(InstanceInfo input) {
                                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                                        }
                                    }), new Function<InstanceInfo, Host>() {
                                        public Host apply(InstanceInfo info) {
                                            String[] parts = StringUtils.split(
                                                    StringUtils.split(info.getHostName(), ".")[0], '-');
    
                                            Host host = new Host(info.getHostName(), info.getPort())
                                                    .addAlternateIpAddress(
                                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                                    parts[4] }, "."))
                                                    .addAlternateIpAddress(info.getIPAddr())
                                                    .setId(info.getId());
                                            
                                            try {
                                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                                }
                                            }
                                            catch (Throwable t) {
                                                LOG.error("Error getting rack for host " + host.getName(), t);
                                            }
    
                                            return host;
                                        }
                                    }));
                }
                else {
                    LOG.warn("Cluster '{}' found in eureka but has no instances", new Object[]{clusterName});
                }
            }
            return hosts;
        }
    };
}
 
Example #13
Source File: EurekaAstyanaxHostSupplier.java    From staash with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<List<Host>> getSupplier(final String clusterName) {
    return new Supplier<List<Host>>() {

        @Override
        public List<Host> get() {
            Application app = eurekaClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = Lists.newArrayList();
            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", new Object[]{clusterName});
            }
            else {
                List<InstanceInfo> ins = app.getInstances();
                if (ins != null && !ins.isEmpty()) {
                    hosts = Lists.newArrayList(Collections2.transform(
                                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                                        @Override
                                        public boolean apply(InstanceInfo input) {
                                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                                        }
                                    }), new Function<InstanceInfo, Host>() {
                                        @Override
                                        public Host apply(InstanceInfo info) {
                                            String[] parts = StringUtils.split(
                                                    StringUtils.split(info.getHostName(), ".")[0], '-');
    
                                            Host host = new Host(info.getHostName(), info.getPort())
                                                    .addAlternateIpAddress(
                                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                                    parts[4] }, "."))
                                                    .addAlternateIpAddress(info.getIPAddr())
                                                    .setId(info.getId());
                                            
                                            try {
                                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                                }
                                            }
                                            catch (Throwable t) {
                                                LOG.error("Error getting rack for host " + host.getName(), t);
                                            }
    
                                            return host;
                                        }
                                    }));
                }
                else {
                    LOG.warn("Cluster '{}' found in eureka but has no instances", new Object[]{clusterName});
                }
            }
            return hosts;
        }
    };
}
 
Example #14
Source File: RequestAttempt.java    From zuul with Apache License 2.0 4 votes vote down vote up
public RequestAttempt(final Server server, final IClientConfig clientConfig, int attemptNumber, int readTimeout) {
    this.status = -1;
    this.attempt = attemptNumber;
    this.readTimeout = readTimeout;

    if (server != null) {
        this.host = server.getHost();
        this.port = server.getPort();
        this.availabilityZone = server.getZone();

        if (server instanceof DiscoveryEnabledServer) {
            InstanceInfo instanceInfo = ((DiscoveryEnabledServer) server).getInstanceInfo();
            this.app = instanceInfo.getAppName().toLowerCase();
            this.asg = instanceInfo.getASGName();
            this.instanceId = instanceInfo.getInstanceId();
            this.host = instanceInfo.getHostName();
            this.port = instanceInfo.getPort();

            if (server.getPort() == instanceInfo.getSecurePort()) {
                this.vip = instanceInfo.getSecureVipAddress();
            }
            else {
                this.vip = instanceInfo.getVIPAddress();
            }
            if (instanceInfo.getDataCenterInfo() instanceof AmazonInfo) {
                this.availabilityZone = ((AmazonInfo) instanceInfo.getDataCenterInfo()).getMetadata().get("availability-zone");
            }
        }
        else {
            final Server.MetaInfo metaInfo = server.getMetaInfo();
            if (metaInfo != null) {
                this.asg = metaInfo.getServerGroup();
                this.vip = metaInfo.getServiceIdForDiscovery();
                this.instanceId = metaInfo.getInstanceId();
            }
        }
        // HACK - get region by just removing the last char from zone.
        if (availabilityZone != null && availabilityZone.length() > 0) {
            region = availabilityZone.substring(0, availabilityZone.length() - 1);
        }
    }

    if (clientConfig != null) {
        this.connectTimeout = clientConfig.get(IClientConfigKey.Keys.ConnectTimeout);
    }
}
 
Example #15
Source File: AmazonInfoHolder.java    From zuul with Apache License 2.0 4 votes vote down vote up
public static final AmazonInfo getInfo() {
    return INFO;
}