1. 工作者模式:
特点:
1. 一个生产者
2. 由多个消费。
3. 统一个队列。
4. 这些消费者之间存在竞争关系。
用处:
比如批量处理上. rabbitMQ里面积压了大量的消息。
生产者
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_work",true,false,false,null);
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "我只是一条消息!"+i;
channel.basicPublish("", "ban129_queue_work", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
消费者01:
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_work",true,callback);
}
}
消费者02:
public class Consumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_work",true,callback);
}
}
2. 发布订阅模式
- 特点:
1.一个生产者
2.多个消费者
3.多个队列。
4.交换机 转发消息。
生产者:
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_fanout01",true,false,false,null);
channel.queueDeclare("ban129_queue_fanout02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange", BuiltinExchangeType.FANOUT,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_fanout01","ban129_exchange","");
channel.queueBind("ban129_queue_fanout02","ban129_exchange","");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "我只是一条消息!"+i;
channel.basicPublish("ban129_exchange", "", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
消费者:
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_fanout01",true,callback);
}
}
3.路由模式
特点:
1.一个生产者
2.多个消费者
3.多个队列。
4.交换机 转发消息。
5.routekey:路由key 只要routekey匹配的消息可以到达对应队列。
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_direct01",true,false,false,null);
channel.queueDeclare("ban129_queue_direct02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange_direct", BuiltinExchangeType.DIRECT,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_direct01","ban129_exchange_direct","error");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","info");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","error");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","warning");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "我只是一条消息!"+i;
channel.basicPublish("ban129_exchange_direct", "error", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
4. topic主体模式
- 绑定按照通配符的模式。
*: 统配一个单词。
#: 统配n个单词
hello.orange.rabbit
lazy.orange
生产者
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_topic01",true,false,false,null);
channel.queueDeclare("ban129_queue_topic02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange_topic", BuiltinExchangeType.TOPIC,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_topic01","ban129_exchange_topic","*.orange.*");
channel.queueBind("ban129_queue_topic02","ban129_exchange_topic","*.*.rabbit");
channel.queueBind("ban129_queue_topic02","ban129_exchange_topic","lazy.#");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "我只是一条消息!"+i;
channel.basicPublish("ban129_exchange_topic", "lazy.orange.rabbit", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
5. rabbitMQ整合springboot
springboot引入了相关的依赖后,提供一个工具类RabbitTemplate.使用这个工具类可以发送消息。
(1)父工程引入相关的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>product</module>
<module>consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ykq</groupId>
<artifactId>springboot-rabbit-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-rabbit-parent</name>
<description>springboot整合rabbitMQ</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--rabbitMQ的依赖: 启动类加载。读取配置文件:
springboot自动装配原理: 引用starter启动依赖时,把对应的自动装配类加载进去,该自动装配类可以读取application配置文件中
内容。 DispatherServlet
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)相应的生产者和消费的配置
server:
port: 9999
#rabbit的配置
spring:
rabbitmq:
host: 192.168.213.188
(3)生产者
public class HelloController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("hello")
public String hello(){ //业务层
System.out.println("下单成功");
//String exchange, String routingKey, Object message
Map<String,Object> map=new HashMap<>();
map.put("productId",1);
map.put("num",10);
map.put("price",12);
rabbitTemplate.convertAndSend("ban129_exchange","", JSON.toJSONString(map)); //序列化过程
return "下单成功";
}
}
消费者监听消息
public class MyRabbitListener {
//队列中存在消息则立即回调该方法
@RabbitListener(queues = {"ban129_queue_fanout01"})
public void listener(String msg){
Map map = JSON.parseObject(msg, Map.class);
System.out.println(map);
}
}