.route(r -> r.host("**.abc.org").and().path("/anything/png") .filters(f -> f.prefixPath("/httpbin") .and().readBody(String.class, s -> s.trim().equalsIgnoreCase("hi")) .filters(f -> f.prefixPath("/httpbin") .host("**.throttle.org").and().path("/get") .filters(f -> f.prefixPath("/httpbin") .filter(new ThrottleGatewayFilter()
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("add_request_header_java_test", r -> r.path("/headers").and().host("**.addrequestheaderjava.org") .filters(f -> f.prefixPath("/httpbin").addRequestHeader("X-Request-Acme", "ValueB")) .uri(uri)) .build(); } }
@Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.host("**.changeuri.org").and().header("X-Next-Url") .filters(f -> f.requestHeaderToRequestUri("X-Next-Url")) .uri("http://example.com")) .route(r -> r.host("**.changeuri.org").and().query("url") .filters(f -> f.changeRequestUri(e -> Optional.of(URI.create( e.getRequest().getQueryParams().getFirst("url"))))) .uri("http://example.com")) .build(); } }
@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, .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("/**")
@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 }
.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); ).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")))))
@Bean RouteLocator gateway(RouteLocatorBuilder rlb) { return rlb .routes() .route(rSpec -> rSpec .path("/proxy").and().host("*.foo.gw") .filters(fSpec -> fSpec.requestRateLimiter(config -> config .setRateLimiter(this.redisRateLimiter()) .setKeyResolver(new PrincipalNameKeyResolver()) )) .uri("http://localhost:8080/hashtags") ) .build(); }
@Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.host("**.changeuri.org").and().header("X-Next-Url") .filters(f -> f.requestHeaderToRequestUri("X-Next-Url")) .uri("http://blueskykong.com")) .route(r -> r.host("**.changeuri.org").and().query("url") .filters(f -> f.changeRequestUri(e -> Optional.of(URI.create( e.getRequest().getQueryParams().getFirst("url"))))) .uri("http://blueskykong.com")) .build(); }
@Test public void testASetOfRoutes() { RouteLocator routeLocator = this.routeLocatorBuilder.routes() .route("test1", r -> r.host("*.somehost.org").and().path("/somepath") .filters(f -> f.addRequestHeader("header1", "header-value-1")) .uri("http://someuri")) .route("test2", r -> r.host("*.somehost2.org") .filters(f -> f.addResponseHeader("header-response-1", "header-response-1")) .uri("https://httpbin.org:9090")) .build(); StepVerifier.create(routeLocator.getRoutes()) .expectNextMatches( r -> r.getId().equals("test1") && r.getFilters().size() == 1 && r.getUri().equals(URI.create("http://someuri:80"))) .expectNextMatches( r -> r.getId().equals("test2") && r.getFilters().size() == 1 && r.getUri() .equals(URI.create("https://httpbin.org:9090"))) .expectComplete().verify(); }
public BooleanSpec predicate(Predicate<ServerWebExchange> predicate) { return asyncPredicate(toAsyncPredicate(predicate)); }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("add_response_param_java_test", r -> r.path("/get").and().host("**.addresponseparamjava.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("example", "myresponsevalue")) .uri(uri)) .build(); } }
@Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes().route(r -> r.cloudFoundryRouteService().and() .header("Host", "dsl.routeservice.example.com") .filters(f -> f.requestHeaderToRequestUri("X-CF-Forwarded-Url")) .uri("http://example.com")).build(); } }
@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(); }
/** * Apply logical {@code or} operator. * @return a {@link BooleanSpec} to be used to add logical operators */ public BooleanOpSpec or() { return new BooleanOpSpec(routeBuilder, builder, OR); }
/** * Apply logical {@code and} operator. * @return a {@link BooleanSpec} to be used to add logical operators */ public BooleanOpSpec and() { return new BooleanOpSpec(routeBuilder, builder, AND); }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_multi_dsl", r -> r.host("**.pathmultidsl.org") .and().path(false, "/anything/multidsl1", "/anything/multidsl3") .filters(f -> f.prefixPath("/httpbin")) .uri(uri)) .build(); } }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("add_request_param_java_test", r -> r.path("/get").and().host("**.addreqparamjava.org") .filters(f -> f.prefixPath("/httpbin").addRequestParameter("example", "ValueB")) .uri(uri)) .build(); } }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("weight_low_test", r -> r.weight("group1", 2) .and().host("**.weightlow.org") .filters(f -> f.prefixPath("/httpbin")) .uri(this.uri)) .build(); }