目录
消息队列 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 死信队列与延迟队列实现思路
Topics工作模式架构图:
如图所示我们需要一个类型为topic的交换机以及两个队列,生产者发布消息到交换机,交换机根据对应的routingkey发布到指定的与它绑定的队列,与Routing工作模式不同的是Topics模式的routingkey引入了通配符,使routingkey更加灵活,比如routingkey为abc.orange.bcd时,消息会被存储到Q1,routingkey为lazy.xxx.bbb时,会被存储到Q2。
#:匹配一个或多个词 * :只能匹配一个单词
一、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_Topics {
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 = "topic_exchange";
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, 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 = "topic_queue1";
String queue2Name = "topic_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
* */
channel.queueBind(queue1Name, exchangeName, "#.error");
channel.queueBind(queue2Name, exchangeName, "#.info");
channel.queueBind(queue2Name, exchangeName, "#.warning");
channel.queueBind(queue2Name, exchangeName, "*.error");
String body = "hello rabbitmq";
channel.basicPublish(exchangeName, "log.error", null, body.getBytes());
channel.basicPublish(exchangeName, "log.info", null, body.getBytes());
channel.basicPublish(exchangeName, "log.warning", null, body.getBytes());
channel.close();
connection.close();
}
}
routingkey为log.error的消息两个队列都会存储,其他两个只有topic_queue2会存储
4. 消费者代码:
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer_Topics {
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 = "topic_queue1";
String queue2Name = "topic_queue2";
Consumer consumer = new Consumer() {
public void handleConsumeOk(String s) {
}
public void handleCancelOk(String s) {
}
public void handleCancel(String s) throws IOException {
}
public void handleShutdownSignal(String s, ShutdownSignalException e) {
}
public void handleRecoverOk(String s) {
}
/*
* handleDelivery(java.lang.String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes)
* s: 标识
* envelope: 获取一些信息,交换机路由key等
* basicProperties: 配置信息
* bytes: 接收到的数据
* */
public void handleDelivery(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); // 消费队列topic_queue1
channel.basicConsume(queue2Name, true, consumer); // 消费队列topic_queue2
}
}
二、SpringBoot操作RabbitMQ
1. 引入依赖:
<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 = "topic_exchange";
public static final String QUEUE1_NAME = "topic_queue1";
public static final String QUEUE2_NAME = "topic_queue2";
// 1. 创建Exchange交换机
@Bean("topic_exchange")
public Exchange createExchange(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
// 2. 创建Queue队列
@Bean("topic_queue1")
public Queue createQueue1(){
return QueueBuilder.durable(QUEUE1_NAME).build();
}
@Bean("topic_queue2")
public Queue createQueue2(){
return QueueBuilder.durable(QUEUE2_NAME).build();
}
// 3. 创建绑定交换机与队列
@Bean
public Binding createBinding1Error(@Qualifier("topic_queue1") Queue queue, @Qualifier("topic_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("#.error").noargs();
}
@Bean
public Binding createBinding2Error(@Qualifier("topic_queue2") Queue queue, @Qualifier("topic_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("#.error").noargs();
}
@Bean
public Binding createBinding2Warn(@Qualifier("topic_queue2") Queue queue, @Qualifier("topic_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("#.warning").noargs();
}
@Bean
public Binding createBinding2Info(@Qualifier("topic_queue2") Queue queue, @Qualifier("topic_exchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("*.info").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 testTopicsSend(){
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "log.error", "hello rabbitmq");
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "log.info", "hello rabbitmq");
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "log.warning", "hello rabbitmq");
}
}
routingkey为log.error的消息两个队列都会存储,另外两个消息只有topic_queue2会存储