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));
}
}
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;
}
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;
}
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));
}
Aggregations