Search in sources :

Example 1 with Instance

use of com.netflix.turbine.discovery.Instance in project spring-cloud-netflix by spring-cloud.

the class CommonsInstanceDiscovery method getInstancesForApp.

/**
 * helper that fetches the Instances for each application from DiscoveryClient.
 * @param serviceId
 * @return List<Instance>
 * @throws Exception
 */
protected List<Instance> getInstancesForApp(String serviceId) throws Exception {
    List<Instance> instances = new ArrayList<>();
    log.info("Fetching instances for app: " + serviceId);
    List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);
    if (serviceInstances == null || serviceInstances.isEmpty()) {
        log.warn("DiscoveryClient returned null or empty for service: " + serviceId);
        return instances;
    }
    try {
        log.info("Received instance list for service: " + serviceId + ", size=" + serviceInstances.size());
        for (ServiceInstance serviceInstance : serviceInstances) {
            Instance instance = marshall(serviceInstance);
            if (instance != null) {
                instances.add(instance);
            }
        }
    } catch (Exception e) {
        log.warn("Failed to retrieve instances from DiscoveryClient", e);
    }
    return instances;
}
Also used : Instance(com.netflix.turbine.discovery.Instance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 2 with Instance

use of com.netflix.turbine.discovery.Instance in project spring-cloud-netflix by spring-cloud.

the class EurekaInstanceDiscoveryTests method testSecureCombineHostPort.

@Test
public void testSecureCombineHostPort() {
    turbineProperties.setCombineHostPort(true);
    EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient);
    String appName = "testAppName";
    int port = 8080;
    int securePort = 8443;
    String hostName = "myhost";
    InstanceInfo instanceInfo = builder.setAppName(appName).setHostName(hostName).setPort(port).setSecurePort(securePort).enablePort(InstanceInfo.PortType.SECURE, true).build();
    Instance instance = discovery.marshall(instanceInfo);
    assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
    assertEquals("securePort is wrong", String.valueOf(securePort), instance.getAttributes().get("securePort"));
    String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
    assertEquals("url is wrong", "https://" + hostName + ":" + securePort + "/actuator/hystrix.stream", urlPath);
}
Also used : Instance(com.netflix.turbine.discovery.Instance) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 3 with Instance

use of com.netflix.turbine.discovery.Instance in project spring-cloud-netflix by spring-cloud.

the class EurekaInstanceDiscoveryTests method testGetSecurePort.

@Test
public void testGetSecurePort() {
    EurekaInstanceDiscovery discovery = new EurekaInstanceDiscovery(turbineProperties, eurekaClient);
    String appName = "testAppName";
    int port = 8080;
    int securePort = 8443;
    String hostName = "myhost";
    InstanceInfo instanceInfo = builder.setAppName(appName).setHostName(hostName).setPort(port).setSecurePort(securePort).enablePort(InstanceInfo.PortType.SECURE, true).build();
    Instance instance = discovery.marshall(instanceInfo);
    assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
    assertEquals("securePort is wrong", String.valueOf(securePort), instance.getAttributes().get("securePort"));
    String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
    assertEquals("url is wrong", "https://" + hostName + ":" + securePort + "/actuator/hystrix.stream", urlPath);
}
Also used : Instance(com.netflix.turbine.discovery.Instance) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 4 with Instance

use of com.netflix.turbine.discovery.Instance in project spring-cloud-netflix by spring-cloud.

the class CommonsInstanceDiscoveryTests method testCombineHostPort.

@Test
public void testCombineHostPort() {
    turbineProperties.setCombineHostPort(true);
    CommonsInstanceDiscovery discovery = createDiscovery();
    String appName = "testAppName";
    int port = 8080;
    String hostName = "myhost";
    DefaultServiceInstance serviceInstance = new DefaultServiceInstance(appName, hostName, port, false);
    Instance instance = discovery.marshall(serviceInstance);
    assertEquals("hostname is wrong", hostName + ":" + port, instance.getHostname());
    assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
    String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
    assertEquals("url is wrong", "http://" + hostName + ":" + port + "/actuator/hystrix.stream", urlPath);
    String clusterName = discovery.getClusterName(serviceInstance);
    assertEquals("clusterName is wrong", appName, clusterName);
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) Instance(com.netflix.turbine.discovery.Instance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) Test(org.junit.Test)

Example 5 with Instance

use of com.netflix.turbine.discovery.Instance in project spring-cloud-netflix by spring-cloud.

the class CommonsInstanceDiscoveryTests method testUseManagementPortFromMetadata.

@Test
public void testUseManagementPortFromMetadata() {
    CommonsInstanceDiscovery discovery = createDiscovery();
    String appName = "testAppName";
    int port = 8080;
    int managementPort = 8081;
    String hostName = "myhost";
    DefaultServiceInstance serviceInstance = new DefaultServiceInstance(appName, hostName, port, false);
    serviceInstance.getMetadata().put("management.port", String.valueOf(managementPort));
    Instance instance = discovery.marshall(serviceInstance);
    assertEquals("port is wrong", String.valueOf(managementPort), instance.getAttributes().get("port"));
    String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
    assertEquals("url is wrong", "http://" + hostName + ":" + managementPort + "/actuator/hystrix.stream", urlPath);
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) Instance(com.netflix.turbine.discovery.Instance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) Test(org.junit.Test)

Aggregations

Instance (com.netflix.turbine.discovery.Instance)14 Test (org.junit.Test)10 InstanceInfo (com.netflix.appinfo.InstanceInfo)6 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)5 ArrayList (java.util.ArrayList)2 ServiceInstance (org.springframework.cloud.client.ServiceInstance)2 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)1 Application (com.netflix.discovery.shared.Application)1