【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
相关推荐
roman_日积跬步-终至千里24 分钟前
【分布式理论13】分布式存储:数据存储难题与解决之道
分布式
焱焱枫25 分钟前
自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
数据库·sql·oracle
2301_7930698228 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
hhw19911237 分钟前
spring boot知识点5
java·数据库·spring boot
ITPUB-微风1 小时前
功能开关聚合对象实践:提升金融领域的高可用性
网络·数据库·金融
去看日出1 小时前
Linux(centos)系统安装部署MySQL8.0数据库(GLIBC版本)
linux·数据库·centos
(; ̄ェ ̄)。1 小时前
在Nodejs中使用kafka(三)offset偏移量控制策略,数据保存策略
分布式·后端·kafka·node.js
Hanyaoo2 小时前
为什么mvcc中?m_ids 列表并不等同于 min_trx_id 和 max_trx_id 之间的所有事务 ID
数据库
偏右右2 小时前
PL/SQL 异常处理
数据库·sql·oracle
binbinxyz2 小时前
【Kafka系列】Kafka 消息传递保障机制
分布式·kafka