马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
rabbitMQ基础篇学习
代码下载链接
http://121.37.250.237:8080/files/download?filename=mq-demo.zip
假如下载不了,进入http://121.37.250.237:8080/login,用户名root,密码123456
登录后再访问上面链接下载
1.rabbitMQ基础
rabbitMQ分为同步通信和异步通信
同步通信:好比打微信电话,对方的消息这边实时接受,只有两边都在线的情况才能通信乐成
异步通信:微信发送消息,对方不在线,但是消息一直存储在rabbitMQ里,对方上线后,rabbitMQ发送消息,这种就不会存在因为对方不在线出现消息丢失的情况
2.rabbitMQ安装
我们一样平常都在docker里安装mq,详情见docker的rabbitMQ安装
3.rabbitMQ模式
3.1简朴模式
登录rabbitMQ网页创建一个队列,创建消耗者和生产者,生产者通过此队列给消耗者发消息。
消耗者代码:
- @RabbitListener(queues = "simple.queue")
- public void listenSimpleQueue(String msg){
- System.out.println("消费者收到了simple.queue的消息:【" + msg +"】");
- }
复制代码 生产者代码:
- @Test
- public void testSendMessage2Queue() {
- String queueName = "simple.queue";
- String msg = "hello, amqp!";
- try {
- rabbitTemplate.convertAndSend(queueName, msg);
- System.out.println("Message sent successfully");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Error occurred while sending message");
- }
- }
复制代码 该模式下,生产者通过rabbitTemplate.convertAndSend函数,向simple.queue队列发送消息,消耗者通过@RabbitListener绑定simple.queue队列,接受消息
3.2 工作模式
一个生产者,多个消耗者,一个队列,生产者发送消息,所有消耗者都吸收到
消耗者代码:
- @RabbitListener(queues = "work.queue")
- public void listenWorkQueue1(String msg) throws InterruptedException {
- System.out.println("消费者1 收到了 work.queue的消息:【" + msg +"】");
- Thread.sleep(20);
- }
- @RabbitListener(queues = "work.queue")
- public void listenWorkQueue2(String msg) throws InterruptedException {
- System.err.println("消费者2 收到了 work.queue的消息...... :【" + msg +"】");
- Thread.sleep(200);
- }
复制代码 生产者代码:
- @Test
- void testWorkQueue() throws InterruptedException {
- String queueName = "work.queue";
- for (int i = 1; i <= 50; i++) {
- String msg = "hello, worker, message_" + i;
- rabbitTemplate.convertAndSend(queueName, msg);
- Thread.sleep(20);
- }
- }
复制代码 该模式下,生产者发送的队列,所有消耗者都能吸收到消息。
3.发布/订阅模式(fanout)
一个生产者、一个 fanout 范例的交换机、多个队列、多个消耗者。
在网页上生成队列queue1和queue2,再生成一个fanout范例的交换机amq.fanout,用amq.fanout绑定队列queue1和queue2,此时生产者通过交换机amq.fanout发送消息,只有与amq.fanout交换机绑定的队列会吸收到消息(注意消耗者监听的是队列,生产者发送的是交换机)
消耗者代码:
- @RabbitListener(queues = "fanout.queue1")
- public void listenFanoutQueue1(String msg) throws InterruptedException {
- System.out.println("消费者1 收到了 fanout.queue1的消息:【" + msg +"】");
- }
- @RabbitListener(queues = "fanout.queue2")
- public void listenFanoutQueue2(String msg) throws InterruptedException {
- System.out.println("消费者2 收到了 fanout.queue2的消息:【" + msg +"】");
- }
复制代码 生产者代码:
- @Test
- void testSendFanout() {
- String exchangeName = "amq.fanout";
- String msg = "hello, everyone!";
- rabbitTemplate.convertAndSend(exchangeName, null, msg);
- }
复制代码 4.路由模式(direct)
一个生产者,一个 direct 范例的交换机,多个队列。
在网页上生成队列queue1和queue2,再生成一个direct范例的交换机hmall.direct,用hmall.direct绑定队列queue1和queue2,和fanout差别的是,direct交换机可以指定一个routing-key,好比与queue1绑定的routing-key=“red”, “blue”,与queue2绑定的routing-key=“red”, “yellow”。
此时生产者通过交换机hmall.direct发送消息并带上一个routing-key,只有与hmall.direct交换机绑定的队列并且routing-key相称的才会吸收到消息
消耗者代码:
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(name = "direct.queue1", durable = "true"),
- exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
- key = {"red", "blue"}
- ))
- public void listenDirectQueue1(String msg) throws InterruptedException {
- System.out.println("消费者1 收到了 direct.queue1的消息:【" + msg +"】");
- }
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(name = "direct.queue2", durable = "true"),
- exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
- key = {"red", "yellow"}
- ))
- public void listenDirectQueue2(String msg) throws InterruptedException {
- System.out.println("消费者2 收到了 direct.queue2的消息:【" + msg +"】");
- }
复制代码 生产者代码:
- @Test
- void testSendDirect() {
- String exchangeName = "hmall.direct";
- String msg = "蓝色通知,警报解除,哥斯拉是放的气球";
- rabbitTemplate.convertAndSend(exchangeName, "blue", msg);
- }
复制代码 此时只有绑定的key=blue的才能收到消息,也就是消耗者1,假如将生产者代码改为red,消耗者1和消耗者2都能收到消息。
5.主题模式(topic)
一个生产者,一个 topic 范例的交换机,多个队列,交换机与队列之间通过 routing-key 举行关联绑定,多个消耗者。生产者发送消息到交换机并且要指定 routing-key,然后消息根据这交换机与队列之间的 routing-key 绑定规则举行路由被指定消耗者消耗。与路由模式差别是 routing-key 有指定的队则,可以更加的通用,满意更过的场景。routing-key 的规则如下:
#:匹配一个大概多个词,例如lazy.# 可以匹配 lazy.xxx 大概 lazy.xxx.xxx
:只能匹配一个词,例如lazy. 只能匹配 lazy.xxx
消耗者代码:
- @RabbitListener(queues = "topic.queue1")
- public void listenTopicQueue1(String msg) throws InterruptedException {
- System.out.println("消费者1 收到了 topic.queue1的消息:【" + msg +"】");
- }
- @RabbitListener(queues = "topic.queue2")
- public void listenTopicQueue2(String msg) throws InterruptedException {
- System.out.println("消费者2 收到了 topic.queue2的消息:【" + msg +"】");
- }
复制代码 生产者代码:
- @Test
- void testSendTopic() {
- String exchangeName = "hmall.topic";
- String msg = "今天天气挺不错,我的心情的挺好的";
- rabbitTemplate.convertAndSend(exchangeName, "china.weather", msg);
- }
- @Test
- void testSendTopic1() {
- String exchangeName = "hmall.topic";
- String msg = "今天天气挺不错,我的心情的挺好的";
- rabbitTemplate.convertAndSend(exchangeName, "japan.news", msg);
- }
复制代码 |