一、RabbitMQ程序
1.1、AMQP协议的回顾
1.2、RabbitMQ支持的消息模型
1.3、引入依赖
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.30</version> </dependency>
1.4、第一种模型(直连)
在上图的模型中,有以下概念:
1)工具类
package utils; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class RabbitMQUtils { private static ConnectionFactory connectionFactory; static { //创建连接mq的连接工厂对象 connectionFactory = new ConnectionFactory(); //设置连接rabbitmq主机 connectionFactory.setHost("10.0.0.11"); //设置端口号 connectionFactory.setPort(5672); //设置连接那个虚拟主机 connectionFactory.setVirtualHost("/ems"); //设置访问虚拟主机的用户名和密码 connectionFactory.setUsername("ems"); connectionFactory.setPassword("123456"); } //获取连接 public static Connection getConnection() { try { //获取连接对象 Connection connection = connectionFactory.newConnection(); return connection; } catch (Exception e) { e.printStackTrace(); } return null; } //关闭channel和连接方法 public static void closeConnectionAndChannel(Channel channel,Connection connection){ try { if(channel != null){ channel.close(); } if(connection != null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } } }
2)生产者
package helloworld; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.MessageProperties; import utils.RabbitMQUtils; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Provider { public static void main(String[] args) throws IOException, TimeoutException { //通过工具类获取连接 Connection connection = RabbitMQUtils.getConnection(); //获取连接中通道 Channel channel = connection.createChannel(); //通道绑定对应消息队列 //参数1: 队列名称 如果队列不存在自动创建 //参数2: 用来定义队列特性是否要持久化 true 持久化队列 false 不持久化 //参数3: exclusive 是否独占队列 true 独占队列 false 不独占 //参数4: autoDelete: 是否在消费完成后自动删除队列 true 自动删除 false 不自动删除 //参数5: 额外附加参数 channel.queueDeclare("hello",true,false,false,null); //发布消息 //参数1: 交换机名称 参数2:队列名称 参数3:传递消息额外设置 参数4:消息的具体内容 channel.basicPublish("","hello", MessageProperties.PERSISTENT_TEXT_PLAIN,"hello rabbitmq".getBytes()); //通过工具类关闭资源 RabbitMQUtils.closeConnectionAndChannel(channel, connection); } }
3)消费者
package helloworld; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Consumer { public static void main(String[] args) throws IOException, TimeoutException { //通过工具类获取连接 Connection connection = RabbitMQUtils.getConnection(); //创建通道 Channel channel = connection.createChannel(); //通道绑定对象 channel.queueDeclare("hello",true,false,false,null); //消费消息 channel.basicConsume("hello",true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("================" + new String(body)); } }); } }
1.5、第二种模型(work quene)
Work queues
,也被称为(Task queues
),任务模型。当消息处理比较耗时的时候,可能生产消息的速度会远远大于消息的消费速度。长此以往,消息就会堆积越来越多,无法及时处理。此时就可以使用work 模型:让多个消费者绑定到一个队列,共同消费队列中的消息。队列中的消息一旦消费,就会消失,因此任务是不会被重复执行的。
角色:
1)生产者
package workqueue; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.RabbitMQUtils; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); //获取通道对象 Channel channel = connection.createChannel(); //通过通道声明队列 channel.queueDeclare("work", true, false, false, null); for (int i = 1; i <= 20; i++) { channel.basicPublish("", "work", null,(i+"hello work queue").getBytes()); } //关闭资源 RabbitMQUtils.closeConnectionAndChannel(channel, connection); } }
2)消费者1
package workqueue; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer1 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //channel.basicQos(1);//每一次只能消费一个消息 channel.queueDeclare("work",true,false,false,null); //参数1:队列名称 参数2:消息自动确认 true 消费者自动向rabbitmq确认消息消费 false 不会自动确认消息 channel.basicConsume("work",true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者-1: "+new String(body)); } }); } }
3)消费者2
package workqueue; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer2 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("work",true,false,false,null); channel.basicConsume("work",true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者-2: "+new String(body)); } }); } }
总结:默认情况下,RabbitMQ将按顺序将每个消息发送给下一个使用者。平均而言,每个消费者都会收到相同数量的消息。这种分发消息的方式称为循环。
消息的自动确认机制:
Doing a task can take a few seconds. You may wonder what happens if one of the consumers starts a long task and dies with it only partly done. With our current code, once RabbitMQ delivers a message to the consumer it immediately marks it for deletion. In this case, if you kill a worker we will lose the message it was just processing. We'll also lose all the messages that were dispatched to this particular worker but were not yet handled.But we don't want to lose any tasks. If a worker dies, we'd like the task to be delivered to another worker.
1)消费者1修改
package workqueue; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer1 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); channel.basicQos(1);//每一次只能消费一个消息 channel.queueDeclare("work",true,false,false,null); //参数1:队列名称 参数2:消息自动确认 true 消费者自动向rabbitmq确认消息消费 false 不会自动确认消息 channel.basicConsume("work",false,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try{ Thread.sleep(2000); }catch (Exception e){ e.printStackTrace(); } System.out.println("消费者-1: "+new String(body)); // 参数1:确认队列中那个具体消息 参数2:是否开启多个消息同时确实 channel.basicAck(envelope.getDeliveryTag(),false); } }); } }
2)消费者2修改
package workqueue; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer2 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); channel.basicQos(1); channel.queueDeclare("work",true,false,false,null); channel.basicConsume("work",false,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者-2: "+new String(body)); //手动确认 参数1:手动确认消息标识 参数2:false 每次确认一个 channel.basicAck(envelope.getDeliveryTag(), false); } }); } }
1.6、第三种模型(fanout)
在广播模式下,消息发送流程是这样的:
1)生产者
package fanout; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.RabbitMQUtils; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //将通道声明指定交换机 //参数1: 交换机名称 参数2: 交换机类型 fanout 广播类型 channel.exchangeDeclare("logs","fanout"); //发送消息 channel.basicPublish("logs","",null,"fanout type message".getBytes()); //释放资源 RabbitMQUtils.closeConnectionAndChannel(channel, connection); } }
2)消费者1
package fanout; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer1 { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //通道绑定交换机 channel.exchangeDeclare("logs","fanout"); //临时队列 String queueName = channel.queueDeclare().getQueue(); //绑定交换机和队列 channel.queueBind(queueName,"logs",""); //消费消息 channel.basicConsume(queueName,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者1: "+new String(body)); } }); } }
3)消费者2
package fanout; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer2 { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //通道绑定交换机 channel.exchangeDeclare("logs","fanout"); //临时队列 String queueName = channel.queueDeclare().getQueue(); //绑定交换机和队列 channel.queueBind(queueName,"logs",""); //消费消息 channel.basicConsume(queueName,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者2: "+new String(body)); } }); } }
4)消费者3
package fanout; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer3 { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //通道绑定交换机 channel.exchangeDeclare("logs","fanout"); //临时队列 String queueName = channel.queueDeclare().getQueue(); //绑定交换机和队列 channel.queueBind(queueName,"logs",""); //消费消息 channel.basicConsume(queueName,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者3: "+new String(body)); } }); } }
1.7、第四种模型(Routing)-订阅-Direct(直连)
在Fanout模式中,一条消息,会被所有订阅的队列都消费。但是,在某些场景下,我们希望不同的消息被不同的队列消费。这时就要用到Direct类型的Exchange。
在Direct模型下:
- P:生产者,向Exchange发送消息,发送消息时,会指定一个routing key。
- X:Exchange(交换机),接收生产者的消息,然后把消息递交给 与routing key完全匹配的队列
- C1:消费者,其所在队列指定了需要routing key 为 error 的消息
- C2:消费者,其所在队列指定了需要routing key 为 info、error、warning 的消息
1)生产者
package direct; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.RabbitMQUtils; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); //获取连接通道对象 Channel channel = connection.createChannel(); String exchangeName = "logs_direct"; //通过通道声明交换机 参数1:交换机名称 参数2:direct 路由模式 channel.exchangeDeclare(exchangeName,"direct"); //发送消息 String routingkey = "info"; channel.basicPublish(exchangeName,routingkey,null,("这是direct模型发布的基于route key: ["+routingkey+"] 发送的消息").getBytes()); //关闭资源 RabbitMQUtils.closeConnectionAndChannel(channel,connection); } }
2)消费者1
package direct; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer1 { public static void main(String[] args) throws IOException { Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); String exchangeName = "logs_direct"; //通道声明交换机以及交换的类型 channel.exchangeDeclare(exchangeName,"direct"); //创建一个临时队列 String queue = channel.queueDeclare().getQueue(); //基于route key绑定队列和交换机 channel.queueBind(queue,exchangeName,"error"); //获取消费的消息 channel.basicConsume(queue,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者1: "+ new String(body)); } }); } }
3)消费者2
package direct; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer2 { public static void main(String[] args) throws IOException { Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); String exchangeName = "logs_direct"; //声明交换机 以及交换机类型 direct channel.exchangeDeclare(exchangeName,"direct"); //创建一个临时队列 String queue = channel.queueDeclare().getQueue(); //临时队列和交换机绑定 channel.queueBind(queue,exchangeName,"info"); channel.queueBind(queue,exchangeName,"error"); channel.queueBind(queue,exchangeName,"warning"); //消费消息 channel.basicConsume(queue,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者2: "+new String(body)); } }); } }
4)测试:当生产者发送Route key为error的消息时
5)测试:当生产者发送Route key为info的消息时
1.8、第五种模型(Routing)-订阅-topic
Topic
类型的Exchange
与Direct
相比,都是可以根据RoutingKey
把消息路由到不同的队列。只不过Topic
类型Exchange
可以让队列在绑定Routing key
的时候使用通配符!这种模型Routingkey
一般都是由一个或多个单词组成,多个单词之间以”.”分割,例如: item.insert
# 统配符 * (star) can substitute for exactly one word. 匹配不多不少恰好1个词 # (hash) can substitute for zero or more words. 匹配0个或多个词 # 如: audit.# 匹配audit.irs.corporate或者 audit.irs 等 audit.* 只能匹配 audit.irs
1)生产者
package topic; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.RabbitMQUtils; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { //获取连接对象 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //声明交换机以及交换机类型 topic channel.exchangeDeclare("topics","topic"); //发布消息 String routekey = "save.user"; channel.basicPublish("topics",routekey,null,("这里是topic动态路由模型,routekey: ["+routekey+"]").getBytes()); //关闭资源 RabbitMQUtils.closeConnectionAndChannel(channel,connection); } }
2)消费者1
package topic; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer1 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //声明交换机以及交换机类型 channel.exchangeDeclare("topics","topic"); //创建一个临时队列 String queue = channel.queueDeclare().getQueue(); //绑定队列和交换机 动态统配符形式route key channel.queueBind(queue,"topics","*.user.*"); //消费消息 channel.basicConsume(queue,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者1: "+ new String(body)); } }); } }
3)消费者2
package topic; import com.rabbitmq.client.*; import utils.RabbitMQUtils; import java.io.IOException; public class Customer2 { public static void main(String[] args) throws IOException { //获取连接 Connection connection = RabbitMQUtils.getConnection(); Channel channel = connection.createChannel(); //声明交换机以及交换机类型 channel.exchangeDeclare("topics","topic"); //创建一个临时队列 String queue = channel.queueDeclare().getQueue(); //绑定队列和交换机 动态统配符形式route key channel.queueBind(queue,"topics","*.user.#"); //消费消息 channel.basicConsume(queue,true,new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println("消费者2: "+ new String(body)); } }); } }
二、springboot中使用RabbitMQ
2.1、搭建初始环境
1)引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2)配置文件
spring: application: name: rabbitmq-springboot rabbitmq: host: 10.0.0.11 port: 5672 username: ems password: 123456 virtual-host: /ems
2.2、hello world模型使用
1)生产者
@Autowired private RabbitTemplate rabbitTemplate; @Test public void testHello(){ rabbitTemplate.convertAndSend("hello","hello world"); }
2)消费者
@Component @RabbitListener(queuesToDeclare = @Queue("hello")) public class HelloCustomer { @RabbitHandler public void receive1(String message){ System.out.println("message = " + message); } }
2.3、work模型使用
1)生产者
@Autowired private RabbitTemplate rabbitTemplate; @Test public void testWork(){ for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend("work","hello work!"); } }
2)消费者
@Component public class WorkCustomer { @RabbitListener(queuesToDeclare = @Queue("work")) public void receive1(String message){ System.out.println("work message1 = " + message); } @RabbitListener(queuesToDeclare = @Queue("work")) public void receive2(String message){ System.out.println("work message2 = " + message); } }
说明:默认在Spring AMQP实现中Work这种方式就是公平调度,如果需要实现能者多劳需要额外配置
2.4、Fanout 广播模型
1)生产者
@Autowired private RabbitTemplate rabbitTemplate; @Test public void testFanout() throws InterruptedException { rabbitTemplate.convertAndSend("logs","","这是日志广播"); }
2)消费者
@Component public class FanoutCustomer { @RabbitListener(bindings = @QueueBinding( value = @Queue, exchange = @Exchange(name="logs",type = "fanout") )) public void receive1(String message){ System.out.println("message1 = " + message); } @RabbitListener(bindings = @QueueBinding( value = @Queue, //创建临时队列 exchange = @Exchange(name="logs",type = "fanout") //绑定交换机类型 )) public void receive2(String message){ System.out.println("message2 = " + message); } }
2.5、Route 路由模型
1)生产者
@Autowired private RabbitTemplate rabbitTemplate; @Test public void testDirect(){ rabbitTemplate.convertAndSend("directs","error","error 的日志信息"); }
2)消费者
@Component public class DirectCustomer { @RabbitListener(bindings ={ @QueueBinding( value = @Queue(), key={"info","error"}, exchange = @Exchange(type = "direct",name="directs") )}) public void receive1(String message){ System.out.println("message1 = " + message); } @RabbitListener(bindings ={ @QueueBinding( value = @Queue(), key={"error"}, exchange = @Exchange(type = "direct",name="directs") )}) public void receive2(String message){ System.out.println("message2 = " + message); } }
2.6、Topic 订阅模型(动态路由模型)
1)生产者
@Autowired private RabbitTemplate rabbitTemplate; //topic @Test public void testTopic(){ rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息"); }
2)消费者
@Component public class TopCustomer { @RabbitListener(bindings = { @QueueBinding( value = @Queue, key = {"user.*"}, exchange = @Exchange(type = "topic",name = "topics") ) }) public void receive1(String message){ System.out.println("message1 = " + message); } @RabbitListener(bindings = { @QueueBinding( value = @Queue, key = {"user.#"}, exchange = @Exchange(type = "topic",name = "topics") ) }) public void receive2(String message){ System.out.println("message2 = " + message); } }