Spring Cloud Gateway 攔截響應問題分析(數據截斷問題)
Spring Cloud Gateway是Spring 官方基于Spring 5.0、Spring Boot 2.0和Project Reactor等技術開發(fā)的網關,Spring Cloud Gateway旨在為微服務架構提供一種簡單有效的、統一的 API 路由管理方式。
Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網關,其目標是替代 Netflix Zuul,它不僅提供統一的路由方式,并且基于Filter鏈的方式提供了網關基本的功能,如:安全、監(jiān)控/埋點和限流等。
Spring Cloud Gateway依賴Spring Boot和Spring WebFlux,基于Netty 運行。不能在傳統的 servlet 容器中工作也不能構建成war包。
關于Spring Cloud Gateway 核心概念
1、Route
Route 是網關的基礎元素,由 ID、目標 URI、斷言、過濾器組成。當請求到達網關時,由 Gateway HandlerMapping 通過斷言進行路由匹配(Mapping),斷言為真時匹配到路由。
2、Predicate
Predicate 是 Java 8 中提供的一個函數。輸入類型是 Spring Framework ServerWebExchange。它允許開發(fā)人員匹配來自 HTTP 的請求,例如請求頭或者請求參數。簡單來說它就是匹配條件。
3、Filter
Filter是Gateway 中的過濾器,可以在請求發(fā)出前后進行一些業(yè)務上的處理。
Spring Cloud Gateway 攔截響應
最近因為上鏈路追蹤后發(fā)現如果在服務層將異常攔截掉,在鏈路追蹤界面上就不會顯示異常鏈路信息,除了服務異常意外,系統的異常不會觸發(fā)鏈路error,所以對服務層做了個變更,將所有未處理異常直接捕獲后統一封裝完拋出,這個時候就需要在網關層統一處理那么網關需要對響應數據進行攔截如果是 9999錯誤碼,則封裝后返回,如果是其它響應碼或者其它數據直接返回。
起初寫法測試后發(fā)現數據過長時被截斷。
return super.writeWith(fluxBody.map(dataBuffer -> {
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
// 釋放掉內存
DataBufferUtils.release(dataBuffer);
String str = new String(content, Charset.forName("UTF-8"));
originalResponse.getHeaders().setContentLength(str.getBytes().length);
log.error("gateway catch service exception error:"+ str);
JsonResult result = new JsonResult();
result.setCode(ErrorCode.SYS_EXCEPTION.getCode());
result.setMessage(ErrorCode.SYS_EXCEPTION.getMsg());
return bufferFactory.wrap(str.getBytes());
})); 查詢api后發(fā)現存在一個DataBufferFactory可以一次性join完所有數據后拼接就不會產生截斷問題。
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
于是修改代碼如下
package com.server.gateway.filters;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.http.protocol.HTTP;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import com.framework.common.enums.ErrorCode;
import com.framework.common.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Component
@Slf4j
public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered{
@Override
public int getOrder() {
// -1 is response write filter, must be called before that
return -2;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpResponse originalResponse = exchange.getResponse();
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
AtomicReference<String> bodyRef = new AtomicReference<>();
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
// 釋放掉內存
DataBufferUtils.release(join);
String str = new String(content, Charset.forName("UTF-8"));
originalResponse.getHeaders().setContentLength(str.getBytes().length);
log.error("gateway catch service exception error:"+ str);
JsonResult result = new JsonResult();
result.setCode(ErrorCode.SYS_EXCEPTION.getCode());
result.setMessage(ErrorCode.SYS_EXCEPTION.getMsg());
return bufferFactory.wrap(str.getBytes());
}));
}
// if body is not a flux. never got there.
return super.writeWith(body);
}
};
// replace response with decorator
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
}經過如上修改后鏈路追蹤可以實現哪個服務出錯就立馬出現鏈路異常馬上可以定位到哪個服務出現了未處理異常

到此這篇關于Spring Cloud Gateway 攔截響應(數據截斷問題)的文章就介紹到這了,更多相關Spring Cloud Gateway 攔截響應內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot項目中application.properties無法變成小樹葉問題解決方案
這篇文章主要介紹了springboot項目中application.properties無法變成小樹葉問題解決,本文通過圖文實例代碼相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
SpringBoot應用能直接運行java -jar的原因分析
這篇文章主要介紹了SpringBoot應用為什么能直接運行java -jar,首先明確一點,普通jar包是不能直接運行的,比如工具類jar,要能運行,至少得要一個main函數作為入口吧?本文給大家介紹了詳細的原因分析,需要的朋友可以參考下2024-03-03
Java for循環(huán)Map集合優(yōu)化實現解析
這篇文章主要介紹了Java for循環(huán)Map集合優(yōu)化實現解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
SpringBoot+MyBatis簡單數據訪問應用的實例代碼
這篇文章主要介紹了SpringBoot+MyBatis簡單數據訪問應用的實例代碼,需要的朋友可以參考下2017-05-05

