• 如何解决Spring Cloud Gateway微服务网关Body只能读取一次的问题?
  • 发布于 2个月前
  • 257 热度
    0 评论
在构建微服务架构时,Spring Cloud Gateway作为一个重要的微服务网关,经常需要在过滤器(Filter)中对POST请求的Body内容进行操作,如日志记录、签名验证和权限验证等。然而,由于Request的Body只能读取一次,如果直接在过滤器中读取而不进行封装,可能导致后续服务无法获取数据。

网上搜这个问题的解决方案,大多数文章都是告诉你写一个Filter将Request的Body缓存起来。这种方法确实可以,只不过需要对代码经过充分压力测试,否则很有可能出现如下所示的堆外内存溢出问题。
// 堆代码 duidaima.com
reactor.netty.ReactorNetty$InternalNettyException: io.netty.util.internal.OutOfDirectMemoryError:failed to allocate
实际上,Spring Cloud Gateway已经内置了AdaptCachedBodyGlobalFilter过滤器,它在Exchange中巧妙地缓存了Request的Body,避免了直接读取导致的一系列问题。这种方式更为稳妥,避免了潜在的内存溢出风险。

在需要获取Body的地方,我们只需要通过以下方法即可:
DataBuffer body = exchange.getAttributeOrDefault("cachedRequestBody", null);
String bodyStr = body.toString(StandardCharsets.UTF_8);
只不过通过源码可以看出,缓存RequestBody需要路由被标记为需要缓存,也就是this.routesToCache.containsKey(rouceId)方法必须返回true。

AdaptCachedBodyGlobalFilter会监听EnableBodyCachingEvent事件,当发布该事件时就将RouteId放入routesToCache中。为了方便使用,我们可以编写一个配置类,在初始化时发布EnableBodyCachingEvent事件,将所有路由都启用缓存功能。
@Configuration(proxyBeanMethods = false)
@Slf4j
public class EnableCachedBodyConfiguration {
    // 堆代码 duidaima.com
    @Resource
    private ApplicationEventPublisher publisher;
    
    @Resource
    private GatewayProperties gatewayProperties;
    
    @PostConstruct
    public void init() {
        gatewayProperties.getRoutes().forEach(routeDefinition -> {
            // 对 spring cloud gateway 路由配置中的每个路由都启用 AdaptCachedBodyGlobalFilter
            EnableBodyCachingEvent enableBodyCachingEvent = new EnableBodyCachingEvent(new Object(), routeDefinition.getId());
            publisher.publishEvent(enableBodyCachingEvent);
        });
    }
}
通过这种方式,我们可以更加方便地处理POST请求的Body内容,避免了一些常见的问题。在实际应用中,考虑到稳定性和性能,这种解决方案提供了一种更为可靠的选择。

DailyMart是一个基于领域驱动设计(DDD)和Spring Cloud Alibaba的微服务商城系统。我们将在该系统中整合博主其他专栏文章的核心内容。如果你对这两大技术栈感兴趣,可以在本公众号回复关键词 DDD 以获取相关源码。
用户评论