业务需求需要将http请求参数与返回值采集,考虑使用AOP形式,获取到请求值与返回值之后将数据封装为json字符串发送到rabbitMQ中,消费者消费数据后将处理后的数据保存至mongodb或其他数据库中,以下为设置代码构建:
自定义注解
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
}
Aspect
//基于注解的切入点
@Pointcut("@annotation(com.youjin.log.annotation.SystemLog)")
public void serviceAspect() {
}
/**
* 正常返回通知,拦截service层记录用户正常的日志
* @param joinPoint
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@AfterReturning(returning = "ret",pointcut = "serviceAspect()")
public void doAfter(JoinPoint joinPoint,Object ret) throws Exception {
Result result = (Result) ret;
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//获取参数
Map param = (Map)joinPoint.getArgs()[0];
//获取方法
// String classType = joinPoint.getTarget().getClass().getName();
// Class<?> clazz = Class.forName(classType);
// String clazzName = clazz.getName();
// String methodName = joinPoint.getSignature().getName(); //获取方法名称
// Object[] args = joinPoint.getArgs();//参数
// Map<String, Object> nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName, args);//获取被切参数名称及参数值
// System.out.println(nameAndArgs.toString());
LogEntity logEntity = new LogEntity();
//
//
//
//发送消息到rabbitMq
// System.out.println(hislogEntity.toString());
try {
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setExchange(env.getProperty("log.user.exchange.name"));
rabbitTemplate.setRoutingKey(env.getProperty("log.user.routing.key.name"));
Message message = MessageBuilder.withBody(objectMapper.writeValueAsBytes(logEntity)).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, MessageProperties.CONTENT_TYPE_JSON);
rabbitTemplate.convertAndSend(message);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
MQ配置
@Configuration
public class RabbitmqConfig {
private static final Logger log= LoggerFactory.getLogger(RabbitmqConfig.class);
@Autowired
private Environment env;
@Autowired
private CachingConnectionFactory connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate() {
connectionFactory.setPublisherConfirms(true);
connectionFactory.setPublisherReturns(true);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);
}
});
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message);
}
});
return rabbitTemplate;
}
@Bean
public Queue logUserQueue(){
return new Queue(env.getProperty("log.user.queue.name"),true);
}
@Bean
public DirectExchange logUserExchange(){
return new DirectExchange(env.getProperty("log.user.exchange.name"),true,false);
}
@Bean
public Binding logUserBinding(){
return BindingBuilder.bind(logUserQueue()).to(logUserExchange()).with(env.getProperty("log.user.routing.key.name"));
}
}
业务service层
@Service
public class LogTsetService {
@SystemLog
public Result findTestdata(Map map){
Result result = new Result();
result.setContent("helooworld");
result.setStatus("ok");
return result;
}
}