消息队列 RabbitMQ[四] RabbitMQ的Publish/Subscribe(发布/订阅)工作模式(SpringBoot方式与amqp-client方式)(转)
(转自)https://blog.csdn.net/qq_43623492/article/details/124362679
目录
消息队列 RabbitMQ[一] RabbitMQ的下载与安装
消息队列 RabbitMQ[二] RabbitMQ可视化管控台创建用户并为用户分配虚拟机
消息队列 RabbitMQ[三] RabbitMQ的HelloWorld工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[四] RabbitMQ的Publish/Subscribe(发布/订阅)工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[五] RabbitMQ的Routing工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[六] RabbitMQ的Topics工作模式(SpringBoot方式与amqp-client方式)
消息队列 RabbitMQ[七] RabbitMQ保证消息的可靠性传递(Confirm Return Ack)
消息队列 RabbitMQ[八] SpringBoot Consumer 限流机制
消息队列 RabbitMQ[九] SpringBoot 设置消息过期时间TTL
消息队列 RabbitMQ[十] SpringBoot 死信队列与延迟队列实现思路
Publish/Subscribe工作模式架构图:
如图所示我们需要一个交换机以及两个队列,生产者发布消息到交换机,交换机会将消息发送到这两个与它绑定的队列
一、amqp-client操作RabbitMQ
1. 引入依赖:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.8.0</version>
</dependency>
2. 通过可视化管理界面添加用户并分配虚拟机
消息队列 RabbitMQ[二] RabbitMQ可视化管控台创建用户并为用户分配虚拟机
3. 生产者代码:
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Producer_PubSub {
public static void main(String[] args) throws IOException, TimeoutException {
// 1. 创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 2. 设置参数
connectionFactory.setHost("127.0.0.1"); // IP地址 默认地址localhost
connectionFactory.setPort(5672); // 端口号 默认5672
connectionFactory.setVirtualHost("/demo_virtual"); // 虚拟机名称 默认/
connectionFactory.setUsername("zdy"); // 用户名 默认guest
connectionFactory.setPassword("zdy"); // 密码 默认guest
// 3. 创建连接 Connection
Connection connection = connectionFactory.newConnection();
// 4. 创建频道 Channel
Channel channel = connection.createChannel();
// 5. 创建交换机
/*
* exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments)
* exchange: 交换机名称
* type: 交换机类型
* DIRECT("direct"),定向
* FANOUT("fanout"),广播 发送到每一个与该交换机绑定的队列
* TOPIC("topic"),通配符方式
* durable: 是否持久化
* autoDelete: 是否自动删除
* internal: 内部使用 一般为false
* arguments: 参数
* */
String exchangeName = "fanout_exchange";
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT, true, false, false, null);
// 6. 创建队列
/*
* queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
* queue: 队列名称
* durable: 是否持久化(当MQ关闭再启动时数据还在)
* exclusive: 是否独占(只能有一个消费者监听该队列)
* autoDelete: 当没有消费者时,是否自动删除队列
* arguments: 参数
* */
String queue1Name = "fanout_queue1";
String queue2Name = "fanout_queue2";
channel.queueDeclare(queue1Name, true, false, false, null);
channel.queueDeclare(queue2Name, true, false, false, null);
// 7. 绑定交换机与队列
/*
* queueBind(String queue, String exchange, String routingKey)
* queue: 队列名称
* exchange: 交换机名称
* routingKey: 路由key 因为交换机类型选为FANOUT 所以不需要routingKey
* */
channel.queueBind(queue1Name, exchangeName, "");
channel.queueBind(queue2Name, exchangeName, "");
// 发送消息
String body = "hello rabbitmq";
channel.basicPublish(exchangeName, "", null, body.getBytes());
channel.close();
connection.close();
}
}
4. 消费者代码
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer_PubSub {
public static void main(String[] args) throws IOException, TimeoutException {
// 1. 创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
// 2. 设置参数
connectionFactory.setHost("127.0.0.1"); // IP地址 默认地址localhost
connectionFactory.setPort(5672); // 端口号 默认5672
connectionFactory.setVirtualHost("/demo_virtual"); // 虚拟机名称 默认/
connectionFactory.setUsername("zdy"); // 用户名 默认guest
connectionFactory.setPassword("zdy"); // 密码 默认guest
// 3. 创建连接 Connection
Connection connection = connectionFactory.newConnection();
// 4. 创建频道 Channel
Channel channel = connection.createChannel();
String queue1Name = "fanout_queue1";
String queue2Name = "fanout_queue2";
Consumer consumer = new Consumer() {
public void handleConsumeOk(java.lang.String s) {
}
public void handleCancelOk(java.lang.String s) {
}
public void handleCancel(java.lang.String s) throws IOException {
}
public void handleShutdownSignal(java.lang.String s, ShutdownSignalException e) {
}
public void handleRecoverOk(java.lang.String s) {
}
/*
* handleDelivery(java.lang.String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes)
* s: 标识
* envelope: 获取一些信息,交换机路由key等
* basicProperties: 配置信息
* bytes: 接收到的数据
* */
public void handleDelivery(java.lang.String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
System.out.println(new String(bytes));
}
};
// 5. 接收消息
/*
* basicConsume(String queue, boolean autoAck, Consumer callback)
* queue: 接收队列名称
* autoAck: 是否自动确认(后期消息可靠性文章中进行讲解)
* callback: 接收到消息执行的回调函数
* */
// channel.basicConsume(queue1Name, true, consumer); // 消费队列fanout_queue1
channel.basicConsume(queue2Name, true, consumer); // 消费队列fanout_queue2
}
}
fanout_queue1消费成功
fanout_queue2消费成功
二、SpringBoot操作RabbitMQ
- 引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
2. 编写生产者代码(创建新的SpringBoot项目)
2.1 编写配置文件application.yml
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: zdy
password: zdy
virtual-host: /demo_virtual
2.2. 编写RabbitMQ配置文件
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String EXCHANGE_NAME = "fanout_exchange";
public static final String QUEUE1_NAME = "fanout_queue1";
public static final String QUEUE2_NAME = "fanout_queue2";
// 1. 创建Exchange交换机
@Bean("fanout_exchange")
public Exchange createExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
// 2. 创建Queue队列
@Bean("fanout_queue1")
public Queue createQueue1(){
return QueueBuilder.durable(QUEUE1_NAME).build();
}
@Bean("fanout_queue2")
public Queue createQueue2(){
return QueueBuilder.durable(QUEUE2_NAME).build();
}
// 3. 创建绑定交换机与队列
@Bean
public Binding createBinding1(@Qualifier("fanout_queue1") Queue queue, @Qualifier("fanout_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("").noargs();
}
@Bean
public Binding createBinding2(@Qualifier("fanout_queue2") Queue queue, @Qualifier("fanout_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("").noargs();
}
}
2.3. 编写测试类
import com.zdy.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testPubSubSend(){
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "", "hello rabbitmq");
}
}
3. 编写消费者代码(创建新的SpringBoot项目)
3.1. 编写配置文件application.yml(与生产者的一样)
3.2. 编写消息监听类
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQListener {
@RabbitListener(queues = "fanout_queue1")
public void listenQueueFanoutQ1(Message message){
System.out.println(new String(message.getBody()));
}
@RabbitListener(queues = "fanout_queue2")
public void listenQueueFanoutQ2(Message message){
System.out.println(new String(message.getBody()));
}
}
三、代码结构: