本篇內容主要講解“nacos config的RequestLogAspect是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“nacos config的RequestLogAspect是什么”吧!
創新互聯建站致力于互聯網品牌建設與網絡營銷,包括成都網站設計、網站制作、SEO優化、網絡推廣、整站優化營銷策劃推廣、電子商務、移動互聯網營銷等。創新互聯建站為不同類型的客戶提供良好的互聯網應用定制及解決方案,創新互聯建站核心團隊十載專注互聯網開發,積累了豐富的網站經驗,為廣大企業客戶提供一站式企業網站建設服務,在網站建設行業內樹立了良好口碑。
本文主要研究一下nacos config的RequestLogAspect
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java
@Aspect @Component public class RequestLogAspect { /** * publish config */ private static final String CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG = "execution(* com.alibaba.nacos.config.server.controller.ConfigController.publishConfig(..)) && args" + "(request,response,dataId,group,tenant,content,..)"; /** * get config */ private static final String CLIENT_INTERFACE_GET_CONFIG = "execution(* com.alibaba.nacos.config.server.controller.ConfigController.getConfig(..)) && args(request," + "response,dataId,group,tenant,..)"; /** * remove config */ private static final String CLIENT_INTERFACE_REMOVE_ALL_CONFIG = "execution(* com.alibaba.nacos.config.server.controller.ConfigController.deleteConfig(..)) && args(request," + "response,dataId,group,tenant,..)"; /** * publishSingle */ @Around(CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG) public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant, String content) throws Throwable { final String md5 = content == null ? null : MD5.getInstance().getMD5String(content); MetricsMonitor.getPublishMonitor().incrementAndGet(); return logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5); } /** * removeAll */ @Around(CLIENT_INTERFACE_REMOVE_ALL_CONFIG) public Object interfaceRemoveAll(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant) throws Throwable { return logClientRequest("remove", pjp, request, response, dataId, group, tenant, null); } /** * getConfig */ @Around(CLIENT_INTERFACE_GET_CONFIG) public Object interfaceGetConfig(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant) throws Throwable { final String groupKey = GroupKey2.getKey(dataId, group, tenant); final String md5 = ConfigService.getContentMd5(groupKey); MetricsMonitor.getConfigMonitor().incrementAndGet(); return logClientRequest("get", pjp, request, response, dataId, group, tenant, md5); } /** * client api request log rt | status | requestIp | opType | dataId | group | datumId | md5 */ private Object logClientRequest(String requestType, ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant, String md5) throws Throwable { final String requestIp = RequestUtil.getRemoteIp(request); String appName = request.getHeader(RequestUtil.CLIENT_APPNAME_HEADER); final long st = System.currentTimeMillis(); Object retVal = pjp.proceed(); final long rt = System.currentTimeMillis() - st; // rt | status | requestIp | opType | dataId | group | datumId | md5 | // appName LogUtil.clientLog.info("{}|{}|{}|{}|{}|{}|{}|{}|{}", rt, retVal, requestIp, requestType, dataId, group, tenant, md5, appName); return retVal; } }
RequestLogAspect攔截了CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG、CLIENT_INTERFACE_GET_CONFIG、CLIENT_INTERFACE_REMOVE_ALL_CONFIG,它們都調用了logClientRequest方法,該方法會往日志打印客戶端請求的信息
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java
public class RequestUtil { private static final String X_REAL_IP = "X-Real-IP"; private static final String X_FORWARDED_FOR = "X-Forwarded-For"; private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ","; public static final String CLIENT_APPNAME_HEADER = "Client-AppName"; //...... public static String getRemoteIp(HttpServletRequest request) { String xForwardedFor = request.getHeader(X_FORWARDED_FOR); if (!StringUtils.isBlank(xForwardedFor)) { return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim(); } String nginxHeader = request.getHeader(X_REAL_IP); return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader; } //...... }
RequestUtil的getRemoteIp方法首先從header獲取X_FORWARDED_FOR,如果該值不為空,則用X_FORWARDED_FOR_SPLIT_SYMBOL分割取第一個;如果該值為空則取X_REAL_IP,如果不為空則返回,為空則取request.getRemoteAddr()
RequestLogAspect攔截了CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG、CLIENT_INTERFACE_GET_CONFIG、CLIENT_INTERFACE_REMOVE_ALL_CONFIG,它們都調用了logClientRequest方法,該方法會往日志打印客戶端請求的信息
到此,相信大家對“nacos config的RequestLogAspect是什么”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
網站名稱:nacosconfig的RequestLogAspect是什么
新聞來源:http://vcdvsql.cn/article22/gdjojc.html
成都網站建設公司_創新互聯,為您提供靜態網站、、手機網站建設、網站策劃、做網站、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