In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about the SpringBoot2 integration log and the custom implementation of complex business. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.
1. Log system integration 1. Log management
In the development of the system, one of the most critical component tools is log, log printing to facilitate problem troubleshooting, or production accident backtracking, log records are used to monitor and analyze system performance points, and on this basis, constantly optimize the system. At the same time, based on the user's operation log, analyze the user behavior, develop the function of intelligent recommendation, or carry out marketing, which are common and key business processes in the system.
2. ELK log system
In the large-scale system architecture, the log management system of ELK is a necessary function of the system. ELK-Stack is a combination of three open source software, Elasticsearch, Logstash and Kiban, which is usually used for log analysis and real-time data retrieval. Based on Logstash as data flow channel, log data continue to flow into the search component, real-time data query based on Elasticsearch, ES visual interface based on Kiban, so as to achieve log data collection, storage, analysis and other core functions, and the system is easy to expand.
Based on the core operation of the ELK system, there are other articles about ElasticSearch can consult the previous content, here is not on display, it seems that a lot of things are accumulated little by little.
2. Integrated environment 1. Project structure
Defined-log-api: test Engineering
Defined-log-config: log core module, which can be annotated using this module after dependency
2. Data table structure CREATE TABLE dt_defined_log (id INT (11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', class_name VARCHAR (200) DEFAULT NULL COMMENT 'request class name', method_name VARCHAR (100) DEFAULT NULL COMMENT 'request method name', method_desc VARCHAR (100) DEFAULT NULL COMMENT 'request method description', api_type INT (1) DEFAULT 0 COMMENT 'API type' Biz_nature INT (1) DEFAULT 0 COMMENT 'service nature type', data_flow_type INT (1) DEFAULT 0 COMMENT 'log data flow', req_param VARCHAR (200) DEFAULT NULL COMMENT 'request message', res_param VARCHAR (200) DEFAULT NULL COMMENT 'response message', PRIMARY KEY (`id`) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = 'log table'
Here, you can customize it entirely based on business requirements.
3. Core code description 1. Annotation parameter @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ Documentedpublic @ interface DefinedLog {/ * Operation Type * / ApiTypeEnum apiType (); / * * method description * / String methodDesc (); / * * Business Nature * / BizNatureEnum bizNature () / * data flow, associated with business nature * / DataFlowEnum dataFlow (); / * storage input parameter * / boolean isSaveReqParam () default true; / * * storage output parameter * / boolean isSaveResParam () default true; / * * whether asynchronous processing is required * / boolean isAsync () default false;}
Here describe the meaning of the following parameters:
BizNature: the nature of the business, that is, whether the log contains analysis or marketing operations, such as whether to push advertisements after browsing a series of products in e-commerce business.
DataFlow: data flow, that is, whether the data is pushed to other data sources after storage. It is common that it may be pushed to MQ or Redis or to the analysis engine. Recommended systems require very high real-time performance of key logs, based on which real-time analysis of user behavior can be done.
IsAsync: whether to process asynchronously or not, in some interfaces with high concurrency, avoid logging as a factor in performance problems.
Other related parameters are very common, such as the addition, deletion, modification and query of interface types, the storage of input and output reference messages, the description of method modules, and so on. These can be customized based on the needs of the business, and then do related business processing and development, with an open mind.
2. Section interception
Based on the aspect programming is the way, do the relevant log processing, obtain the corresponding parameters, and build the log model.
@ Component@Aspectpublic class LogAop {private static final Logger LOGGER = LoggerFactory.getLogger (LogAop.class); @ Value ("${spring.application.app-id}") private String appId; @ Resource private DefineLogService defineLogService / * log entry point * / @ Pointcut ("@ annotation (com.defined.log.annotation.DefinedLog)") public void logPointCut () {} / * surround cut into * / @ Around ("logPointCut ()") public Object around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {Object result = null; StopWatch stopWatch = new StopWatch (); stopWatch.start () Try {/ / execution method result = proceedingJoinPoint.proceed (); stopWatch.stop ();} catch (Exception e) {stopWatch.stop ();} finally {/ / Save log LOGGER.info ("execute time: {} ms", stopWatch.getTotalTimeMillis ()); DefineLogModel defineLogModel = buildLogParam (proceedingJoinPoint) DefineLogModel.setResParam (JSONObject.toJSONString (result)); defineLogService.saveLog (defineLogModel);} return result;} private DefineLogModel buildLogParam (ProceedingJoinPoint point) {DefineLogModel defineLogModel = new DefineLogModel (); MethodSignature signature = (MethodSignature) point.getSignature (); Method reqMethod = signature.getMethod (); String className = point.getTarget (). GetClass (). GetName () Object [] reqParam = point.getArgs (); LOGGER.info ("request method:" + reqMethod.getName ()); LOGGER.info ("request class name:" + className); LOGGER.info ("request parameter:" + JSONObject.toJSONString (reqParam)); / / get notes on the method reqMethod.getAnnotation (DefinedLog.class). GetClass (); DefinedLog definedLog = reqMethod.getAnnotation (DefinedLog.class) / / build parameters String methodName = reqMethod.getName (); Integer apiType = definedLog.apiType (). GetApiType (); String apiTypeDesc = definedLog.apiType (). GetApiTypeDesc (); String methodDesc = definedLog.methodDesc (); Integer bizNature = definedLog.bizNature (). GetBizNature (); Integer dataFlowType = definedLog.dataFlow (). GetDataFlowType (); boolean isSaveReqParam = definedLog.isSaveReqParam () Boolean isSaveResParam = definedLog.isSaveResParam (); boolean isAsync = definedLog.isAsync (); defineLogModel.setAppId (appId); defineLogModel.setClassName (className); defineLogModel.setMethodName (methodName); defineLogModel.setMethodDesc (methodDesc); defineLogModel.setApiType (apiType); defineLogModel.setApiTypeDesc (apiTypeDesc); defineLogModel.setBizNature (bizNature); defineLogModel.setDataFlowType (dataFlowType); defineLogModel.setSaveReqParam (isSaveReqParam) DefineLogModel.setSaveResParam (isSaveResParam); defineLogModel.setAsync (isAsync); defineLogModel.setReqParam (JSONObject.toJSONString (reqParam)); return defineLogModel;} 3, usage
DefinedLog annotations can be done on the interface method.
@ RestControllerpublic class LogController {@ GetMapping ("/ logApi") @ DefinedLog (apiType=ApiTypeEnum.COMPOSITE, methodDesc= "test log", bizNature= BizNatureEnum.DEFAULT, dataFlow= DataFlowEnum.DEFAULT) public String logApi (@ RequestParam ("param") String param) {return "success-re";}} 4, record parameters
This completes the custom logging process.
The above is what the SpringBoot2 integration log and custom implementation under complex business are like. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.