Search in sources :

Example 1 with RouteDefinition

use of org.springframework.cloud.gateway.route.RouteDefinition in project spring-cloud-gateway by spring-cloud.

the class DiscoveryClientRouteDefinitionLocator method getRouteDefinitions.

@Override
public Flux<RouteDefinition> getRouteDefinitions() {
    return Flux.fromIterable(discoveryClient.getServices()).map(serviceId -> {
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.setId(this.routeIdPrefix + serviceId);
        routeDefinition.setUri(URI.create("lb://" + serviceId));
        // add a predicate that matches the url at /serviceId
        /*PredicateDefinition barePredicate = new PredicateDefinition();
					barePredicate.setName(normalizePredicateName(PathRoutePredicate.class));
					barePredicate.addArg(PATTERN_KEY, "/" + serviceId);
					routeDefinition.getPredicates().add(barePredicate);*/
        // add a predicate that matches the url at /serviceId/**
        PredicateDefinition subPredicate = new PredicateDefinition();
        subPredicate.setName(normalizeRoutePredicateName(PathRoutePredicateFactory.class));
        subPredicate.addArg(PATTERN_KEY, "/" + serviceId + "/**");
        routeDefinition.getPredicates().add(subPredicate);
        // TODO: support for other default predicates
        // add a filter that removes /serviceId by default
        FilterDefinition filter = new FilterDefinition();
        filter.setName(normalizeFilterFactoryName(RewritePathGatewayFilterFactory.class));
        String regex = "/" + serviceId + "/(?<remaining>.*)";
        String replacement = "/${remaining}";
        filter.addArg(REGEXP_KEY, regex);
        filter.addArg(REPLACEMENT_KEY, replacement);
        routeDefinition.getFilters().add(filter);
        return routeDefinition;
    });
}
Also used : FilterDefinition(org.springframework.cloud.gateway.filter.FilterDefinition) RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) PredicateDefinition(org.springframework.cloud.gateway.handler.predicate.PredicateDefinition) RewritePathGatewayFilterFactory(org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory) PathRoutePredicateFactory(org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory)

Aggregations

FilterDefinition (org.springframework.cloud.gateway.filter.FilterDefinition)1 RewritePathGatewayFilterFactory (org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory)1 PathRoutePredicateFactory (org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory)1 PredicateDefinition (org.springframework.cloud.gateway.handler.predicate.PredicateDefinition)1 RouteDefinition (org.springframework.cloud.gateway.route.RouteDefinition)1