org.springframework.cloud.gateway.route.RouteLocator Java Examples
The following examples show how to use
org.springframework.cloud.gateway.route.RouteLocator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RouteBuilderTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Test public void testRouteOptionsPropagatedToRoute() { Map<String, Object> routeMetadata = Maps.newHashMap("key", "value"); RouteLocator routeLocator = this.routeLocatorBuilder.routes() .route("test1", r -> r.host("*.somehost.org").and().path("/somepath") .filters(f -> f.addRequestHeader("header1", "header-value-1")) .metadata("key", "value").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")) && r.getMetadata().equals(routeMetadata)) .expectNextMatches(r -> r.getId().equals("test2") && r.getFilters().size() == 1 && r.getUri().equals(URI.create("https://httpbin.org:9090")) && r.getMetadata().isEmpty()) .expectComplete().verify(); }
Example #2
Source File: ReservationClientApplication.java From bootiful-reactive-microservices with Apache License 2.0 | 6 votes |
@Bean RouteLocator gateway(RouteLocatorBuilder rlb) { return rlb .routes() .route( rSpec -> rSpec .host("*.foo.com").and().path("/proxy") .filters( spec -> spec .addResponseHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*") .setPath("/reservations") .requestRateLimiter(config -> config.setRateLimiter(redisRateLimiter())) ) .uri("lb://reservation-service") ) .build(); }
Example #3
Source File: ReservationClientApplication.java From bootiful-reactive-microservices with Apache License 2.0 | 6 votes |
@Bean RouteLocator gateway(RouteLocatorBuilder rlb) { return rlb .routes() .route( routeSpec -> routeSpec .host("*.spring.com").and().path("/proxy") .filters(fSpec -> fSpec .setPath("/reservations") .addResponseHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*") .requestRateLimiter(rl -> rl .setRateLimiter(rl()) ) ) .uri("http://localhost:8080") ) .build(); }
Example #4
Source File: RoutePredicateHandlerMappingTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Test public void lookupRouteFromSyncPredicates() { Route routeFalse = Route.async().id("routeFalse").uri("http://localhost") .predicate(swe -> false).build(); Route routeFail = Route.async().id("routeFail").uri("http://localhost") .predicate(swe -> { throw new IllegalStateException("boom"); }).build(); Route routeTrue = Route.async().id("routeTrue").uri("http://localhost") .predicate(swe -> true).build(); RouteLocator routeLocator = () -> Flux.just(routeFalse, routeFail, routeTrue) .hide(); RoutePredicateHandlerMapping mapping = new RoutePredicateHandlerMapping(null, routeLocator, new GlobalCorsProperties(), new MockEnvironment()); final Mono<Route> routeMono = mapping .lookupRoute(Mockito.mock(ServerWebExchange.class)); StepVerifier.create(routeMono.map(Route::getId)).expectNext("routeTrue") .verifyComplete(); outputCapture .expect(containsString("Error applying predicate for route: routeFail")); outputCapture.expect(containsString("java.lang.IllegalStateException: boom")); }
Example #5
Source File: TweetClientApplication.java From reactive-spring-online-training with Apache License 2.0 | 6 votes |
@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(); }
Example #6
Source File: RedisRateLimiterConfigTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("custom_redis_rate_limiter", r -> r.path("/custom").filters(f -> f.requestRateLimiter() .rateLimiter(RedisRateLimiter.class, rl -> rl.setBurstCapacity(40).setReplenishRate(20) .setRequestedTokens(10)) .and()).uri("http://localhost")) .route("alt_custom_redis_rate_limiter", r -> r.path("/custom") .filters(f -> f.requestRateLimiter( c -> c.setRateLimiter(myRateLimiter()))) .uri("http://localhost")) .build(); }
Example #7
Source File: GateWayConfig.java From codeway_service with GNU General Public License v3.0 | 6 votes |
/** * 自定义拦截规则 * id:固定,不同 id 对应不同的功能,可参考 官方文档 * uri:目标服务地址 * predicates:路由条件 * filters:过滤规则 * stripPrefix(0):剥夺前缀数字为几则删除路径上的几位,stripPrefix(2) /name/bar/foo -》 http://nameservice/foo. * lb = load-balanced(负载均衡) * @param builder * @return RouteLocator */ @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() // 用户服务 .route("user_route", r -> r.path("/su/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-USER")) // 基础服务 .route("base_route", r -> r.path("/ba/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-BASE")) // 文章服务 .route("article_route", a -> a.path("/ar/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-ARTICLE")) // 授权、鉴权、第三方登录 .route("auth_route", a -> a.path("/oauth/**").filters(f -> f.stripPrefix(0)).uri("lb://AUTHENTICATION-SERVER")) .route("social_route", a -> a.path("/social/**").filters(f -> f.stripPrefix(1)).uri("lb://AUTHENTICATION-SERVER")) // API .route("api_base_route", r -> r.path("/api/ba/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-BASE")) .route("api_article_route", a -> a.path("/api/ar/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-ARTICLE")) .route("api_user_route", a -> a.path("/api/su/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-USER")) .build(); }
Example #8
Source File: RouteConfiguration.java From microservice-integration with MIT License | 6 votes |
@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(); }
Example #9
Source File: ModifyRequestBodyGatewayFilterFactorySslTimeoutTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Bean @DependsOn("testModifyRequestBodyGatewayFilterFactory") public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("test_modify_request_body_ssl_timeout", r -> r.order(-1).host("**.modifyrequestbodyssltimeout.org") .filters(f -> f.modifyRequestBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE, (serverWebExchange, aVoid) -> { byte[] largeBody = new byte[10 * 1024 * 1024]; return Mono.just(new String(largeBody)); })) .uri(uri)) .route("test_modify_request_body_exception", r -> r.order(-1) .host("**.modifyrequestbodyexception.org") .filters(f -> f.modifyRequestBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE, (serverWebExchange, body) -> { return Mono.error( new Exception("modify body exception")); })) .uri(uri)) .build(); }
Example #10
Source File: JeecgGatewayApplication.java From jeecg-cloud with Apache License 2.0 | 6 votes |
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/get") .uri("http://httpbin.org")) .route("baidu_path_route", r -> r.path("/baidu") .uri("https://news.baidu.com/guonei")) .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")) .build(); }
Example #11
Source File: RouteBuilderTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@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(); }
Example #12
Source File: RetryGatewayFilterFactoryIntegrationTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@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) .setMethods(HttpMethod.POST, HttpMethod.GET))) .uri(uri)) .route("retry_only_get", r -> r.host("**.retry-only-get.org") .filters(f -> f.prefixPath("/httpbin") .retry(config -> config.setRetries(2) .setMethods(HttpMethod.GET))) .uri(uri)) .route("retry_with_backoff", r -> r.host("**.retrywithbackoff.org") .filters(f -> f.prefixPath("/httpbin").retry(config -> { config.setRetries(2).setBackoff(Duration.ofMillis(100), null, 2, true); })).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(); }
Example #13
Source File: GateWayConfig.java From codeway_service with GNU General Public License v3.0 | 6 votes |
/** * 自定义拦截规则 * id:固定,不同 id 对应不同的功能,可参考 官方文档 * uri:目标服务地址 * predicates:路由条件 * filters:过滤规则 * stripPrefix(0):剥夺前缀数字为几则删除路径上的几位,stripPrefix(2) /name/bar/foo -》 http://nameservice/foo. * lb = load-balanced(负载均衡) * @param builder * @return RouteLocator */ @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() // 用户服务 .route("user_route", r -> r.path("/su/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-USER")) // 基础服务 .route("base_route", r -> r.path("/ba/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-BASE")) // 文章服务 .route("article_route", a -> a.path("/ar/**").filters(f -> f.stripPrefix(1)).uri("lb://SERVICE-ARTICLE")) // 授权、鉴权、第三方登录 .route("auth_route", a -> a.path("/oauth/**").filters(f -> f.stripPrefix(0)).uri("lb://AUTHENTICATION-SERVER")) .route("social_route", a -> a.path("/social/**").filters(f -> f.stripPrefix(1)).uri("lb://AUTHENTICATION-SERVER")) // API .route("api_base_route", r -> r.path("/api/ba/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-BASE")) .route("api_article_route", a -> a.path("/api/ar/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-ARTICLE")) .route("api_user_route", a -> a.path("/api/su/**").filters(f -> f.stripPrefix(0)).uri("lb://SERVICE-USER")) .build(); }
Example #14
Source File: BetweenRoutePredicateFactoryIntegrationTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #15
Source File: RouteBuilderTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Test public void testRoutesWithTimeout() { RouteLocator routeLocator = this.routeLocatorBuilder.routes() .route("test1", r -> { return r.host("*.somehost.org").and().path("/somepath") .filters(f -> f.addRequestHeader("header1", "header-value-1")) .metadata(RESPONSE_TIMEOUT_ATTR, 1) .metadata(CONNECT_TIMEOUT_ATTR, 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")) && r.getMetadata().get(RESPONSE_TIMEOUT_ATTR).equals(1) && r.getMetadata().get(CONNECT_TIMEOUT_ATTR).equals(1)) .expectNextMatches( r -> r.getId().equals("test2") && r.getFilters().size() == 1 && r.getUri() .equals(URI.create("https://httpbin.org:9090"))) .expectComplete().verify(); }
Example #16
Source File: RequestHeaderToRequestUriGatewayFilterFactoryIntegrationTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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("https://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("https://example.com")) .build(); }
Example #17
Source File: AddRequestParameterGatewayFilterFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("add_request_param_java_test", r -> r.path("/get").and().host("{sub}.addreqparamjava.org") .filters(f -> f.prefixPath("/httpbin") .addRequestParameter("example", "ValueB-{sub}")) .uri(uri)) .build(); }
Example #18
Source File: RedirectToGatewayFilterFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #19
Source File: SetResponseHeaderGatewayFilterFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("test_set_response_header_dsl", r -> r.order(-1).host("{sub}.setresponseheaderdsl.org") .filters(f -> f.prefixPath("/httpbin") .addResponseHeader("X-Res-Foo", "First") .setResponseHeader("X-Res-Foo", "Second-{sub}")) .uri(uri)) .build(); }
Example #20
Source File: SetStatusGatewayFilterFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public RouteLocator enumRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("test_enum_http_status", r -> r.host("*.setenumstatus.org") .filters(f -> f.setStatus(HttpStatus.UNAUTHORIZED)).uri(uri)) .build(); }
Example #21
Source File: GatewayAutoConfiguration.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean @Conditional(OnVerboseDisabledCondition.class) @ConditionalOnAvailableEndpoint public GatewayLegacyControllerEndpoint gatewayLegacyControllerEndpoint( RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters, List<GatewayFilterFactory> gatewayFilters, List<RoutePredicateFactory> routePredicates, RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) { return new GatewayLegacyControllerEndpoint(routeDefinitionLocator, globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter, routeLocator); }
Example #22
Source File: RemoteAddrRoutePredicateFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("x_forwarded_for_test", r -> r .path("/xforwardfor").and() .remoteAddr(XForwardedRemoteAddressResolver.maxTrustedIndex(1), "12.34.56.78") .filters(f -> f.setStatus(200)).uri(uri)).build(); }
Example #23
Source File: CloudFoundryRouteServiceRoutePredicateFactoryIntegrationTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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("https://example.com")).build(); }
Example #24
Source File: GatewayControllerEndpointTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean RouteLocator testRouteLocator(RouteLocatorBuilder routeLocatorBuilder) { return routeLocatorBuilder.routes() .route("test-service", r -> r.path("/test-service/**").uri("lb://test-service")) .build(); }
Example #25
Source File: ReadBodyPredicateFactoryTest.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #26
Source File: HostRoutePredicateFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #27
Source File: PathRoutePredicateFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #28
Source File: WeightRoutePredicateFactoryIntegrationTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }
Example #29
Source File: MethodRoutePredicateFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public RouteLocator testRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("method_test_get", r -> r.method("GET").and().path("/get") .filters(f -> f.prefixPath("/httpbin")).uri(uri)) .route("method_test_get_and_post", r -> r.method("GET", "POST").and().path("/multivalueheaders") .filters(f -> f.prefixPath("/httpbin")).uri(uri)) .build(); }
Example #30
Source File: QueryRoutePredicateFactoryTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@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(); }