package com.example.demo.RabbitMQ; import com.example.demo.config.RabbitMQConfig; import com.example.demo.domain.DTO.OperationLogDTO; import com.example.demo.domain.entity.OperationLog; import com.example.demo.mapper.coin.OperationLogMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; // com.example.demo.consumer.LogConsumer.java @Component @Slf4j public class LogConsumer { @Autowired private OperationLogMapper operationLogMapper; @RabbitListener(queues = RabbitMQConfig.LOG_QUEUE) public void consumeLog(OperationLogDTO logDTO) { try { OperationLog log = new OperationLog(); log.setUserId(logDTO.getUserId()); log.setUsername(logDTO.getUsername()); log.setAction(logDTO.getAction()); log.setIp(logDTO.getIp()); log.setMethod(logDTO.getMethod()); log.setArgs(logDTO.getArgs()); log.setCreateTime(logDTO.getCreateTime()); System.out.println("consumer" + log); operationLogMapper.insertLog(log); } catch (Exception e) { log.error("持久化日志失败", e); // 可以重试或记录到文件 } } }