聊聊在springcloud gateway如何获取请求体

前言

在我们扩展scg时,获取requestbody也是一个挺常见的需求了,比如记录日志,我们要获取请求体里面的内容。在HTTP协议中,服务器接收到客户端的请求时,请求体(RequestBody)通常是以流的形式传输的。这个流在设计上是只读且不可重复读取的。即request body只能读取一次,但我们很多时候是更希望这个requestbody可以被多次读取,那我们今天就来聊下这个话题

实现思路

通常我们会实现一个全局过滤器,并将过滤器的优先级调到最高。

该过滤器调到最高的原因是防止一些内置过滤器优先读取到requestbody,会导致我们这个过滤器读取到requestbody,就已经报body只能读取一次的异常。

异常如下

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: Only one connection receive subscriber allowed.
Caused by: java.lang.IllegalStateException: Only one connection receive subscriber allowed.

在这个过滤器里面我们要实现的功能如下

  1. 将原有的request请求中的body内容读出来
  2. 使用ServerHttpRequestDecorator这个请求装饰器对request进行包装,重写getBody方法
  3. 将包装后的请求放到过滤器链中传递下去

示例

@RequiredArgsConstructor
public class RequestBodyParamsFetchGlobalFilter implements Ordered, GlobalFilter {

    private final GwCommonProperty gwCommonProperty;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (isSkipFetchRequestBodyParams(exchange)) {
            return chain.filter(exchange);
        } else {
            return DataBufferUtils.join(exchange.getRequest().getBody())
                    .flatMap(dataBuffer -> {
                        DataBufferUtils.retain(dataBuffer);
                        Flux<DataBuffer> cachedFlux = Flux
                                .defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));

                         exchange.getAttributes().put(REQUEST_BODY_PARAMS_ATRR_NAME, RouteUtil.getRequestBodyParams(exchange));
                        ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(
                                exchange.getRequest()) {
                            @Override
                            public Flux<DataBuffer> getBody() {
                                return cachedFlux;
                            }
                        };
                        return chain.filter(exchange.mutate().request(mutatedRequest).build());
                    });
        }
    }

    private boolean isSkipFetchRequestBodyParams(ServerWebExchange exchange){
        if(!gwCommonProperty.isFetchRequestBodyParams()){
            return true;
        }

        if(exchange.getRequest().getHeaders().getContentType() == null && !HttpMethod.POST.name().equalsIgnoreCase(Objects.requireNonNull(exchange.getRequest().getMethod()).name())){
            return true;
        }else{
            return false;
        }
    }

@Override
public int getOrder() {
    return Ordered.HIGHEST_PRECEDENCE;
    }
}

大家如果搜索一下,scg获取请求体,有很大一部分都是这种写法。这种写法基本上是可以满足我们的需求。但是在请求压力比较大的情况下,可能会堆外内存溢出问题

reactor.netty.ReactorNetty$InternalNettyException: io.netty.util.internal.OutOfDirectMemoryError:failed to allocate

有没有更好的实现方式

我这边使用的springcloud版本是Hoxton.SR3,在这个版本我发现了一个挺好玩的过滤器

org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter

见名之意,这就是一个自适应的缓存body全局过滤器。这个过滤器的代码如下

public class AdaptCachedBodyGlobalFilter
		implements GlobalFilter, Ordered, ApplicationListener<EnableBodyCachingEvent> {

	private ConcurrentMap<String, Boolean> routesToCache = new ConcurrentHashMap<>();

	/**
	 * Cached request body key.
	 */
	@Deprecated
	public static final String CACHED_REQUEST_BODY_KEY = CACHED_REQUEST_BODY_ATTR;

	@Override
	public void onApplicationEvent(EnableBodyCachingEvent event) {
		this.routesToCache.putIfAbsent(event.getRouteId(), true);
	}

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		// the cached ServerHttpRequest is used when the ServerWebExchange can not be
		// mutated, for example, during a predicate where the body is read, but still
		// needs to be cached.
		ServerHttpRequest cachedRequest = exchange
				.getAttributeOrDefault(CACHED_SERVER_HTTP_REQUEST_DECORATOR_ATTR, null);
		if (cachedRequest != null) {
			exchange.getAttributes().remove(CACHED_SERVER_HTTP_REQUEST_DECORATOR_ATTR);
			return chain.filter(exchange.mutate().request(cachedRequest).build());
		}

		//
		DataBuffer body = exchange.getAttributeOrDefault(CACHED_REQUEST_BODY_ATTR, null);
		Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);

		if (body != null || !this.routesToCache.containsKey(route.getId())) {
			return chain.filter(exchange);
		}

		return ServerWebExchangeUtils.cacheRequestBody(exchange, (serverHttpRequest) -> {
			// don't mutate and build if same request object
			if (serverHttpRequest == exchange.getRequest()) {
				return chain.filter(exchange);
			}
			return chain.filter(exchange.mutate().request(serverHttpRequest).build());
		});
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE + 1000;
	}

}

