springboot异步和切面_springboot中实现rabbitmq异步日志记录功能

本文介绍了如何在SpringBoot应用中利用AOP和RabbitMQ实现日志记录的异步处理。通过创建一个LogAspect切面,定义切入点并使用@Before注解实现前置通知,在方法调用前记录日志信息,包括请求内容、类型和IP等。日志信息首先保存到数据库,然后通过RabbitTemplate发送到消息队列,实现了日志处理的异步化,提高了系统性能。
摘要由CSDN通过智能技术生成

package com.seecen.redis.aop;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.seecen.redis.entity.TLog;

import com.seecen.redis.mapper.TLogMapper;

import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.Signature;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestAttributes;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Method;

import java.util.Map;/**

* @author bigpeng

* @create 2020-07-22-10:17*/@Aspect

@Component

@Slf4jpublic classLogAspect {

@AutowiredprivateTLogMapper logMapper;

@AutowiredprivateRabbitTemplate rabbitTemplate;//定义切入点,使用了该注解的方法将被AOP切入

@Pointcut("@annotation( com.seecen.redis.aop.Log)")public voidlogPointCut(){}/**

* 使用前置通知来完成日志的记录

* @param joinPoint 连接点信息*/@Before("logPointCut()")public voidlogBefore(JoinPoint joinPoint) throws JsonProcessingException {

MethodSignature signature=(MethodSignature) joinPoint.getSignature();

Method method=signature.getMethod();

Log logAnnotation= method.getAnnotation(Log.class);

String type=logAnnotation.logType().getType();

String content=logAnnotation.content();//获取request对象

ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request=requestAttributes.getRequest();//请求的IP地址

String ip =request.getRemoteAddr();//请求数据

Map data =request.getParameterMap();

String dataJsonStr="";if(data!=null) {

ObjectMapper objectMapper= newObjectMapper();

dataJsonStr=objectMapper.writeValueAsString(data);

}

TLog tLog= newTLog();

tLog.setContent(content);

tLog.setData(dataJsonStr);

tLog.setIp(ip);

tLog.setType(type);//log {}表示占位符

log.info("请求日志信息:{}",tLog);//插入数据库

long start =System.currentTimeMillis();

logMapper.insertSelective(tLog);

log.info("直接插入数据库耗时:{}",System.currentTimeMillis()-start);

start=System.currentTimeMillis();//发消息队列

rabbitTemplate.convertAndSend("log.exchange","aopLog",tLog);

log.info("发送mq消息耗时:{}",System.currentTimeMillis()-start);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值