Talk nacos config of RequestLogAspect

sequence

In this paper, we look nacos config of 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 intercepted CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG, CLIENT_INTERFACE_GET_CONFIG, CLIENT_INTERFACE_REMOVE_ALL_CONFIG, they call logClientRequest method, which will print request to log client information

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;
    }

    //......

}
复制代码
  • The method first obtains RequestUtil getRemoteIp from header X_FORWARDED_FOR, if the value is not null, then take the first division with X_FORWARDED_FOR_SPLIT_SYMBOL; if the value is empty take X_REAL_IP, if not null is returned, the empty take request.getRemoteAddr ( )

summary

RequestLogAspect intercepted CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG, CLIENT_INTERFACE_GET_CONFIG, CLIENT_INTERFACE_REMOVE_ALL_CONFIG, they call logClientRequest method, which will print request to log client information

doc

Guess you like

Origin juejin.im/post/5d712572f265da03e71b1677