看到这个源码,是不是有种豁然开朗的感觉,它的实现套路不就是我们上文说的实现思路吗,根据源码,我们仅需发布EnableBodyCachingEvent事件,并将要监听的routeId送入EnableBodyCachingEvent,剩下缓存requestbody的事情,就交给AdaptCachedBodyGlobalFilter来帮我们处理

示例

**
 * @see AdaptCachedBodyGlobalFilter
 */
@Configuration
@AutoConfigureAfter(GatewayAutoConfiguration.class)
@RequiredArgsConstructor
public class RequestBodyCacheConfig implements ApplicationContextAware, CommandLineRunner {


    private final RouteLocator routeDefinitionRouteLocator;
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        List<Signal<Route>> routes = routeDefinitionRouteLocator.getRoutes().materialize()
                .collect(Collectors.toList()).block();

        assert routes != null;
        routes.forEach(routeSignal -> {
            if (routeSignal.get() != null) {
                Route route = routeSignal.get();
                System.out.println(route.getId());
                publishEnableBodyCachingEvent(route.getId());
            }
        });
    }


    @EventListener
    public void refreshRoutesEvent(RefreshRoutesEvent refreshRoutesEvent){
        if(refreshRoutesEvent.getSource() instanceof NewRouteId){
            publishEnableBodyCachingEvent(((NewRouteId) refreshRoutesEvent.getSource()).getRouteId());
        }else{
            routeDefinitionRouteLocator.getRoutes().subscribe(route -> {
                publishEnableBodyCachingEvent(route.getId());
            });
        }
    }


    private void publishEnableBodyCachingEvent(String routeId){
        EnableBodyCachingEvent enableBodyCachingEvent = new EnableBodyCachingEvent(this, routeId);
        applicationContext.publishEvent(enableBodyCachingEvent);
    }


    public void addRouteRouteDefinition(RouteDefinition routeDefinition){
        NewRouteId source = NewRouteId.builder().routeId(routeDefinition.getId()).source(this).build();
        applicationContext.publishEvent(new RefreshRoutesEvent(source));
    }



    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


}

这个代码的意思就是在项目启动时,遍历一下路由,发送EnableBodyCachingEvent。并再监听RefreshRoutesEvent 事件,当有路由新增时,再次发送EnableBodyCachingEvent事件。其业务语义是让每个route都能被AdaptCachedBodyGlobalFilter处理,并缓存requestbody

发布EnableBodyCachingEvent事件的核心代码如下

  private void publishEnableBodyCachingEvent(String routeId){
        EnableBodyCachingEvent enableBodyCachingEvent = new EnableBodyCachingEvent(this, routeId);
        applicationContext.publishEvent(enableBodyCachingEvent);
    }

做完上述的事情后,我们仅需在我们需要获取requestbody的地方,写下如下代码即可

String bodyContent = null;
 DataBuffer body = exchange.getAttributeOrDefault(CACHED_REQUEST_BODY_ATTR, null);
 if(body != null){
      bodyContent = body.toString(StandardCharsets.UTF_8);
   }

总结

框架也是不断在演进,因此对于我们日常使用的框架,要多多关注下,有现成的轮子,就使用现成的轮子,现成轮子满不足不了,先看下该轮子是否有预留扩展点,如果没有,我们再考虑自己制造轮子

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway 是一个反向代理和路由器,它可以拦截进入的请求并将它们路由到不同的服务。Spring Cloud Gateway 可以通过以下步骤获取请求: 1. 在 Spring Cloud Gateway 的路由配置中,定义一个过滤器: ```java @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/foo/**") .filters(f -> f.filter(new RequestBodyFilter())) .uri("http://localhost:8080")) .build(); } ``` 这里定义了一个名为 `RequestBodyFilter` 的过滤器,用于获取请求。 2. 实现 `RequestBodyFilter` 过滤器: ```java public class RequestBodyFilter implements GatewayFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return exchange.getRequest().getBody() .flatMap(body -> { // 处理请求 byte[] bytes = new byte[body.readableByteCount()]; body.read(bytes); String requestBody = new String(bytes); System.out.println(requestBody); // 重新设置请求 DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes); exchange.getRequest().mutate().body(buffer); return chain.filter(exchange); }); } } ``` 这里的 `filter` 方法会获取请求,并进行处理。处理完成后,需要重新设置请求,以便后续的过滤器或路由器可以正确地处理请求。 3. 在请求中发送请求: ```bash curl -X POST http://localhost:8080/foo -d '{"name": "John"}' ``` 这里使用 `curl` 命令发送一个 POST 请求,并在请求中包含 JSON 数据。Spring Cloud Gateway 会拦截这个请求,并使用定义的过滤器获取请求。 以上就是 Spring Cloud Gateway 获取请求的方法。需要注意的是,获取请求可能会影响性能,因此应该谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值