@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(); }
@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[]
@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 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(); } }
@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 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(); } }
.route(r -> r.host("**.abc.org").and().path("/anything/png") .filters(f -> f.prefixPath("/httpbin") .route("read_body_pred", r -> r.host("*.readbody.org") .and().readBody(String.class, s -> s.trim().equalsIgnoreCase("hi")) .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("X-TestHeader", "rewrite_request") .route("rewrite_request_upper", r -> r.host("*.rewriterequestupper.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("X-TestHeader", "rewrite_request_upper") .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("X-TestHeader", "rewrite_response_upper") .route("rewrite_response_obj", r -> r.host("*.rewriteresponseobj.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("X-TestHeader", "rewrite_response_obj") .host("**.throttle.org").and().path("/get") .filters(f -> f.prefixPath("/httpbin") .filter(new ThrottleGatewayFilter()
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("test_request_size", r -> r.order(-1).host("**.setrequestsize.org").filters(f -> f.setRequestSize(5000000L)).uri(uri)) .build(); } }
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("test_set_request_header", r -> r.order(-1) .host("**.setrequestheader.org") .filters(f -> f.prefixPath("/httpbin") .addRequestHeader("X-Req-Foo", "First") .setRequestHeader("X-Req-Foo", "Second")) .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 hystrixRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("retry_java", r -> r.host("**.retryjava.org") .filters(f -> f.prefixPath("/httpbin") .retry(config -> config.setRetries(2))) .uri(uri)) .route("retry_with_loadbalancer", r -> r.host("**.retrywithloadbalancer.org") .filters(f -> f.prefixPath("/httpbin") .retry(config -> config.setRetries(2))) .uri("lb://badservice2")) .build(); } }
@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("test_preserve_host_header", r -> r.order(-1) .host("**.preservehostheader.org") .filters(f -> f.prefixPath("/httpbin") .preserveHostHeader() .setRequestHeader("Host", "myhost.net")) .uri(uri)) .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(); }
@Bean public RouteLocator hystrixRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("hystrix_java", r -> r.host("**.hystrixjava.org") .filters(f -> f.prefixPath("/httpbin") .hystrix(config -> config.setFallbackUri("forward:/fallbackcontroller2"))) .uri(uri)) .route("hystrix_connection_failure", r -> r.host("**.hystrixconnectfail.org") .filters(f -> f.prefixPath("/httpbin") .hystrix(config -> { })) .uri("lb:badservice")) /* * This is a route encapsulated in a hystrix command that is ready to wait * for a response far longer than the underpinning WebClient would. */ .route("hystrix_response_stall", r -> r.host("**.hystrixresponsestall.org") .filters(f -> f.prefixPath("/httpbin") .hystrix(config -> config.setName("stalling-command"))) .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://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(); }
@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 }