Search in sources :

Example 1 with Application

use of com.netflix.discovery.shared.Application in project jhipster-registry by jhipster.

the class ZuulUpdaterService method updateZuulRoutes.

@Scheduled(fixedDelay = 5_000)
public void updateZuulRoutes() {
    boolean isDirty = false;
    List<Application> applications = EurekaServerContextHolder.getInstance().getServerContext().getRegistry().getApplications().getRegisteredApplications();
    for (Application application : applications) {
        for (InstanceInfo instanceInfos : application.getInstances()) {
            if (!instanceInfos.getStatus().equals(InstanceInfo.InstanceStatus.UP) && !instanceInfos.getStatus().equals(InstanceInfo.InstanceStatus.STARTING))
                continue;
            String instanceId = instanceInfos.getId();
            String url = instanceInfos.getHomePageUrl();
            log.debug("Checking instance {} - {} ", instanceId, url);
            ZuulRouteDTO route = new ZuulRouteDTO(instanceId, application.getName().toLowerCase() + "/" + instanceId + "/**", null, url, zuulProperties.isStripPrefix(), zuulProperties.getRetryable(), null, instanceInfos.getStatus().toString());
            if (zuulProperties.getRoutes().containsKey(instanceId)) {
                log.debug("Instance '{}' already registered", instanceId);
                if (!zuulProperties.getRoutes().get(instanceId).getUrl().equals(url) || !((ZuulRouteDTO) zuulProperties.getRoutes().get(instanceId)).getStatus().equals(instanceInfos.getStatus().toString())) {
                    log.debug("Updating instance '{}' with new URL: {}", instanceId, url);
                    zuulProperties.getRoutes().put(instanceId, route);
                    isDirty = true;
                }
            } else {
                log.debug("Adding instance '{}' with URL: {}", instanceId, url);
                zuulProperties.getRoutes().put(instanceId, route);
                isDirty = true;
            }
        }
    }
    List<String> zuulRoutesToRemove = new ArrayList<>();
    for (String key : zuulProperties.getRoutes().keySet()) {
        if (applications.stream().flatMap(application -> application.getInstances().stream()).filter(instanceInfo -> instanceInfo.getId().equals(key)).count() == 0) {
            log.debug("Removing instance '{}'", key);
            zuulRoutesToRemove.add(key);
            isDirty = true;
        }
    }
    for (String key : zuulRoutesToRemove) {
        zuulProperties.getRoutes().remove(key);
    }
    if (isDirty) {
        log.info("Zuul routes have changed - refreshing the configuration");
        this.publisher.publishEvent(new RoutesRefreshedEvent(routeLocator));
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application) ZuulProperties(org.springframework.cloud.netflix.zuul.filters.ZuulProperties) Scheduled(org.springframework.scheduling.annotation.Scheduled) ArrayList(java.util.ArrayList) List(java.util.List) Service(org.springframework.stereotype.Service) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EurekaServerContextHolder(com.netflix.eureka.EurekaServerContextHolder) RoutesRefreshedEvent(org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent) RouteLocator(org.springframework.cloud.netflix.zuul.filters.RouteLocator) ZuulRouteDTO(io.github.jhipster.registry.service.dto.ZuulRouteDTO) ArrayList(java.util.ArrayList) Application(com.netflix.discovery.shared.Application) InstanceInfo(com.netflix.appinfo.InstanceInfo) ZuulRouteDTO(io.github.jhipster.registry.service.dto.ZuulRouteDTO) RoutesRefreshedEvent(org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with Application

use of com.netflix.discovery.shared.Application in project jhipster-registry by jhipster.

the class EurekaResource method getApplications.

private List<Map<String, Object>> getApplications() {
    List<Application> sortedApplications = getRegistry().getSortedApplications();
    ArrayList<Map<String, Object>> apps = new ArrayList<>();
    for (Application app : sortedApplications) {
        LinkedHashMap<String, Object> appData = new LinkedHashMap<>();
        apps.add(appData);
        appData.put("name", app.getName());
        List<Map<String, Object>> instances = new ArrayList<>();
        for (InstanceInfo info : app.getInstances()) {
            Map<String, Object> instance = new HashMap<>();
            instance.put("instanceId", info.getInstanceId());
            instance.put("homePageUrl", info.getHomePageUrl());
            instance.put("healthCheckUrl", info.getHealthCheckUrl());
            instance.put("statusPageUrl", info.getStatusPageUrl());
            instance.put("status", info.getStatus().name());
            instance.put("metadata", info.getMetadata());
            instances.add(instance);
        }
        appData.put("instances", instances);
    }
    return apps;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) LinkedHashMap(java.util.LinkedHashMap) Application(com.netflix.discovery.shared.Application) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with Application

use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.

the class EurekaControllerTests method setup.

@Before
public void setup() throws Exception {
    PeerEurekaNodes peerEurekaNodes = mock(PeerEurekaNodes.class);
    when(peerEurekaNodes.getPeerNodesView()).thenReturn(Collections.<PeerEurekaNode>emptyList());
    InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("test").setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).build();
    this.infoManager = mock(ApplicationInfoManager.class);
    this.original = ApplicationInfoManager.getInstance();
    setInstance(this.infoManager);
    when(this.infoManager.getInfo()).thenReturn(instanceInfo);
    Application myapp = new Application("myapp");
    myapp.addInstance(InstanceInfo.Builder.newBuilder().setAppName("myapp").setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).setInstanceId("myapp:1").build());
    ArrayList<Application> applications = new ArrayList<>();
    applications.add(myapp);
    PeerAwareInstanceRegistry registry = mock(PeerAwareInstanceRegistry.class);
    when(registry.getSortedApplications()).thenReturn(applications);
    EurekaServerContext serverContext = mock(EurekaServerContext.class);
    EurekaServerContextHolder.initialize(serverContext);
    when(serverContext.getRegistry()).thenReturn(registry);
    when(serverContext.getPeerEurekaNodes()).thenReturn(peerEurekaNodes);
    when(serverContext.getApplicationInfoManager()).thenReturn(this.infoManager);
}
Also used : MyDataCenterInfo(com.netflix.appinfo.MyDataCenterInfo) EurekaServerContext(com.netflix.eureka.EurekaServerContext) PeerEurekaNodes(com.netflix.eureka.cluster.PeerEurekaNodes) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) Application(com.netflix.discovery.shared.Application) ApplicationInfoManager(com.netflix.appinfo.ApplicationInfoManager) PeerAwareInstanceRegistry(com.netflix.eureka.registry.PeerAwareInstanceRegistry) Before(org.junit.Before)

Example 4 with Application

use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.

the class EurekaController method populateApps.

private void populateApps(Map<String, Object> model) {
    List<Application> sortedApplications = getRegistry().getSortedApplications();
    ArrayList<Map<String, Object>> apps = new ArrayList<>();
    for (Application app : sortedApplications) {
        LinkedHashMap<String, Object> appData = new LinkedHashMap<>();
        apps.add(appData);
        appData.put("name", app.getName());
        Map<String, Integer> amiCounts = new HashMap<>();
        Map<InstanceInfo.InstanceStatus, List<Pair<String, String>>> instancesByStatus = new HashMap<>();
        Map<String, Integer> zoneCounts = new HashMap<>();
        for (InstanceInfo info : app.getInstances()) {
            String id = info.getId();
            String url = info.getStatusPageUrl();
            InstanceInfo.InstanceStatus status = info.getStatus();
            String ami = "n/a";
            String zone = "";
            if (info.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
                AmazonInfo dcInfo = (AmazonInfo) info.getDataCenterInfo();
                ami = dcInfo.get(AmazonInfo.MetaDataKey.amiId);
                zone = dcInfo.get(AmazonInfo.MetaDataKey.availabilityZone);
            }
            Integer count = amiCounts.get(ami);
            if (count != null) {
                amiCounts.put(ami, count + 1);
            } else {
                amiCounts.put(ami, 1);
            }
            count = zoneCounts.get(zone);
            if (count != null) {
                zoneCounts.put(zone, count + 1);
            } else {
                zoneCounts.put(zone, 1);
            }
            List<Pair<String, String>> list = instancesByStatus.get(status);
            if (list == null) {
                list = new ArrayList<>();
                instancesByStatus.put(status, list);
            }
            list.add(new Pair<>(id, url));
        }
        appData.put("amiCounts", amiCounts.entrySet());
        appData.put("zoneCounts", zoneCounts.entrySet());
        ArrayList<Map<String, Object>> instanceInfos = new ArrayList<>();
        appData.put("instanceInfos", instanceInfos);
        for (Iterator<Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>>> iter = instancesByStatus.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>> entry = iter.next();
            List<Pair<String, String>> value = entry.getValue();
            InstanceInfo.InstanceStatus status = entry.getKey();
            LinkedHashMap<String, Object> instanceData = new LinkedHashMap<>();
            instanceInfos.add(instanceData);
            instanceData.put("status", entry.getKey());
            ArrayList<Map<String, Object>> instances = new ArrayList<>();
            instanceData.put("instances", instances);
            instanceData.put("isNotUp", status != InstanceInfo.InstanceStatus.UP);
            for (Pair<String, String> p : value) {
                LinkedHashMap<String, Object> instance = new LinkedHashMap<>();
                instances.add(instance);
                instance.put("id", p.first());
                String url = p.second();
                instance.put("url", url);
                boolean isHref = url != null && url.startsWith("http");
                instance.put("isHref", isHref);
            /*
					 * String id = p.first(); String url = p.second(); if(url != null &&
					 * url.startsWith("http")){
					 * buf.append("<a href=\"").append(url).append("\">"); }else { url =
					 * null; } buf.append(id); if(url != null){ buf.append("</a>"); }
					 * buf.append(", ");
					 */
            }
        }
    // out.println("<td>" + buf.toString() + "</td></tr>");
    }
    model.put("apps", apps);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) InstanceInfo(com.netflix.appinfo.InstanceInfo) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) AmazonInfo(com.netflix.appinfo.AmazonInfo) Pair(com.netflix.discovery.shared.Pair) Application(com.netflix.discovery.shared.Application) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Application

use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.

the class RestTemplateEurekaHttpClient method getApplication.

@Override
public EurekaHttpResponse<Application> getApplication(String appName) {
    String urlPath = serviceUrl + "apps/" + appName;
    ResponseEntity<Application> response = restTemplate.exchange(urlPath, HttpMethod.GET, null, Application.class);
    Application application = response.getStatusCodeValue() == HttpStatus.OK.value() && response.hasBody() ? response.getBody() : null;
    return anEurekaHttpResponse(response.getStatusCodeValue(), application).headers(headersOf(response)).build();
}
Also used : Application(com.netflix.discovery.shared.Application)

Aggregations

Application (com.netflix.discovery.shared.Application)104 InstanceInfo (com.netflix.appinfo.InstanceInfo)51 Test (org.junit.Test)43 Applications (com.netflix.discovery.shared.Applications)41 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)7 Response (javax.ws.rs.core.Response)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)6 Map (java.util.Map)6 DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)4 Lease (com.netflix.eureka.lease.Lease)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 AmazonInfo (com.netflix.appinfo.AmazonInfo)2 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)2 Pair (com.netflix.discovery.shared.Pair)2