RabbitMQ基本操作: https://blog.csdn.net/Michael_lcf/article/details/124677268
RabbitMQ的5大核心概念: https://blog.csdn.net/Michael_lcf/article/details/126435452
RabbitMQ的队列模式:https://blog.csdn.net/Michael_lcf/article/details/139362674
RabbitMQ实现分布式WebSocket通信: https://blog.csdn.net/Michael_lcf/article/details/126403772
RabbitMQ的队列模式
1、简单模式(Simple Model)
1.1、模式介绍
简单模式(Simple Model、Hello World)这是最基本的消息模型,生产者将消息发送到队列,消费者从队列中获取消息。RabbitMQ相当于一个消息代理,负责将A的消息转发给B。
1.2、应用场景
将发送的电子邮件放到消息队列,然后邮件服务在队列中获取邮件并发送给收件人。
1.3、撸代码
生产者
package com.michael.demo1;
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 Sender {
private final static String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明队列
// queue:队列名
// durable:是否持久化
// exclusive:是否排外 即只允许该channel访问该队列 一般等于true的话用于一个队列只能有一个消费者来消费的场景
// autoDelete:是否自动删除 消费完删除
// arguments:其他属性
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//消息内容
String message = "simple_queue test message O(∩_∩)O哈哈~";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("[x]Sent '" + message + "'");
//最后关闭通关和连接
channel.close();
connection.close();
}
}
消费者
package com.michael.demo1;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Receiver {
private final static String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
// 获取连接
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
}
2、工作队列模式(Work Model)
2.1、模式介绍
在这种模式下,多个消费者可以竞争同一个队列中的消息,适合需要处理大量相同类型消息的场景。
在多个消费者之间分配任务(竞争的消费者模式),一个生产者对应多个消费者,一般适用于执行资源密集型任务,单个消费者处理不过来,需要多个消费者进行处理。
2.2、应用场景
一个订单的处理需要10s,有多个订单可以同时放到消息队列,然后让多个消费者同时处理,这样就是并行了,而不是单个消费者的串行情况。
2.3、撸代码
消费者1
package com.michael.demo2;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Receiver1 {
private final static String QUEUE_NAME = "queue_work";
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 同一时刻服务器只会发送一条消息给消费者
channel.basicQos(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
}
消费者2
package com.michael.demo2;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Receiver2 {
private final static String QUEUE_NAME = "queue_work";
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 同一时刻服务器只会发送一条消息给消费者
channel.basicQos(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
});
}
}
生产者
package com.michael.demo2;
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 Sender {
private final static String QUEUE_NAME = "queue_work";
public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 0; i < 100; i++) {
String message = " O(∩_∩)O哈哈~ Work mode message " + i;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("[x] Sent '" + message + "'");
Thread.sleep(i * 10);
}
channel.close();
connection.close();
}
}
3、发布订阅模式(Publish/Subscribe Model)
3.1、模式介绍
生产者发送的消息会被广播给所有订阅了该消息的消费者,适用于需要向多个消费者发送相同消息的场景。
一次向许多消费者发送消息,一个生产者发送的消息会被多个消费者获取,也就是将消息将广播到所有的消费者中。
3.2、应用场景
更新商品库存后需要通知多个缓存和多个数据库,这里的结构应该是:
一个fanout类型交换机扇出两个消息队列,分别为缓存消息队列、数据库消息队列。
一个缓存消息队列对应着多个缓存消费者。
一个数据库消息队列对应着多个数据库消费者。
3.3、撸代码
消费者1
package com.michael.demo3;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class Receiver1 {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
// 订阅消息的回调函数
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received1 '" + message + "'");
};
// 消费者,有消息时出发订阅回调函数
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
});
}
}
消费者2
package com.michael.demo3;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class Receiver2 {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
// 订阅消息的回调函数
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received2 '" + message + "'");
};
// 消费者,有消息时出发订阅回调函数
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
});
}
}
生产者
package com.michael.demo3;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Sender {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.168.100");
factory.setPort(5672);
factory.setUsername("admin123");
factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = "publish subscribe message O(∩_∩)O哈哈~";
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}
4、路由模式(Routing Model)
4.1、模式介绍
生产者将消息发送到交换机,交换机根据路由键将消息路由到特定的队列,消费者订阅这些队列以接收消息。这种模式允许对消息进行更精细的控制,可以实现一对一、多对一或多对多的消息传递。
有选择地(Routing key)接收消息,发送消息到交换机并且要指定路由key ,消费者将队列绑定到交换机时需要指定路由key,仅消费指定路由key的消息。
4.2、应用场景
如在商品库存中增加了1台iphone12,iphone12促销活动消费者指定routing key为iphone12,只有此促销活动会接收到消息,其它促销活动不关心也不会消费此routing key的消息。
4.3、撸代码
消费者1
消费者2
生产者
5、主题模式(Topic Model)
5.1、模式介绍
这是路由模式的一种扩展,使用通配符来匹配路由键,实现更灵活的消息路由。适用于需要基于主题或类别来分发消息的场景。
根据主题(Topics)来接收消息,将路由key和某模式进行匹配,此时队列需要绑定在一个模式上,#
匹配一个词或多个词,*
只匹配一个词。
5.2、应用场景
同上,iphone促销活动可以接收主题为iphone的消息,如iphone12、iphone13等。
5.3、撸代码
消费者1
消费者2
生产者
6、远程过程调用(RPC Model)
6.1、模式介绍
如果我们需要在远程计算机上运行功能并等待结果就可以使用RPC,具体流程可以看图。
6.2、应用场景
需要等待接口返回数据,如订单支付。
6.3、撸代码
7、发布者确认(Publisher Confirms Model)
7.1、模式介绍
与发布者进行可靠的发布确认,发布者确认是RabbitMQ扩展,可以实现可靠的发布。在通道上启用发布者确认后,RabbitMQ将异步确认发送者发布的消息,这意味着它们已在服务器端处理。
7.2、应用场景
对于消息可靠性要求较高,比如钱包扣款。
7.3、撸代码
https://www.rabbitmq.com/tutorials
https://github.com/rabbitmq/rabbitmq-tutorials