Search in sources :

Example 1 with RouteLocator

use of org.springframework.cloud.netflix.zuul.filters.RouteLocator 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 RouteLocator

use of org.springframework.cloud.netflix.zuul.filters.RouteLocator in project spring-cloud-netflix by spring-cloud.

the class ZuulHandlerMappingTests method mappingWithIgnoredPathsAndRoutes.

private ZuulHandlerMapping mappingWithIgnoredPathsAndRoutes(List<String> ignoredPaths, Route route) {
    RouteLocator routeLocator = Mockito.mock(RouteLocator.class);
    Mockito.when(routeLocator.getIgnoredPaths()).thenReturn(ignoredPaths);
    Mockito.when(routeLocator.getRoutes()).thenReturn(Collections.singletonList(route));
    ZuulHandlerMapping zuulHandlerMapping = new ZuulHandlerMapping(routeLocator, new ZuulController());
    return zuulHandlerMapping;
}
Also used : RouteLocator(org.springframework.cloud.netflix.zuul.filters.RouteLocator)

Example 3 with RouteLocator

use of org.springframework.cloud.netflix.zuul.filters.RouteLocator in project spring-cloud-netflix by spring-cloud.

the class LocationRewriteFilterTests method setFilterUpWith.

private LocationRewriteFilter setFilterUpWith(RequestContext context, ZuulProperties zuulProperties, Route route, String toZuulRequestUri, String redirectedUri) {
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setRequestURI(toZuulRequestUri);
    httpServletRequest.setServerName(ZUUL_HOST);
    httpServletRequest.setScheme(ZUUL_SCHEME);
    httpServletRequest.setServerPort(ZUUL_PORT);
    context.setRequest(httpServletRequest);
    MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
    context.getZuulResponseHeaders().add(new Pair<>("Location", String.format("%s%s", SERVER_BASE_URL, redirectedUri)));
    context.setResponse(httpServletResponse);
    RouteLocator routeLocator = mock(RouteLocator.class);
    when(routeLocator.getMatchingRoute(toZuulRequestUri)).thenReturn(route);
    LocationRewriteFilter filter = new LocationRewriteFilter(zuulProperties, routeLocator);
    return filter;
}
Also used : RouteLocator(org.springframework.cloud.netflix.zuul.filters.RouteLocator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 4 with RouteLocator

use of org.springframework.cloud.netflix.zuul.filters.RouteLocator in project spring-cloud-netflix by spring-cloud.

the class RoutesEndpointDetailsTests method setUp.

@Before
public void setUp() {
    this.locator = new RouteLocator() {

        @Override
        public Collection<String> getIgnoredPaths() {
            return null;
        }

        @Override
        public List<Route> getRoutes() {
            List<Route> routes = new ArrayList<>();
            routes.add(new Route("foo", "foopath", "foolocation", null, true, Collections.EMPTY_SET));
            routes.add(new Route("bar", "barpath", "barlocation", "bar-prefix", true, Collections.EMPTY_SET));
            return routes;
        }

        @Override
        public Route getMatchingRoute(String path) {
            return null;
        }
    };
    endpoint = spy(new RoutesEndpoint(locator));
}
Also used : RouteLocator(org.springframework.cloud.netflix.zuul.filters.RouteLocator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Route(org.springframework.cloud.netflix.zuul.filters.Route) Before(org.junit.Before)

Aggregations

RouteLocator (org.springframework.cloud.netflix.zuul.filters.RouteLocator)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 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 Collection (java.util.Collection)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 RoutesRefreshedEvent (org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent)1 Route (org.springframework.cloud.netflix.zuul.filters.Route)1 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Service (org.springframework.stereotype.Service)1