本文源码:https://download.csdn.net/download/nealinvip/11181266
1. 基础知识
RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现。
基础概念:
1.Queue(队列)。特征是先进先出,用于存储消息。生产者生产消息,消费者消费消息。可以存在多个消费者订阅同一个队列,一起处理这些消息。
2.Exchange(交换器)。生产者产生的消息并不是直接发送给消息队列Queue的,而是要经过Exchange(交换器),由Exchange再将消息路由到一个或多个Queue。RabbitMQ是通过Binding将Exchange和Queue结合在一起。
Exchange Type(交换器类型)
fanout
- fanout:将生产者消息发送到所有跟他关联的queue中。相当于广播1 V all。
- direct:将生产者消息发送到Routing Key与Binding Key完全匹配的Queue中,相当于点对点1v1
- topic:将生产者消息发送到Routing Key与Binding Key模糊匹配的Queue,相当于组播1 V n
· routing key为一个句点号"."分隔的字符串(每一段独立的字符串称为一个单词),例如:topic.sys
· "*"与"#"用于做模糊匹配,其中‘’**"用于匹配一个单词,"#"用于匹配多个单词(可以是零个或多个)
- headers:headers类型的Exchange不依赖于routing key与binding key的匹配规则来路由消息,而是根据发送的消息内容中的headers属性进行匹配。并不常用。
2. rabbitMQ3.7.14 windows下的安装
- 安装erlang 我下载的是:OTP 22.0 Windows 64-bit Binary File
- 安装rabbitmq,我下载的是:rabbitmq-server-3.7.14.exe
- 安装过程都随意,没什么需要注意的。安装后,可以通过windows 服务控制rabbitmq的启停。
- 安装后,通过端口号15672进行访问,http://localhost:15672/ 用户名和密码都是guest(管理员权限)。
3. springboot2.1.4+rabbitMQ3.7.14
- application.properties添加RabbitMQ的配置信息
#********rabbitMQ start*********
spring.application.name=spring-boot-rabbitmq-sender
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#********rabbitMQ end*********
- 依赖中添加相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3.1 Direct模式
- 配置类
@SpringBootConfiguration
public class DirectConfig {
@Bean
public Queue stringQueue() {
return new Queue("stringQueue");
}
@Bean
public Object objectQueue() {
return new Queue("objectQueue");
}
}
- 消息发送
@RestController
@RequestMapping("directSenderController")
public class DirectSenderController {
@Resource
private RabbitTemplate rabbitTemplate;
/**
* 用于发送字符串消息到队列
*/
@RequestMapping("string")
public void send1() {
this.rabbitTemplate.convertAndSend("stringQueue", "hello world");
}
/**
* 用于发送实体类类型消息到队列
*/
@RequestMapping("object")
public void send2() {
User user = new User();
user.setUsername("LBJ");
user.setPassword("35");
rabbitTemplate.convertAndSend("objectQueue", user);
}
}
- 消息消费
@Component
public class DirectReceiver {
//监听器监听stringQueue
@RabbitListener(queues="stringQueue")
public void process1(String str) {
System.out.println("Receive:"+str);
}
//监听器监听objectQueue
@RabbitListener(queues="objectQueue")
public void process2(User user) {
System.out.println(user);
}
}
通过http://localhost:8080/directSenderController/string 可以发送实体类类型消息, http://localhost:8080/directSenderController/object 可以发送实体类类型消息。
3.2 Topic模式
- 配置类
@Configuration
public class TopicConfig {
@Bean(name="male")
public Queue queueMessage() {
return new Queue("topic.male");
}
@Bean(name="people")
public Queue queueMessages() {
return new Queue("topic.people");
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("exchange");
}
/**
* 用于接收topic.male的队列
* @param queueMessage
* @param exchange
* @return
*/
@Bean
Binding bindingExchangeMessage(@Qualifier("male") Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.male");
}
/**
* 用于接收topic.*的队列
* @param queueMessages
* @param exchange
* @return
*/
@Bean
Binding bindingExchangeMessages(@Qualifier("people") Queue queueMessages, TopicExchange exchange) {
//*表示一个词,#表示零个或多个词
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.*");
}
}
- 消息发送类
@RestController
@RequestMapping("topicController")
public class TopicController {
@Autowired
private AmqpTemplate template;
@RequestMapping("send")
public String send() {
template.convertAndSend("exchange","topic.female","hello rabbit~~");
template.convertAndSend("exchange","topic.male","hello rabbit~~");
return "success";
}
}
- 消息接收类
@Component
public class TopicReceiver {
@RabbitListener(queues="topic.male") //监听器监听指定的Queue
public void process1(String str) {
System.out.println("male:"+str);
}
@RabbitListener(queues="topic.people") //监听器监听指定的Queue
public void process2(String str) {
System.out.println("people:"+str);
}
}
通过http://localhost:8080/topicController/send可以发送消息到topic模式的队列中。
打印结果:
3.3 Fanout模式
- 配置类
@SpringBootConfiguration
public class FanoutConfig {
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("helloFanoutExchange");
}
@Bean
public Queue carQueue() {
return new AnonymousQueue();
}
@Bean
public Queue busQueue() {
return new AnonymousQueue();
}
@Bean
public Binding carBinding(FanoutExchange fanoutExchange, Queue carQueue) {
return BindingBuilder.bind(carQueue).to(fanoutExchange);
}
@Bean
public Binding busBinding(FanoutExchange fanoutExchange, Queue busQueue) {
return BindingBuilder.bind(busQueue).to(fanoutExchange);
}
}
- 消息发送类
@RestController
@RequestMapping("fanoutSenderController")
public class FanoutSenderController {
@Autowired
private FanoutExchange fanoutExchange;
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("send")
public void send() {
System.out.println(fanoutExchange.getName());
rabbitTemplate.convertAndSend(fanoutExchange.getName(), "", "Hello world");
}
}
- 消息接收类
@Component
public class FanoutReceiver {
@RabbitListener(queues = "#{carQueue.name}")
public void carReceiver(String str) {
System.out.println("carReceiver:" + str);
}
@RabbitListener(queues = "#{busQueue.name}")
public void busReceiver(String str) {
System.out.println("busReceiver:" + str);
}
}