目 录CONTENT

文章目录

SpringBoot整合RabbitMQ 使用自定义注解实现日志的异步存储

Sir丶雨轩
2021-05-13 / 0 评论 / 2 点赞 / 844 阅读 / 0 字 / 正在检测是否收录...

需求场景:

我们需要将系统操作日志存储至数据库,又不能影响主业务流程的实现,固这里使用消息队列进行实现

首先我们需要安装 RabbitMQ 我们这里使用的docker安装

创建容器并运行(15672是管理界面的端口,5672是服务的端口。这里顺便将管理系统的用户名和密码设置为admin admin)

docker run -dit --name Myrabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 rabbitmq:management

在Pom中添加

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
 </dependency>

定义MQ的配置

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Sir丶雨轩
 * @since 2021/5/12
 */
@Configuration
public class RabbitMQConfig {

   //交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "log_topic_exchange";
    //队列名称
    public static final String ITEM_QUEUE = "log_queue";

    //声明交换机
    @Bean("logTopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }

    //声明队列
    @Bean("logQueue")
    public Queue itemQueue(){
        return QueueBuilder.durable(ITEM_QUEUE).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding itemQueueExchange(@Qualifier("logQueue") Queue queue,
                                     @Qualifier("logTopicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("log.#").noargs();
    }
}

这里我们是使用自定义注解来进行统一的日志处理 所以需要添加注解类,切面类

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 日志处理
 *
 * @author Sir丶雨轩
 * @since 2021-05-12
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
}

切面类

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hsst.basic.common.utils.Lang;
import com.hsst.basic.common.utils.WebUtil;
import com.hsst.xiaoqingriver.manager.common.utils.StpEx;
import com.hsst.xiaoqingriver.manager.config.RabbitMQConfig;
import com.hsst.xiaoqingriver.manager.modules.system.entity.Log;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sir丶雨轩
 * @since 2021/5/12
 */
@Component
@Aspect
@Slf4j
public class LogAop {

    ThreadLocal<Long> currentTime = new ThreadLocal<>();

    private final RabbitTemplate rabbitTemplate;

    public LogAop(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Pointcut("@annotation(com.hsst.xiaoqingriver.manager.support.aop.log.annotaion.Log)")
    public void logPointcut() {
        // 该方法无方法体,主要为了让同类中其他方法使用此切入点
    }

    /**
     * 配置环绕通知,使用在方法logPointcut()上注册的切入点
     *
     * @param joinPoint join point for advice
     */
    @Around("logPointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        currentTime.set(System.currentTimeMillis());
        result = joinPoint.proceed();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        // 方法路径
        String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";

        Log log = new Log();
        log.setLogType("INFO");
        log.setRequestIp(WebUtil.getIp());
        log.setBrowser(WebUtil.getBrowser());
        log.setTime(System.currentTimeMillis() - currentTime.get());
        log.setUsername(StpEx.getLoginUser().getUsername());
        log.setAddress(WebUtil.getCityInfo(log.getRequestIp()));
        log.setCreateTime(Lang.getTime());
        log.setCreateBy(StpEx.getLoginUser().getUsername());

        log.setMethod(methodName);
        log.setParams(getParameter(method, joinPoint.getArgs()));

        com.hsst.xiaoqingriver.manager.support.aop.log.annotaion.Log logAnn = method.getAnnotation(com.hsst.xiaoqingriver.manager.support.aop.log.annotaion.Log.class);

        if(logAnn != null){
            log.setDescription(logAnn.value());
        }



        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "log.info", log);
        currentTime.remove();


        return result;
    }

    /**
     * 配置异常通知
     *
     * @param joinPoint join point for advice
     * @param e         exception
     */
    @AfterThrowing(pointcut = "logPointcut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        // 方法路径
        String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";

        Log log = new Log();
        log.setLogType("ERROR");
        log.setRequestIp(WebUtil.getIp());
        log.setBrowser(WebUtil.getBrowser());
        log.setTime(System.currentTimeMillis() - currentTime.get());
        log.setUsername(StpEx.getLoginUser().getUsername());
        log.setAddress(WebUtil.getCityInfo(log.getRequestIp()));
        log.setCreateTime(Lang.getTime());
        log.setCreateBy(StpEx.getLoginUser().getUsername());

        log.setMethod(methodName);
        log.setParams(getParameter(method, joinPoint.getArgs()));

        com.hsst.xiaoqingriver.manager.support.aop.log.annotaion.Log logAnn = method.getAnnotation(com.hsst.xiaoqingriver.manager.support.aop.log.annotaion.Log.class);

        if(logAnn != null){
            log.setDescription(logAnn.value());
        }

        log.setExceptionDetail(ExceptionUtil.getMessage(e));


        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "log.error", log);
        currentTime.remove();




    }

    /**
     * 根据方法和传入的参数获取请求参数
     */
    private String getParameter(Method method, Object[] args) {
        List<Object> argList = new ArrayList<>();
        Parameter[] parameters = method.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            //将RequestBody注解修饰的参数作为请求参数
            RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
            if (requestBody != null) {
                argList.add(args[i]);
            }
            //将RequestParam注解修饰的参数作为请求参数
            RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
            if (requestParam != null) {
                Map<String, Object> map = new HashMap<>();
                String key = parameters[i].getName();
                if (!StrUtil.isEmpty(requestParam.value())) {
                    key = requestParam.value();
                }
                map.put(key, args[i]);
                argList.add(map);
            }
        }
        if (argList.size() == 0) {
            return "";
        }
        return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList);
    }
}

rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "log.error", log); 请注意这里,log.error 这个key 需要满足配置中的条件 with("log.#")

这样我们就已经完成了大部分工作,配置MQ,生产消息,接下来我们只需要定义一个消费者来消费消息(把log具体插入的数据库中)就ok了

import com.hsst.xiaoqingriver.manager.config.RabbitMQConfig;
import com.hsst.xiaoqingriver.manager.modules.system.entity.Log;
import com.hsst.xiaoqingriver.manager.modules.system.mapper.LogMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Sir丶雨轩
 * @since 2021/5/12
 */
@Component
public class LogMQListener {

    @Resource
    private LogMapper logMapper;

    @RabbitListener(queues = RabbitMQConfig.ITEM_QUEUE)
    public void log(Log log){
        logMapper.insert(log);
    }
}
2
广告 广告

评论区