1.安装:
win10安装RabbitMQ
2.使用
2.0 pom
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.4.3</version>
</dependency>
2.1简单模式
一个生产者 一个消费者
生产者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel=connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue",false,false,false,null);
//发送消息
// 消息内容
String mes="hello word";
channel.basicPublish("","basic_queue",null,mes.getBytes());
System.out.println("生产者发送"+mes);
Thread.sleep(10);
channel.close();
connection.close();
}
消费者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue", false, false, false, null);
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue", true, defaultConsumer);
}
2.2 work模式
一个生产者 多个消费者
生产者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel=connection.createChannel();
channel.queueDeclare("basic_queue",false,false,false,null);
//发送消息
// 循环发布任务
for (int i = 1; i <= 10; i++) {
// 消息内容
String mes="hello word"+i;
channel.basicPublish("","basic_queue",null,mes.getBytes());
System.out.println("生产者发送"+mes);
Thread.sleep(10);
}
channel.close();
connection.close();
}
消费者1
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue", false, false, false, null);
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue", true, defaultConsumer);
}
消费者2
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue", false, false, false, null);
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者2接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue", true, defaultConsumer);
}
2.3 订阅模式
一个生产者发送的消息会被多个消费者获取。
生产者:可以将消息发送到队列或者是交换机。
消费者:只能从队列中获取消息。
如果消息发送到没有队列绑定的交换机上,那么消息将丢失。
每个发到 fanout 类型交换器的消息都会分到所有绑定的队列上去。 fanout 交换器不处理路由键,只是简单的将队列绑定到交换器上,每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上。很像子网广播,每台子网内的主机都获得了一份复制的消息。 fanout 类型转发消息是最快的 。
生产者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel=connection.createChannel();
//声明exchange(广播模式)
channel.exchangeDeclare("fanout_exchange", BuiltinExchangeType.FANOUT);
/*//声明队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);*/
//发送消息
// 消息内容
String mes="hello word";
channel.basicPublish("fanout_exchange","",null,mes.getBytes());
System.out.println("生产者发送"+mes);
Thread.sleep(10);
channel.close();
connection.close();
}
消费者1
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("bas_queue1", false, false, false, null);
//声明交换机
channel.exchangeDeclare("fanout_exchange",BuiltinExchangeType.FANOUT);
//队列与交换机绑定
channel.queueBind("bas_queue1","fanout_exchange","");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("bas_queue1", true, defaultConsumer);
}
消费者2
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("bas_queue2", false, false, false, null);
//声明交换机
channel.exchangeDeclare("fanout_exchange",BuiltinExchangeType.FANOUT);
//队列与交换机绑定
channel.queueBind("bas_queue2","fanout_exchange","");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("bas_queue2", true, defaultConsumer);
}
2.4 路由模式
1、 发送消息到交换机并且要指定路由key
2、 消费者将队列绑定到交换机时需要指定路由key
是一种完全匹配,只有匹配到的消费者才能消费消息
消息中的路由键( routing key)如果和 Binding 中的 binding key 一致, 交换器就将消息发到对应的队列中。路由键与队列名完全匹配,如果一个队列绑定到交换机要求路由键为“ zc”,则只转发 routing key 标记为“ zc”的消息,它是完全匹配、单播的模式。
生产者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel=connection.createChannel();
//声明交换机direct(路由模式)
channel.exchangeDeclare("direct_exchange",BuiltinExchangeType.DIRECT);
//发送消息
// 消息内容
String mes="hello word";
channel.basicPublish("direct_exchange","zc",null,mes.getBytes());
System.out.println("生产者发送"+mes);
Thread.sleep(10);
channel.close();
connection.close();
}
消费者1
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue1", false, false, false, null);
//绑定对列到交换机上,并制定路由件未zc
channel.queueBind("basic_queue1","direct_exchange","zc");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue1", true, defaultConsumer);
}
消费者2
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue2", false, false, false, null);
//绑定对列到交换机上,并制定路由件未sb
channel.queueBind(QUEUE_NAME,"direct_exchange","sb");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者2接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue2", true, defaultConsumer);
}
2.5 通配符模式
发送给主题转发器的消息不能是任意设置的选择键,必须是用小数点隔开的一系列的标识符。
这些标识符可以是随意,但是通常跟消息的某些特性相关联。
主题转发器的逻辑类似于direct类型的转发器。消息通过一个特定的路由键发送到所有与绑定键匹配的队列中,关于绑定键有两种特殊的情况:
1.* 号只能匹配一个字符
2.#能匹配一个或多个字符
生产者
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel=connection.createChannel();
//声明交换机topic(通配符模式)
channel.exchangeDeclare("topic_exchange",BuiltinExchangeType.TOPIC);
//发送消息
// 消息内容
String mes="hello word";
channel.basicPublish("topic_exchange","zc.nb.ss.kkkk",null,mes.getBytes());
System.out.println("生产者发送"+mes);
Thread.sleep(10);
channel.close();
connection.close();
}
消费者1
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue1", false, false, false, null);
//绑定对列到交换机上
channel.queueBind("basic_queue1","topic_exchange","zc.#");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者1接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue1", true, defaultConsumer);
}
消费者2
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue2", false, false, false, null);
//绑定对列到交换机上
channel.queueBind("basic_queue2","topic_exchange","*.nb");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者2接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue2", true, defaultConsumer);
}
消费者3
public static void main(String[] args)throws Exception {
//与mq建立连接
//定义连接工厂
ConnectionFactory connectionFactory=new ConnectionFactory();
//设置服务地址
connectionFactory.setHost("127.0.0.1");
//端口
connectionFactory.setPort(5672);
//账号密码
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 通过工程获取连接
Connection connection = connectionFactory.newConnection();
//建立通道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("basic_queue3", false, false, false, null);
//绑定对列到交换机上
channel.queueBind("basic_queue3","topic_exchange","#.kkkk");
DefaultConsumer defaultConsumer=new DefaultConsumer(channel){
//获取消息,并且处理
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
// body 即消息体
String msg = new String(body);
System.out.println("消费者3接收到消息:" + msg);
try {
Thread.sleep(50);//模拟消费耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//消费消息
channel.basicConsume("basic_queue3", true, defaultConsumer);
}
消费者1和消费者3能接收到消息,消费者2不能接收到消息。