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

How to implement Business Log component with SpringCloud Alibaba

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use SpringCloud Alibaba to achieve business log components". In daily operation, I believe many people have doubts about how to use SpringCloud Alibaba to achieve business log components. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use SpringCloud Alibaba to achieve business log components". Next, please follow the editor to study!

Overview

In a single project, if we need to record the operation log, it is generally achieved by the following means:

Create a custom annotation to mark the type of business operation

Assemble log entities through AOP to complete log collection

But in the micro-service architecture, it is impossible for every service to write a custom annotation and then an AOP, which obviously violates the spirit of Don't repeat yourself. So at this time, we usually set up a common component to collect logs in the common component, and the back-end service only needs to introduce this common component.

SpringBoot Starter

To achieve the above functions we need to use SringBoot Starter to achieve, one of the major advantages of SpringBoot is Starter, through Starter we can encapsulate common business logic and parameter initialization, if you are in micro-service development, the preparation of Starter must be mastered.

Here we briefly mention the process of SpringBoot Starter to realize automatic configuration.

When spring-boot starts, it will find the resources/META-INF/spring.factories file in the starterjar package, and according to the configuration in the spring.factories file, find the class that needs to be automatically configured, xxxAutoConfigure

Bind the properties of the current module to "Environment" through the annotation @ EnableConfigurationProperties on xxxAutoConfigure, if any.

It is automatically assembled into the IOC container through the bean defined in xxxAutoConfigure.

Actual combat

The process is as follows:

First, we set up a module for starter, such as cloud-component-logging-starter, in the project

Write the configuration class SysLogAutoConfigure

@ Configuration public class SysLogAutoConfigure {@ Bean public SysLogAspect controllerLogAspect () {return new SysLogAspect ();}}

We injected a log aspect SysLogAspect into SysLogAutoConfigure, and since the log collection tool does not require additional configuration properties, we do not need to define property configuration classes.

Custom log annotation SysLog

@ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ Documented public @ interface SysLog {/ * log content * @ return {String} * / String value ();}

Define log section SysLogAspect

@ Aspect public class SysLogAspect {private final Logger log = LoggerFactory.getLogger (this.getClass ()); @ Pointcut ("@ annotation (com.javadaily.component.logging.annotation.SysLog)") public void logPointCut () {} @ Around ("logPointCut ()") public Object around (ProceedingJoinPoint pjp) throws Throwable {MethodSignature signature = (MethodSignature) pjp.getSignature (); Method method = signature.getMethod () / / Class name String className = pjp.getTarget (). GetClass (). GetName (); / / method name String methodName = signature.getName (); SysLog syslog = method.getAnnotation (SysLog.class); / / Operation String operator = syslog.value (); long beginTime = System.currentTimeMillis (); Object returnValue = null; Exception ex = null Try {returnValue = pjp.proceed (); return returnValue;} catch (Exception e) {ex = e; throw e;} finally {long cost = System.currentTimeMillis ()-beginTime If (ex! = null) {log.error ("[class: {}] [method: {}] [operator: {}] [cost: {} ms] [args: {}] [exception]", className, methodName, operator, pjp.getArgs (), ex) } else {log.info ("[class: {}] [method: {}] [operator: {}] [cost: {} ms] [args: {}] [return: {}]", className, methodName, operator, cost, pjp.getArgs (), returnValue);}

The above aspect indicates that logs are automatically collected for methods that use the @ SysLog annotation to enter logs into the log file.

Set up the spring.factories file in the resource/META-INF directory and load the configuration class SysLogAutoConfigure

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.javadaily.component.logging.configure.SysLogAutoConfigure

Introducing log components into microservices

Com.jianzh6.cloud cloud-component-logging-starter 1.0.0

Add @ SysLog annotations to the methods that require log collection

SysLog ("find users") public ResultData getByCode (@ PathVariable (value = "accountCode") String accountCode) {log.warn ("get account detail,accountCode is: {}", accountCode); SecurityUser securityUser = SecurityUtils.getUser (); log.info (securityUser); AccountDTO accountDTO = accountService.selectByCode (accountCode); return ResultData.success (accountDTO) At this point, the study on "how to implement business log components with SpringCloud Alibaba" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report