1

我有spring-cloud-gateway如下配置:

@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
                  .route(r -> r.path("/api/bank/account/user/**") //intercept calls to
                               .uri("http://mrq-bank-account.cloud.host/")
                               .id("bankModule"))
                  .build();
}

该路由预计将接受两种类型的请求:

/api/bank/account/user/savingsAccount

/api/bank/account/user/studentAccount

第一个请求携带一个SavingsAccount对象,第二个请求携带一个StudentAccount对象。这两个对象都有一个userRef:String我需要提取的属性。是否有可能通过添加它以某种方式做到这cloud-gateway-configuration一点?

基本上,我试图找出如何从上面的 spring-cloud-gateway 配置中的传入请求携带的对象中提取属性。

这是库方法,我认为可以用来实现:

package org.springframework.cloud.gateway.route.builder;


    /**
 * This predicate is BETA and may be subject to change in a future release. A
 * predicate that checks the contents of the request body
 * @param inClass the class to parse the body to
 * @param predicate a predicate to check the contents of the body
 * @param <T> the type the body is parsed to
 * @return a {@link BooleanSpec} to be used to add logical operators
 */
public <T> BooleanSpec readBody(Class<T> inClass, Predicate<T> predicate) {
    return asyncPredicate(
            getBean(ReadBodyRoutePredicateFactory.class).applyAsync(c -> c.setPredicate(inClass, predicate)));
}

但我想用它来提取读取对象的属性之一的值,然后使用该值来调用将返回主机名的服务 - 最终必须从uri()方法中使用的值

这个方法org.springframework.cloud.gateway.route.builder看起来也很有用

    /**
 * A filter which change the URI the request will be routed to by the Gateway.
 * @param determineRequestUri a {@link Function} which takes a
 * {@link ServerWebExchange} and returns a URI to route the request to
 * @return a {@link GatewayFilterSpec} that can be used to apply additional filters
 */
public GatewayFilterSpec changeRequestUri(Function<ServerWebExchange, Optional<URI>> determineRequestUri) {
    return filter(new AbstractChangeRequestUriGatewayFilterFactory<Object>(Object.class) {
        @Override
        protected Optional<URI> determineRequestUri(ServerWebExchange exchange, Object config) {
            return determineRequestUri.apply(exchange);
        }
    }.apply(c -> {
    }));
}
4

0 回答 0