rabbitMQ拥有多种模式,以应对不同的需求,模式主要分为五种:
- 简单模式
- 工作者模式
- 发布订阅模式
- 路由模式
- topic主体模式
1.简单模式
该模式仅有一个生产者,一个队列,一个消费者
我们可以用代码来简单实现这种模式,
生产者代码:
package com.wyj.hello;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/19 20:05
*/
public class Product {
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象
Connection connection = factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列名
* boolean durable,是否持久化
* boolean exclusive,是否独占
* boolean autoDelete,是否自动删除
*Map<String, Object> arguments 额外参数
*/
channel.queueDeclare("qy129",true,false,false,null);
String msg="你你真帅";
channel.basicPublish("","qy129",null,msg.getBytes());
}
}
消费者代码:
package com.wyj.hello;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 15:31
*/
public class Consumer {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("消息的内容:"+new String(body));
}
};
channel.basicConsume("qy129",true,callback);
}
}
2.工作模式
即一个生产者,一个队列,多个消费者
该模式下,两个消费者交替消费队列中的消息
生产者代码如下:
package com.wyj.work;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 15:32
*/
public class Product {
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象
Connection connection = factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列名
* boolean durable,是否持久化
* boolean exclusive,是否独占
* boolean autoDelete,是否自动删除
*Map<String, Object> arguments 额外参数
*/
channel.queueDeclare("qy129_work",true,false,false,null);
for (int i = 0; i < 10; i++) {
String msg="你你真帅"+i;
channel.basicPublish("","qy129_work",null,msg.getBytes());
}
}
}
生产者1:
package com.wyj.Work;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 15:33
*/
public class Comsumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("工作模式01:"+new String(body));
}
};
channel.basicConsume("qy129_work",true,callback);
}
}
生产者2:
package com.wyj.Work;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 15:33
*/
public class Comsumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("工作模式02:"+new String(body));
}
};
channel.basicConsume("qy129_work",true,callback);
}
}
3.发布订阅模式
一个生产者P,一个交换机X,两个队列,两个消费者C1,C2
该模式下,两个消费者可以接收到相同的消息,因为队列中的消息都是由交换机发送的
生产者代码如下:
package com.wyj.fanout;
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;
/**
* @author 王逸君
* @version 1.0
*
* @date 2021/4/20 15:36
*/
public class Product {
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象
Connection connection = factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列名
* boolean durable,是否持久化
* boolean exclusive,是否独占
* boolean autoDelete,是否自动删除
*Map<String, Object> arguments 额外参数
*/
channel.queueDeclare("qy129_exchange01",true,false,false,null);
channel.queueDeclare("qy129_exchange02",true,false,false,null);
//创建交换机
/**
* String exchange 交换机名
* BuiltinExchangeType type
* boolean durable 持久化
*/
channel.exchangeDeclare("qy129_exchange_fanout", BuiltinExchangeType.FANOUT,true);
//绑定交换机跟队列
/**
* String queue, 队列名
* String exchange, 交换机名
* String routingKey 路由key
*/
channel.queueBind("qy129_exchange01","qy129_exchange_fanout","");
channel.queueBind("qy129_exchange02","qy129_exchange_fanout","");
for (int i = 0; i < 10; i++) {
String msg="你你真帅"+i;
channel.basicPublish("qy129_exchange_fanout","",null,msg.getBytes());
}
}
}
生产者C1:
package com.wyj.fanout;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:17
*/
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("订阅者模式01:"+new String(body));
}
};
channel.basicConsume("qy129_exchange01",true,callback);
}
}
生产者C2:
package com.wyj.fanout;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:17
*/
public class Consumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("订阅者模式02:"+new String(body));
}
};
channel.basicConsume("qy129_exchange02",true,callback);
}
}
4.路由模式
该模式有一个生产者,一个交换机,多个队列,多个消费者
该模式下交换机可以发送指定类型的消息给指定的队列,两个消费者消费的消息可以不同
生产者代码如下:
package com.wyj.direct;
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;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:23
*/
public class Product {
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象
Connection connection = factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列名
* boolean durable,是否持久化
* boolean exclusive,是否独占
* boolean autoDelete,是否自动删除
*Map<String, Object> arguments 额外参数
*/
channel.queueDeclare("qy129_direct01",true,false,false,null);
channel.queueDeclare("qy129_direct02",true,false,false,null);
//创建交换机
/**
* String exchange 交换机名
* BuiltinExchangeType type
* boolean durable 持久化
*/
channel.exchangeDeclare("qy129_exchange_direct", BuiltinExchangeType.DIRECT,true);
//绑定交换机跟队列
/**
* String queue, 队列名
`在这里插入代码片` * String exchange, 交换机名
* String routingKey 路由key
*/
channel.queueBind("qy129_direct01","qy129_exchange_direct","error");
channel.queueBind("qy129_direct02","qy129_exchange_direct","info");
channel.queueBind("qy129_direct02","qy129_exchange_direct","error");
channel.queueBind("qy129_direct02","qy129_exchange_direct","warning");
for (int i = 0; i < 10; i++) {
String msg="你你真帅"+i;
String msg1="我我我真帅"+i;
channel.basicPublish("qy129_exchange_direct","info",null,msg1.getBytes());
}
}
}
消费者C1:
package com.wyj.direct;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:25
*/
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("路由模式02:"+new String(body));
}
};
channel.basicConsume("qy129_direct01",true,callback);
}
}
消费者C2:
package com.wyj.direct;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:25
*/
public class Consumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("路由模式02:"+new String(body));
}
};
channel.basicConsume("qy129_direct02",true,callback);
}
}
5.主题模式
该模式与路由模式大致相似,只不过这里的交换机使用的是topic类型,topic类型的交换机和direct的不
同就在于topic可以匹配通配符。*代表匹配一个元素,#代表匹配一个或多个元素
生产者代码如下:
package com.wyj.topic;
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;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:35
*/
public class Product {
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象
Connection connection = factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列名
* boolean durable,是否持久化
* boolean exclusive,是否独占
* boolean autoDelete,是否自动删除
*Map<String, Object> arguments 额外参数
*/
channel.queueDeclare("qy129_topic01",true,false,false,null);
channel.queueDeclare("qy129_topic02",true,false,false,null);
//创建交换机
/**
* String exchange 交换机名
* BuiltinExchangeType type
* boolean durable 持久化
*/
channel.exchangeDeclare("qy129_exchange_topic", BuiltinExchangeType.TOPIC,true);
//绑定交换机跟队列
/**
* String queue, 队列名
* String exchange, 交换机名
* String routingKey 路由key
*/
channel.queueBind("qy129_topic01","qy129_exchange_topic","*.orange.*");
channel.queueBind("qy129_topic02","qy129_exchange_topic","*.*.rabbit");
channel.queueBind("qy129_topic02","qy129_exchange_topic","lazy.#");
for (int i = 0; i < 10; i++) {
String msg="你你真帅"+i;
channel.basicPublish("qy129_exchange_topic","a.orange.rabbit",null,msg.getBytes());
}
}
}
消费者C1:
package com.wyj.topic;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:37
*/
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("主题模式01:"+new String(body));
}
};
channel.basicConsume("qy129_topic01",true,callback);
}
}
消费者C2:
package com.wyj.topic;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 王逸君
* @version 1.0
* @date 2021/4/20 16:37
*/
public class Consumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.192.129");
//创建连接对象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 接受的信息
System.out.println("主题模式02:"+new String(body));
}
};
channel.basicConsume("qy129_topic02",true,callback);
}
}