北京住房投资建设中心网站首,企业网站可信度建设,产品规划,dedecms 获取网站地址文章目录 RabbitMQ项目实战选择客户端基础实战 前情提要#xff1a;我们了解了消息队列#xff0c;RabbitMQ的入门#xff0c;交换机#xff0c;以及核心特性等知识#xff0c;现在终于来到了激动人心的项目实战环节#xff01;本小节主要介绍通过Spring Boot RabbitMQ S… 文章目录 RabbitMQ项目实战选择客户端基础实战 前情提要我们了解了消息队列RabbitMQ的入门交换机以及核心特性等知识现在终于来到了激动人心的项目实战环节本小节主要介绍通过Spring Boot RabbitMQ Starter 在SpringBoot项目中跑通测试RabbitMQ话不多说我们马上开始 RabbitMQ项目实战
选择客户端
怎么在项目中使用RabbitMQ?
使用官方的客户端类比jdbc
优点兼容性好换语言成本低比较灵活 缺点太灵活要自己去处理一些事情比如要自己维护管理链接很麻烦
使用封装好的客户端比如Spring Boot RabbitMQ Starter类比mybatis
优点简单易用直接配置直接用更方便地去管理链接 缺点不够灵活被框架限制
基础实战
我们使用Spring Boot RabbitMQ Starter https://spring.io/guides/gs/messaging-rabbitmq/
引入依赖 注意使用的版本一定要和你的springboot版本一致去maven中心仓库中找版本一致的
!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactIdversion2.7.2/version
/dependency
2 在yml中引入配置
spring:rabbitmq:host: localhostport: 5672password: guestusername: guest3创建交换机和队列一般在项目启动之前执行创建一次即可
package com.yupi.springbootinit.bimq;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;/*** 用于创建测试程序用到的交换机和队列只用在程序启动前执行一次*/
public class MqInitMain {public static void main(String[] args) {try{ConnectionFactory factory new ConnectionFactory();factory.setHost(localhost);Connection connection factory.newConnection();Channel channel connection.createChannel();String EXCHANGE_NAME code_exchange;channel.exchangeDeclare(EXCHANGE_NAME,direct);String queueName code_queue;channel.queueDeclare(queueName,true,false,false,null);channel.queueBind(queueName,EXCHANGE_NAME,my_routingKey);}catch (Exception e){}}
}
4生产者代码
package com.yupi.springbootinit.bimq;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;Component
public class MyMessageProducer {Resourceprivate RabbitTemplate rabbitTemplate;public void sendMessage(String exchange,String routingKey,String message){rabbitTemplate.convertAndSend(exchange,routingKey,message);}
}
5消费者代码
package com.yupi.springbootinit.bimq;import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;Component
Slf4j
public class MyMessageConsumer {//使用SneakyThrows注解简化异常处理//使得你可以在不声明抛出异常的方法中抛出受检异常而无需捕获它们。这在一些特定情况下可能会很有用但通常不建议频繁使用因为它可能会破坏代码的可读性和健壮性。SneakyThrows//使用RabbitListener注解指定要监听的队列名称为code_queue并设置消息的确认机制为手动确认RabbitListener(queues {code_queue},ackMode MUNAL)// // 在RabbitMQ中,每条消息都会被分配一个唯一的投递标签用于标识该消息在通道中的投递状态和顺序。通过使用Header(AmqpHeaders.DELIVERY_TAG)注解,可以从消息头中提取出该投递标签,并将其赋值给long deliveryTag参数。public void reciveMessage(String message, Channel channel, Header(AmqpHeaders.DELIVERY_TAG) long deliverttag){log.info(receiveMessage message {}, message);//手动确认消息的接收channel.basicAck(deliverttag,false);}
}
测试类测试
package com.yupi.springbootinit.bimq;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;import static org.junit.jupiter.api.Assertions.*;
SpringBootTest
class MyMessageProducerTest {Resourceprivate MyMessageProducer myMessageProducer;Testvoid sendMessage() {myMessageProducer.sendMessage(code_exchange, my_routingKey, 你好呀);}
}打印出了日志说明消费者收到了消息测试通过 既然测试已经通过接下来那就把它运用到项目中去吧欲知后事如何且听下回分解~