@Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p.path("/events").and().method(HttpMethod.POST).and(). readBody(Event.class, eventPredicate("message.channels")). filters(f -> f.setPath("/messageChannel/events")).uri("lb://messageChannel")) .route(p -> p.path("/events").and().method(HttpMethod.POST).and(). readBody(Event.class, eventPredicate("message")) .filters(f -> f.setPath("/message/events")).uri("lb://message")) .build(); }
@Bean public RouteLocator myRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("test_metrics_happy_path", r -> r.host("*.metricshappypath.org") .uri(testUri)) .route("test_custom_http_status_metrics", r -> r.host("*.setcustomstatusmetrics.org") .filters(f -> f.setStatus(432)) .uri(testUri)) .build(); }
/** * Negate the logical operator. * @return a {@link BooleanSpec} to be used to add logical operators */ public BooleanSpec negate() { this.routeBuilder.negate(); return new BooleanSpec(routeBuilder, builder); }
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { String httpUri = "http://httpbin.org:80"; return builder.routes() .route(p -> p .path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri(httpUri)) .route(p -> p .host("*.hystrix.com") .filters(f -> f .hystrix(config -> config .setName("mycmd") .setFallbackUri("forward:/fallback"))) .uri(httpUri)) .build(); } // end::route-locator[]
.uri("http://localhost:9099/error").order(-102) .route("read_body_pred", r -> r.path("/test10/test2").and().readBody(String.class, s -> { JsonObject jsonObject = (JsonObject) new Gson().fromJson(s, JsonObject.class); }).filters(f -> f.addResponseHeader("X-TestHeader", "read_body_pred")) .uri("http://httpbin.org") ).route("modify_request_body", r -> r.path("/modify/test2") .filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_request_upper") .modifyRequestBody(String.class, String.class, (exchange, s) -> Mono.just(s.toUpperCase())).modifyResponseBody(String.class, String.class, ).uri("http://localhost:8005/body") .route(r -> r.path("/test5/**").and().header("X-Next-Url", ".+") .filters(f -> f.requestHeaderToRequestUri("X-Next-Url")) .uri("http://baidu.com")) .route(r -> r.path("/test6/**").and().query("url") .filters(f -> f.changeRequestUri(e -> Optional.of(URI.create( e.getRequest().getQueryParams().getFirst("url"))))) .uri("http://example.com")) .route(r -> r.path("/customer/**") .filters(f -> f.stripPrefix(2) .filter(new RateLimitByIpGatewayFilter(10, 1, Duration.ofSeconds(1)))) .uri("http://baidu.com")
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("fortune_api", p -> p.path("/v2/fortune").and().host("api.monolith.com") .filters(f -> f.setPath("/fortune") .requestRateLimiter().rateLimiter(RedisRateLimiter.class, c -> c.setBurstCapacity(1).setReplenishRate(1)) .uri("lb://fortune")) .route("fortune_rewrite", p -> p.path("/service/randomfortune") .filters(f -> f.setPath("/fortune") .hystrix(c -> c.setFallbackUri("forward:/defaultfortune"))) .uri("lb://fortune")) .route("hello_rewrite", p -> p.path("/service/hello/**") .filters(f -> f.filter((exchange, chain) -> { String name = exchange.getRequest().getQueryParams().getFirst("name"); String path = "/hello/"+name; .uri("lb://hello")) .route("index", p -> p.path("/") .filters(f -> f.setPath("/index.html")) .uri("lb://ui")) .route("ui", p -> p.path("/").or().path("/css/**").or().path("/js/**") .uri("lb://ui")) .route("monolith", p -> p.path("/**") .uri("http://localhost:8081")) .build();
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("host_multi_dsl", r -> r.host("**.hostmultidsl1.org", "**.hostmultidsl2.org") .filters(f -> f.prefixPath("/httpbin")) .uri(uri)) .build(); } }
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { //@formatter:off return builder.routes() .route("path_route", r -> r.path("/get") .uri("http://httpbin.org")) .route("host_route", r -> r.host("*.myhost.org") .uri("http://httpbin.org")) .route("rewrite_route", r -> r.host("*.rewrite.org") .filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}")) .uri("http://httpbin.org")) .route("hystrix_route", r -> r.host("*.hystrix.org") .filters(f -> f.hystrix(c -> c.setName("slowcmd"))) .uri("http://httpbin.org")) .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org") .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))) .uri("http://httpbin.org")) .route("limit_route", r -> r .host("*.limited.org").and().path("/anything/**") .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))) .uri("http://httpbin.org")) .route("websocket_route", r -> r.path("/echo") .uri("ws://localhost:9000")) .build(); //@formatter:on }
@Bean public RouteLocator sseRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("sse_route", r -> r.alwaysTrue() .uri("http://localhost:"+this.port)) .build(); } }
@Bean RouteLocator queryRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("header_exists_dsl", r -> r.header("X-Foo") .filters(f -> f.prefixPath("/httpbin")) .uri(uri)) .build(); } }
public BooleanSpec asyncPredicate(AsyncPredicate<ServerWebExchange> predicate) { this.routeBuilder.asyncPredicate(predicate); return new BooleanSpec(this.routeBuilder, this.builder); }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("test_between_valid", r -> r.host("**.betweenvalid.org") .and().between(ZonedDateTime.now().minusDays(1), ZonedDateTime.now().plusDays(1)) .filters(f -> f.prefixPath("/httpbin")) .uri(uri)) .build(); } }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("relative_redirect", r -> r.host("**.relativeredirect.org") .filters(f -> f.redirect(302, "/index.html#/customers")) .uri("no://op")) .build(); } }
@Override public BooleanSpec asyncPredicate(AsyncPredicate<ServerWebExchange> predicate) { switch (this.operator) { case AND: this.routeBuilder.and(predicate); break; case OR: this.routeBuilder.or(predicate); break; case NEGATE: this.routeBuilder.negate(); } return new BooleanSpec(this.routeBuilder, this.builder); } }
@Bean public RouteLocator myRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("test_custom_http_status", r -> r.host("*.setcustomstatus.org") .filters(f -> f.setStatus(432)) .uri(uri)) .build(); } }
@Bean RouteLocator queryRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("foo_query_param", r -> r.query("foo", "bar") .filters(f -> f.prefixPath("/httpbin")) .uri(uri)) .build(); } }