public InstanceInfo getMyInstanceInfo() { return createApplicationManager().getInfo(); }
public InstanceInfo.InstanceStatus localStatus() { return applicationInfoManager.getInfo().getStatus(); }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo(); InstanceStatus status = myInfo.getStatus(); if (status != InstanceStatus.UP && response instanceof HttpServletResponse) { HttpServletResponse httpRespone = (HttpServletResponse) response; httpRespone.sendError(SC_TEMPORARY_REDIRECT, "Current node is currently not ready to serve requests -- current status: " + status + " - try another DS node: "); } chain.doFilter(request, response); }
private String getMyZone() { InstanceInfo info = applicationInfoManager.getInfo(); AmazonInfo amazonInfo = info != null ? (AmazonInfo) info.getDataCenterInfo() : null; String zone = amazonInfo != null ? amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone) : null; if (zone == null) { throw new RuntimeException("Cannot extract availabilityZone"); } return zone; }
private Collection<String> getIPsForZoneFromDNS(String myZone) { return EndpointUtils.getServiceUrlsFromDNS( clientConfig, myZone, true, new EndpointUtils.InstanceInfoBasedUrlRandomizer(applicationInfoManager.getInfo()) ); }
/** * Checks if the given service url contains the current host which is trying * to replicate. Only after the EIP binding is done the host has a chance to * identify itself in the list of replica nodes and needs to take itself out * of replication traffic. * * @param url the service url of the replica node that the check is made. * @return true, if the url represents the current node which is trying to * replicate, false otherwise. */ public boolean isThisMyUrl(String url) { final String myUrlConfigured = serverConfig.getMyUrl(); if (myUrlConfigured != null) { return myUrlConfigured.equals(url); } return isInstanceURL(url, applicationInfoManager.getInfo()); }
private static String getRegistrationHostnameFromAmazonDataCenterInfo(ApplicationInfoManager applicationInfoManager) { InstanceInfo myInfo = applicationInfoManager.getInfo(); AmazonInfo dataCenterInfo = (AmazonInfo) myInfo.getDataCenterInfo(); String ip = dataCenterInfo.get(AmazonInfo.MetaDataKey.publicHostname); if (ip == null || ip.length() == 0) { return dataCenterInfo.get(AmazonInfo.MetaDataKey.localHostname); } return ip; }
/** * Sets a flag if this instance is the same as the discovery server that is * return the instances. This flag is used by the discovery clients to * identity the discovery server which is coordinating/returning the * information. */ public void setIsCoordinatingDiscoveryServer() { String instanceId = getId(); if ((instanceId != null) && (instanceId.equals(ApplicationInfoManager.getInstance() .getInfo().getId()))) { isCoordinatingDiscoveryServer = Boolean.TRUE; } else { isCoordinatingDiscoveryServer = Boolean.FALSE; } }
/** * @deprecated 2016-06-27 use instance version of {@link #isThisMyUrl(String)} * * Checks if the given service url contains the current host which is trying * to replicate. Only after the EIP binding is done the host has a chance to * identify itself in the list of replica nodes and needs to take itself out * of replication traffic. * * @param url the service url of the replica node that the check is made. * @return true, if the url represents the current node which is trying to * replicate, false otherwise. */ public static boolean isThisMe(String url) { InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo(); String hostName = hostFromUrl(url); return hostName != null && hostName.equals(myInfo.getHostName()); }
private static String getAccountId() { InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo(); return ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.accountId); }
public InstanceInfo.InstanceStatus localStatus() { return applicationInfoManager.getInfo().getStatus(); }
/** * Initializes the <tt>Discovery Client</tt> with the given configuration. * * @param config * the instance info configuration that will be used for * registration with Eureka. * @param eurekaConfig the eureka client configuration of the instance. */ public void initComponent(EurekaInstanceConfig config, EurekaClientConfig eurekaConfig, AbstractDiscoveryClientOptionalArgs args) { this.eurekaInstanceConfig = config; this.eurekaClientConfig = eurekaConfig; if (ApplicationInfoManager.getInstance().getInfo() == null) { // Initialize application info ApplicationInfoManager.getInstance().initComponent(config); } InstanceInfo info = ApplicationInfoManager.getInstance().getInfo(); discoveryClient = new DiscoveryClient(info, eurekaConfig, args); }
private EurekaMonitors(String name, String description) { this.name = name; this.description = description; DataCenterInfo dcInfo = ApplicationInfoManager.getInstance().getInfo().getDataCenterInfo(); if (dcInfo.getName() == Name.Amazon) { myZoneCounterName = ((AmazonInfo) dcInfo).get(MetaDataKey.availabilityZone) + "." + name; } else { myZoneCounterName = "dcmaster." + name; } }
public boolean alreadyBound() throws MalformedURLException { InstanceInfo myInfo = applicationInfoManager.getInfo(); String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId); AmazonEC2 ec2Service = getEC2Service(); List<InstanceNetworkInterface> instanceNetworkInterfaces = instanceData(myInstanceId, ec2Service).getNetworkInterfaces(); List<String> candidateIPs = getCandidateIps(); for (String ip : candidateIPs) { for(InstanceNetworkInterface ini: instanceNetworkInterfaces) { if (ip.equals(ini.getPrivateIpAddress())) { logger.info("My instance {} seems to be already associated with the ip {}", myInstanceId, ip); return true; } } } return false; }
/** * Checks if an EIP is already bound to the instance. * @return true if an EIP is bound, false otherwise */ public boolean isEIPBound() { InstanceInfo myInfo = applicationInfoManager.getInfo(); String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId); String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone); String myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4); Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone); for (String eipEntry : candidateEIPs) { if (eipEntry.equals(myPublicIP)) { logger.info("My instance {} seems to be already associated with the public ip {}", myInstanceId, myPublicIP); return true; } } return false; }
/** * Unbind the IP that this instance is associated with. */ public void unbind() throws Exception { InstanceInfo myInfo = applicationInfoManager.getInfo(); String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId); AmazonEC2 ec2 = getEC2Service(); List<InstanceNetworkInterface> result = instanceData(myInstanceId, ec2).getNetworkInterfaces(); List<String> ips = getCandidateIps(); for(InstanceNetworkInterface networkInterface: result){ if (ips.contains(networkInterface.getPrivateIpAddress())) { String attachmentId = networkInterface.getAttachment().getAttachmentId(); ec2.detachNetworkInterface(new DetachNetworkInterfaceRequest().withAttachmentId(attachmentId)); break; } } }
@Test public void testCustomInstanceStatusMapper() { ApplicationInfoManager.OptionalArgs optionalArgs = new ApplicationInfoManager.OptionalArgs(); optionalArgs.setInstanceStatusMapper(new ApplicationInfoManager.InstanceStatusMapper() { @Override public InstanceInfo.InstanceStatus map(InstanceInfo.InstanceStatus prev) { return InstanceInfo.InstanceStatus.UNKNOWN; } }); applicationInfoManager = new ApplicationInfoManager(config, instanceInfo, optionalArgs); InstanceInfo.InstanceStatus existingStatus = applicationInfoManager.getInfo().getStatus(); assertNotEquals(existingStatus, InstanceInfo.InstanceStatus.UNKNOWN); applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.UNKNOWN); existingStatus = applicationInfoManager.getInfo().getStatus(); assertEquals(existingStatus, InstanceInfo.InstanceStatus.UNKNOWN); }
@Test public void testNullResultInstanceStatusMapper() { ApplicationInfoManager.OptionalArgs optionalArgs = new ApplicationInfoManager.OptionalArgs(); optionalArgs.setInstanceStatusMapper(new ApplicationInfoManager.InstanceStatusMapper() { @Override public InstanceInfo.InstanceStatus map(InstanceInfo.InstanceStatus prev) { return null; } }); applicationInfoManager = new ApplicationInfoManager(config, instanceInfo, optionalArgs); InstanceInfo.InstanceStatus existingStatus1 = applicationInfoManager.getInfo().getStatus(); assertNotEquals(existingStatus1, InstanceInfo.InstanceStatus.UNKNOWN); applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.UNKNOWN); InstanceInfo.InstanceStatus existingStatus2 = applicationInfoManager.getInfo().getStatus(); assertEquals(existingStatus2, existingStatus1); } }
@SuppressWarnings("deprecation") @Test public void testDI() { InstanceInfo instanceInfo = injector.getInstance(InstanceInfo.class); Assert.assertEquals(ApplicationInfoManager.getInstance().getInfo(), instanceInfo); 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); Binding<TransportClientFactories> binding = injector.getExistingBinding(Key.get(TransportClientFactories.class)); Assert.assertNull(binding); // no bindings so defaulting to default of jersey1 } }