使用Java實現(xiàn)接口攔截器來監(jiān)控接口的執(zhí)行情況
在工作中,我們往往會遇到一些接口的報錯,在排查問題的時候,由于沒有對接口的執(zhí)行情況,以及入?yún)⑦M行監(jiān)控,所以排查起問題就特別費勁,今天我們就一起來寫一個接口的攔截器。
@Slf4j @Aspect @Component public class LogAspect { @SneakyThrows @Around("execution(* cn.com.alliance.we.controller..*(..))") public Object doAround(ProceedingJoinPoint pjp){ // 創(chuàng)造計時器 Stopwatch stWatch = Stopwatch.createStarted(); //獲取請求信息 ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = sra.getRequest(); String tenantId = "unknown"; try { tenantId = TenantContextHolder.getTenantId().toLowerCase(Locale.ROOT); }catch (Exception e){ log.error("LogAspect獲取租戶異常:", e); } //獲取請求地址 String requestURI = request.getRequestURI(); //組合請求參數(shù),進行日志打印 Object[] objects = pjp.getArgs(); Object[] arguments = new Object[objects.length]; for (int i = 0; i < objects.length; i++) { if (objects[i] instanceof ServletRequest || objects[i] instanceof ServletResponse || objects[i] instanceof MultipartFile) { //過濾掉不能序列化的參數(shù) continue; } arguments[i] = objects[i]; } //獲取登錄信息 OcManageUser currentUserOrNull = OcManageUserContext.getCurrentUserOrNull(); String currentOrgCode = "Unknown"; String currentEmpCode = "Unknown"; if(currentUserOrNull != null){ currentOrgCode = currentUserOrNull.getOrgCode(); currentEmpCode = currentUserOrNull.getEmpCode(); } Object result = null; try { result = pjp.proceed(); //請求操作成功 String resultJosnString = ""; if (result != null) { try{ resultJosnString = JsonUtil.bean2Json(result); }catch (Exception e){ resultJosnString = String.valueOf(result); } } //記錄請求完成執(zhí)行時間: long usedTime = stWatch.elapsed(TimeUnit.MILLISECONDS); //記錄日志 if (log.isInfoEnabled()) { log.info("URI信息:{},租戶:{},登錄人:{},登錄網(wǎng)點:{}, 耗時:{},請求參數(shù):{}, 響應參數(shù):{}", v("requestURI", requestURI),tenantId,currentEmpCode,currentOrgCode, v("elapsed", usedTime), JsonUtil.bean2Json(arguments), resultJosnString); } return result; }catch (Throwable et){ //請求異常操作 //記錄請求完成執(zhí)行時間: long usedTime = stWatch.elapsed(TimeUnit.MILLISECONDS); //記錄日志 log.error("URI信息:{},租戶:{},登錄人:{},登錄網(wǎng)點:{}, 耗時:{},請求參數(shù):{}, 響應異常信息:", v("requestURI", requestURI),tenantId,currentEmpCode,currentOrgCode, v("elapsed", usedTime), JsonUtil.bean2Json(arguments), et); throw et; } } }
寫這么一個攔截器并不復雜,主要的還是需要注意一些細節(jié),比如過濾掉一些不能序列化的參數(shù);還有就是可以根據(jù)自己的需要,將請求頭的參數(shù)也打印出來。
到此這篇關于使用Java實現(xiàn)接口攔截器來監(jiān)控接口的執(zhí)行情況的文章就介紹到這了,更多相關Java接口攔截器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot CommandLineRunner接口實現(xiàn)自動任務加載功能
這篇文章主要介紹了springboot CommandLineRunner接口實現(xiàn)自動任務加載功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05java開發(fā)實現(xiàn)訂閱到貨通知幫我們買到想買的東西
這篇文章主要為大家介紹了java開發(fā)實現(xiàn)訂閱到貨通知幫我們買到想買的東西示例demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Spring Boot集成Thymeleaf模板引擎的完整步驟
這篇文章主要給大家介紹了關于Spring Boot集成Thymeleaf模板引擎的完整步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-02-02java中優(yōu)化大量if...else...方法總結
在我們平時的開發(fā)過程中,經(jīng)??赡軙霈F(xiàn)大量If else的場景,代碼顯的很臃腫,非常不優(yōu)雅,下面這篇文章主要給大家介紹了關于java中優(yōu)化大量if...else...方法的相關資料,需要的朋友可以參考下2023-03-03