Search in sources :

Example 1 with ZuulRouteDTO

use of io.github.jhipster.registry.service.dto.ZuulRouteDTO 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)

Aggregations

InstanceInfo (com.netflix.appinfo.InstanceInfo)1 Application (com.netflix.discovery.shared.Application)1 EurekaServerContextHolder (com.netflix.eureka.EurekaServerContextHolder)1 ZuulRouteDTO (io.github.jhipster.registry.service.dto.ZuulRouteDTO)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 RoutesRefreshedEvent (org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent)1 RouteLocator (org.springframework.cloud.netflix.zuul.filters.RouteLocator)1 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Service (org.springframework.stereotype.Service)1