2024年大数据最新rabbitmq 工作模式(未完全版只demo),2024年最新熬夜肝完这份Framework笔记

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

username: root
password: root
virtual-host: /
listener:
  direct:
    prefetch: 1   #预抓取一条

### 简单模式


#### 调用



package com.example.demo.work;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestSend {
//rabbitmq跟springboot整合,springboot提供了模板给我们使用。
//例如:restTemplate redisTemplate thymeleafTemplate
@Autowired
RabbitTemplate rabbitTemplate;

//1.工作模式

@GetMapping(“testa”)
public void testSendWork() {
//使用convertAndSend
//1.当前队列的名称。2.你要携带的信息内容
try {
rabbitTemplate.convertAndSend(“workqueue”, “测试1!!”);
}catch (Exception e){
e.printStackTrace();
}
}
}


### 接收



package com.example.demo.work;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqListen {
@RabbitListener(queues = “workqueue”)
public void workQueue(String str) {
System.out.println(“当前监听到了:” + str);
}
}


### 工作模式


#### 调用



package com.example.demo.work2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Send1 {
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping(“/send1”)
public void send(){
for (int i = 0; i < 8; i++) {
rabbitTemplate.convertAndSend(“send1”,i);
}

}

}


### 接收


#### yml 文件 设置 prefetch: 1 合理分发



package com.example.demo.work2;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Recv1s {
@RabbitListener(queues = “send1”)
public void send1(String s) throws InterruptedException {
Thread.sleep(Long.parseLong(“1000”));
System.out.println("当前1 "+s);

}
@RabbitListener(queues = "send1")
public void send2(String s){
    System.out.println("当前2 "+s);
}

}


### Publish(广播模式)


#### 调用



package com.example.demo.Publish;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class publish {
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping(“/ces”)
public void testSendPublish(){

    //1.交换机的名称 2.你的规则,发布订阅模式为空 3.消息的主题
    try {
        rabbitTemplate.convertAndSend("logs","","work message");
    }catch (Exception e){
        e.printStackTrace();
    }
}

}


### 接收



package com.example.demo.Publish;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
// d定义一个 log交换机 类型为 fanout
/*@Queue注解为我们提供了队列相关的一些属性,具体如下:
name: 队列的名称;
durable: 是否持久化;
exclusive: 是否独享、排外的;
autoDelete: 是否自动删除;
arguments:队列的其他属性参数,有如下可选项,可参看图2的arguments:*/

@Component
public class PublishListen {
@RabbitListener(bindings = {
@QueueBinding(exchange = @Exchange(value = “logs”,type = “fanout”),//绑定交换机 类型为fanout
value=@Queue // 创建临时队列

        )
})
public void receive(String message){
    System.out.println("receive1 =" +message);
}
@RabbitListener(bindings = {
        @QueueBinding(exchange = @Exchange(value = "logs",type = "fanout"),//绑定交换机 类型为fanout
                value=@Queue // 创建临时队列
        )
})
public void receive2(String message){
    System.out.println("receive2 =" +message);
}

}


### route(路由模式)


#### 创建



package com.example.demo.cesdir;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class Publish1{
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping(“/ces11”)
public void testSendPublish(){

    //1.交换机的名称 2.你的规则,发布订阅模式为空 3.消息的主题
    for (int i = 0; i <6 ; i++) {

String a=“a”+i;
try {
rabbitTemplate.convertAndSend(“cesdir”,a,a);
}catch (Exception e){
e.printStackTrace();
}
}
}

}


### 接收



package com.example.demo.cesdir;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PublishListen1 {
@RabbitListener(bindings = @QueueBinding(
key = {“a1”,“a2”},
exchange=@Exchange(value = “cesdir”,type = “direct”),
value = @Queue

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

[外链图片转存中…(img-sBHvxABN-1714880115063)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ提供了6种工作模式,包括简单模式、work queues、Publish/Subscribe发布与订阅模式、Routing路由模式、Topics主题模式、RPC远程调用模式(不太符合MQ)。这些模式分别具有不同的特点和使用场景。简单模式是最基本的模式,消息发送到队列中被消费者接收。工作队列模式是多个消费者共同消费一个队列中的消息。Publish/Subscribe发布与订阅模式中,生产者将消息发送到交换机,然后交换机将消息广播给所有绑定的队列。Routing路由模式是生产者选择将消息发送到特定的路由键上,消费者通过绑定队列和路由键来接收消息。Topics主题模式类似于Routing模式,但是可以使用通配符进行匹配路由键。RPC远程调用模式用于远程调用服务,不太符合消息队列的特点。 可以看出,这些模式在消息的传输、消费者的数量、消息的路由等方面有所不同。工作队列模式不需要定义交换机,而发布/订阅模式需要定义交换机。发布/订阅模式是面向交换机发送消息,而工作队列模式是面向队列发送消息(底层使用默认交换机)。发布/订阅模式需要设置队列和交换机的绑定,而工作队列模式不需要设置,实际上工作队列模式会将队列绑定到默认的交换机。 综上所述,RabbitMQ的工作模式包括简单模式、work queues、Publish/Subscribe发布与订阅模式、Routing路由模式、Topics主题模式、RPC远程调用模式。每种模式在实际应用中有不同的用途和特点,可以根据具体需求选择合适的工作模式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [RabbitMQ工作模式](https://blog.csdn.net/weixin_42440154/article/details/124689685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [RabbitMQ六种工作模式详解](https://blog.csdn.net/qq_44760609/article/details/125084962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值