You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

  1. package com.example.demo.RabbitMQ;
  2. import com.example.demo.config.RabbitMQConfig;
  3. import com.example.demo.domain.DTO.OperationLogDTO;
  4. import com.example.demo.domain.entity.OperationLog;
  5. import com.example.demo.mapper.coin.OperationLogMapper;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. // com.example.demo.consumer.LogConsumer.java
  11. @Component
  12. @Slf4j
  13. public class LogConsumer {
  14. @Autowired
  15. private OperationLogMapper operationLogMapper;
  16. @RabbitListener(queues = RabbitMQConfig.LOG_QUEUE)
  17. public void consumeLog(OperationLogDTO logDTO) {
  18. try {
  19. OperationLog log = new OperationLog();
  20. log.setUserId(logDTO.getUserId());
  21. log.setUsername(logDTO.getUsername());
  22. log.setAction(logDTO.getAction());
  23. log.setIp(logDTO.getIp());
  24. log.setMethod(logDTO.getMethod());
  25. log.setArgs(logDTO.getArgs());
  26. log.setCreateTime(logDTO.getCreateTime());
  27. System.out.println("consumer" + log);
  28. operationLogMapper.insertLog(log);
  29. } catch (Exception e) {
  30. log.error("持久化日志失败", e);
  31. // 可以重试或记录到文件
  32. }
  33. }
  34. }