【RabbitMQ】10-抽取MQ工具

1. Nacos共享配置

shared-mq.yaml

java 复制代码
spring:
  rabbitmq:
    host: ${hm.mq.host:*.*.*.*} # 你的虚拟机IP
    port: ${hm.mq.port:5672} # 端口
    virtual-host: ${hm.mq.vhost:/hmall} # 虚拟主机
    username: ${hm.mq.un:hmall} # 用户名
    password: ${hm.mq.pw:***} # 密码

2. Common包下引入依赖

java 复制代码
<!--AMQP依赖-->
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-amqp</artifactId>
    <scope>provided</scope>
</dependency>
<!--Spring整合Rabbit依赖-->
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <scope>provided</scope>
</dependency>
<!--json处理-->
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <scope>provided</scope>
</dependency>

3. RabbitMqHelper工具类

java 复制代码
@Slf4j
@AllArgsConstructor
public class RabbitMqHelper {

    private final RabbitTemplate rabbitTemplate;

    public void sendMessage(String exchange, String routingKey, Object msg) {
        log.debug("即将发送消息:exchange:{}, routingKey:{}, msg{}", exchange, routingKey, msg);
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
        log.info("发送消息成功");
    }

    public void sendDelayMessage(String exchange, String routingKey, Object msg, int delay) {
        log.debug("准备发送延迟消息:exchange:{}, routingKey:{}, msg:{}, delay:{}", exchange, routingKey, msg, delay);
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setDelay(delay);
            return message;
        });
        log.info("发送延迟消息成功");
    }

    public void sendMessageWithConfirm(String exchange, String routingKey, Object msg, int maxRetries) {
        log.debug("准备发送消息:exchange:{}, routingKey:{}, msg:{}, maxRetries:{}", exchange, routingKey, msg, maxRetries);
        CorrelationData cd = new CorrelationData(UUID.randomUUID().toString());
        cd.getFuture().addCallback(new ListenableFutureCallback<CorrelationData.Confirm>() {
            int retryCount;
            @Override
            public void onFailure(Throwable ex) {
                log.error("处理ack回收失败", ex);
            }

            @Override
            public void onSuccess(CorrelationData.Confirm result) {
                if (result != null && !result.isAck()) {
                    log.debug("消息发送失败,收到nack,已重试:{}", retryCount);
                    if (retryCount >= maxRetries) {
                        log.error("消息发送重试次数耗尽,发送失败");
                        return;
                    }
                }
            }
        });
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, cd);
        log.info("发送确认消息成功");
    }
}

3. 自动装配

java 复制代码
@Configuration
@ConditionalOnClass(value = {MessageConverter.class, RabbitTemplate.class})
public class MqConfig {
    @Bean
    @ConditionalOnBean(ObjectMapper.class)
    public MessageConverter messageConverter(ObjectMapper mapper) {
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(mapper);
        jackson2JsonMessageConverter.setCreateMessageIds(true);
        return jackson2JsonMessageConverter;
    }

    @Bean
    public RabbitMqHelper rabbitMqHelper(RabbitTemplate rabbitTemplate) {
        return new RabbitMqHelper(rabbitTemplate);
    }
}

4. 配置扫描

在spring.factories设置

java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hmall.common.config.MyBatisConfig,\
  com.hmall.common.config.MvcConfig,\
  com.hmall.common.config.MqConfig,\
  com.hmall.common.config.JsonConfig
相关推荐
时序数据说3 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.7 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)7 小时前
KINGBASE集群日常维护管理命令总结
数据库·database
妖灵翎幺7 小时前
Java应届生求职八股(2)---Mysql篇
数据库·mysql
HMBBLOVEPDX7 小时前
MySQL的事务日志:
数据库·mysql
DjangoJason8 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
weixin_419658319 小时前
MySQL数据库备份与恢复
数据库·mysql
专注API从业者11 小时前
基于 Flink 的淘宝实时数据管道设计:商品详情流式处理与异构存储
大数据·前端·数据库·数据挖掘·flink
小猿姐11 小时前
KubeBlocks for Milvus 揭秘
数据库·云原生
AI 嗯啦12 小时前
SQL详细语法教程(四)约束和多表查询
数据库·人工智能·sql