溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志

發(fā)布時間:2021-10-09 17:40:31 來源:億速云 閱讀:237 作者:iii 欄目:數(shù)據(jù)庫

這篇文章主要講解了“如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志”吧!

如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志

獲取輸入輸出參數(shù)

首先我們先定義一個日志體

@Data public class GatewayLog {     /**訪問實例*/     private String targetServer;     /**請求路徑*/     private String requestPath;     /**請求方法*/     private String requestMethod;     /**協(xié)議 */     private String schema;     /**請求體*/     private String requestBody;     /**響應(yīng)體*/     private String responseData;     /**請求ip*/     private String ip;  /**請求時間*/     private Date requestTime;  /**響應(yīng)時間*/     private Date responseTime;     /**執(zhí)行時間*/     private long executeTime; }

【關(guān)鍵】在網(wǎng)關(guān)定義日志過濾器,獲取輸入輸出參數(shù)

/**  * 日志過濾器,用于記錄日志  * @author jianzh6  * @date 2020/3/24 17:17  */ @Slf4j @Component public class AccessLogFilter implements GlobalFilter, Ordered {     @Autowired     private AccessLogService accessLogService;      private final List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();      @Override     public int getOrder() {         return -100;     }      @Override     @SuppressWarnings("unchecked")     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {          ServerHttpRequest request = exchange.getRequest();          // 請求路徑         String requestPath = request.getPath().pathWithinApplication().value();          Route route = getGatewayRoute(exchange);           String ipAddress = WebUtils.getServerHttpRequestIpAddress(request);          GatewayLog gatewayLog = new GatewayLog();         gatewayLog.setSchema(request.getURI().getScheme());         gatewayLog.setRequestMethod(request.getMethodValue());         gatewayLog.setRequestPath(requestPath);         gatewayLog.setTargetServer(route.getId());         gatewayLog.setRequestTime(new Date());         gatewayLog.setIp(ipAddress);          MediaType mediaType = request.getHeaders().getContentType();          if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){             return writeBodyLog(exchange, chain, gatewayLog);         }else{             return writeBasicLog(exchange, chain, gatewayLog);         }     }      private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog accessLog) {         StringBuilder builder = new StringBuilder();         MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();         for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {             builder.append(entry.getKey()).append("=").append(StringUtils.join(entry.getValue(), ","));         }         accessLog.setRequestBody(builder.toString());          //獲取響應(yīng)體         ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLog);          return chain.filter(exchange.mutate().response(decoratedResponse).build())                 .then(Mono.fromRunnable(() -> {                     // 打印日志                     writeAccessLog(accessLog);                 }));     }       /**      * 解決 request body 只能讀取一次問題,      * 參考: org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory      * @param exchange      * @param chain      * @param gatewayLog      * @return      */     @SuppressWarnings("unchecked")     private Mono writeBodyLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog gatewayLog) {         ServerRequest serverRequest = ServerRequest.create(exchange,messageReaders);          Mono<String> modifiedBody = serverRequest.bodyToMono(String.class)                 .flatMap(body ->{                     gatewayLog.setRequestBody(body);                     return Mono.just(body);                 });          // 通過 BodyInserter 插入 body(支持修改body), 避免 request body 只能獲取一次         BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);         HttpHeaders headers = new HttpHeaders();         headers.putAll(exchange.getRequest().getHeaders());         // the new content type will be computed by bodyInserter         // and then set in the request decorator         headers.remove(HttpHeaders.CONTENT_LENGTH);          CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);          return bodyInserter.insert(outputMessage,new BodyInserterContext())                 .then(Mono.defer(() -> {                     // 重新封裝請求                     ServerHttpRequest decoratedRequest = requestDecorate(exchange, headers, outputMessage);                      // 記錄響應(yīng)日志                     ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, gatewayLog);                      // 記錄普通的                     return chain.filter(exchange.mutate().request(decoratedRequest).response(decoratedResponse).build())                             .then(Mono.fromRunnable(() -> {                                 // 打印日志                                 writeAccessLog(gatewayLog);                             }));                 }));     }      /**      * 打印日志      * @author javadaily      * @date 2021/3/24 14:53      * @param gatewayLog 網(wǎng)關(guān)日志      */     private void writeAccessLog(GatewayLog gatewayLog) {         log.info(gatewayLog.toString());       }        private Route getGatewayRoute(ServerWebExchange exchange) {         return exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);     }       /**      * 請求裝飾器,重新計算 headers      * @param exchange      * @param headers      * @param outputMessage      * @return      */     private ServerHttpRequestDecorator requestDecorate(ServerWebExchange exchange, HttpHeaders headers,                                                        CachedBodyOutputMessage outputMessage) {         return new ServerHttpRequestDecorator(exchange.getRequest()) {             @Override             public HttpHeaders getHeaders() {                 long contentLength = headers.getContentLength();                 HttpHeaders httpHeaders = new HttpHeaders();                 httpHeaders.putAll(super.getHeaders());                 if (contentLength > 0) {                     httpHeaders.setContentLength(contentLength);                 } else {                     // TODO: this causes a 'HTTP/1.1 411 Length Required' // on                     // httpbin.org                     httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, "chunked");                 }                 return httpHeaders;             }              @Override             public Flux<DataBuffer> getBody() {                 return outputMessage.getBody();             }         };     }       /**      * 記錄響應(yīng)日志      * 通過 DataBufferFactory 解決響應(yīng)體分段傳輸問題。      */     private ServerHttpResponseDecorator recordResponseLog(ServerWebExchange exchange, GatewayLog gatewayLog) {         ServerHttpResponse response = exchange.getResponse();         DataBufferFactory bufferFactory = response.bufferFactory();          return new ServerHttpResponseDecorator(response) {             @Override             public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {                 if (body instanceof Flux) {                     Date responseTime = new Date();                     gatewayLog.setResponseTime(responseTime);                     // 計算執(zhí)行時間                     long executeTime = (responseTime.getTime() - gatewayLog.getRequestTime().getTime());                      gatewayLog.setExecuteTime(executeTime);                      // 獲取響應(yīng)類型,如果是 json 就打印                     String originalResponseContentType = exchange.getAttribute(ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);                       if (ObjectUtil.equal(this.getStatusCode(), HttpStatus.OK)                             && StringUtil.isNotBlank(originalResponseContentType)                             && originalResponseContentType.contains("application/json")) {                          Flux<? extends DataBuffer> fluxBody = Flux.from(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);                              // 釋放掉內(nèi)存                             DataBufferUtils.release(join);                             String responseResult = new String(content, StandardCharsets.UTF_8);                                gatewayLog.setResponseData(responseResult);                              return bufferFactory.wrap(content);                         }));                     }                 }                 // if body is not a flux. never got there.                 return super.writeWith(body);             }         };     } }

代碼較長建議直接拷貝到編輯器,只要注意下面一個關(guān)鍵點:

getOrder()方法返回的值必須要<-1,「否則標準的NettyWriteResponseFilter將在您的過濾器被調(diào)用的機會之前發(fā)送響應(yīng),即不會執(zhí)行獲取后端響應(yīng)參數(shù)的方法」

通過上面的兩步我們已經(jīng)可以獲取到請求的輸入輸出參數(shù)了,在  writeAccessLog()中將其輸出到了日志文件,大家可以在Postman發(fā)送請求觀察日志。

存儲日志

如果需要將日志持久化方便后期檢索的話可以考慮將日志存儲在MongoDB中,實現(xiàn)過程很簡單。(安裝MongoDB可以參考這篇文章:實戰(zhàn)|MongoDB的安裝配置)

  • 引入MongoDB

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency>

由于gateway是基于webflux,所以我們需要選擇reactive版本。

  • 在GatewayLog上添加對應(yīng)的注解

@Data @Document public class GatewayLog {     @Id     private String id;  ... }
  • 建立AccessLogRepository

@Repository public interface AccessLogRepository extends ReactiveMongoRepository<GatewayLog,String> {    }
  • 建立Service

public interface AccessLogService {      /**      * 保存AccessLog      * @param gatewayLog 請求響應(yīng)日志      * @return 響應(yīng)日志      */     Mono<GatewayLog> saveAccessLog(GatewayLog gatewayLog);  }
  • 建立實現(xiàn)類

@Service public class AccessLogServiceImpl implements AccessLogService {     @Autowired     private AccessLogRepository accessLogRepository;      @Override     public Mono<GatewayLog> saveAccessLog(GatewayLog gatewayLog) {         return accessLogRepository.insert(gatewayLog);     } }
  • 在Nacos配置中心添加MongoDB對應(yīng)配置

spring:   data:     mongodb:       host: xxx.xx.x.xx       port: 27017       database: accesslog       username: accesslog       password: xxxxxx

執(zhí)行請求,打開MongoDB客戶端,查看日志結(jié)果

如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志

感謝各位的閱讀,以上就是“如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何實現(xiàn)SpringCloud Gateway請求響應(yīng)日志這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI