扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇内容主要讲解“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是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流