Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is nacos config's RequestLogAspect?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the RequestLogAspect of nacos config". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the RequestLogAspect of nacos config"?

Order

This paper mainly studies the RequestLogAspect of nacos config.

RequestLogAspect

Nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java

@ Aspect@Componentpublic 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 intercepts CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG, CLIENT_INTERFACE_GET_CONFIG, and CLIENT_INTERFACE_REMOVE_ALL_CONFIG, which all call the logClientRequest method, which prints the information requested by the client to the log.

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 getRemoteIp method of RequestUtil first gets the X_FORWARDED_FOR from header, and splits the first one with X_FORWARDED_FOR_SPLIT_SYMBOL if the value is not empty; X_REAL_IP if the value is empty, returns if it is not empty, and request.getRemoteAddr () if empty

Summary

RequestLogAspect intercepts CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG, CLIENT_INTERFACE_GET_CONFIG, and CLIENT_INTERFACE_REMOVE_ALL_CONFIG, which all call the logClientRequest method, which prints the information requested by the client to the log.

At this point, I believe you have a deeper understanding of "what is the RequestLogAspect of nacos config". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report