溫馨提示×

溫馨提示×

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

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

nacos config的RequestLogAspect是什么

發(fā)布時(shí)間:2021-06-22 14:27:13 來源:億速云 閱讀:175 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“nacos config的RequestLogAspect是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“nacos config的RequestLogAspect是什么”吧!

本文主要研究一下nacos config的RequestLogAspect

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,它們都調(diào)用了logClientRequest方法,該方法會(huì)往日志打印客戶端請求的信息

RequestUtil.getRemoteIp

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分割取第一個(gè);如果該值為空則取X_REAL_IP,如果不為空則返回,為空則取request.getRemoteAddr()

小結(jié)

RequestLogAspect攔截了CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG、CLIENT_INTERFACE_GET_CONFIG、CLIENT_INTERFACE_REMOVE_ALL_CONFIG,它們都調(diào)用了logClientRequest方法,該方法會(huì)往日志打印客戶端請求的信息

到此,相信大家對“nacos config的RequestLogAspect是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

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

AI