up:: RabbitMQ入门交换机类型
说明:
(1) 本篇博客的内容:在Spring Boot项目中使用RabbitMQ;
(2) 本篇博客的内容,虽然略显呆板,不贴合实际开发;但,自己感觉到,只要对本篇博客的内容清楚明白,稍微 _ 调整_ 就能在实际项目中初步使用RabbitMQ;
零:前期准备:创建两个Spring Boot项目,分别充当Producer和Consumer;
关于Spring Boot项目的创建,可以参考
● 【创建SpringBoot项目】;
● 【SpringBoot电商项目数据库表设计】;
1.创建【spring-boot-rabbitmq-producer】项目,充当Producer;
2.创建【spring-boot-rabbitmq-consumer】项目,充当Consumer;
一:基于【spring-boot-rabbitmq-producer】项目,整合RabbitMQ,开发Producer;
声明: 这儿的内容,为了演示在Spring Boot中使用RabbitMQ,可能会显得死板、僵化、过于工整、啰嗦;只要我们心里能明白这个过程,在实际开发中,可以根据当前业务和项目的需求,灵活的使用;自然,究竟如何在实际的项目中,比较规范的、贴合实际的使用RabbitMQ,还需要积累和总结;
1.在pom.xml中,引入RabbitMQ依赖;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.在application.properties中,配置RabbitMQ;
server.port=8080
spring.application.name=producer
#配置RabbitMQ的地址和端口号;用户名和密码;虚拟机;
spring.rabbitmq.addresses=1**.***.***.**8:5672
#配置用户名和密码;
spring.rabbitmq.username=admin
spring.rabbitmq.password=password
#虚拟机使用默认的/
spring.rabbitmq.virtual-host=/
#配置连接的超时时间
spring.rabbitmq.connection-timeout=15000
说明:
(1) 能够感受到,Spring Boot为了能够支持RabbitMQ,为了能让用户在Spring Boot项目中 比较方便的 使用RabbitMQ,背后还是做了很多支撑的;
3.创建TopicRabbitConfig类,使用【使用Java Config方式实现SpringIoC】的方式,实例化两个队列(Queue)、一个交换机(Exchange),队列绑定到交换机(Binding);
TopicRabbitConfig类:
package com.imooc.springbootrabbitmqproducer;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 描述:RabbitMQ配置类:配置队列,交换机,队列与交换机的绑定;
*/
@Configuration
public class TopicRabbitConfig {
//首先,定义两个队列,queue1和queue2;
@Bean
public Queue queue1() {
return new Queue("queue1");
}
@Bean
public Queue queue2() {
return new Queue("queue2");
}
//然后,定义一个交换机
@Bean
TopicExchange exchange() {
return new TopicExchange("bootExchange");
}
//然后,把队列绑定到交换机上去;
@Bean
Binding bindingExchangeMessage1(Queue queue1, TopicExchange exchange) {
return BindingBuilder.bind(queue1).to(exchange).with("dog.red");
}
@Bean
Binding bindingExchangeMessage2(Queue queue2, TopicExchange exchange) {
return BindingBuilder.bind(queue2).to(exchange).with("dog.#");
}
}
说明:
(1) Spring Boot为了能够更好的让用户使用RabbitMQ,其会把连接RabbitMQ中需要用到的队列,交换机等,使用IoC容器管理起来;(虽然,其深入的内容不了解,但目前是可以这样理解的);
(2) 这儿使用的是【使用Java Config方式实现Spring IoC】的方式,来实例化对象的;如有需要可以参考【JavaConfig-对象依赖注入】;
(3) 类内容说明;
4.创建MsgSender类,编写发送消息的逻辑方法;
MsgSender类:
package com.imooc.springbootrabbitmqproducer;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 描述:发送消息的
*/
@Component
public class MsgSender {
@Autowired
private AmqpTemplate rabbitmqTemplate;
//发送一个消息
public void send1() {
String message = "This message 1 and routing key is dog.red";
System.out.println("发送了" + message);
this.rabbitmqTemplate.convertAndSend("bootExchange", "dog.red", message);
}
//再发送一个消息
public void send2() {
String message = "This message 2 and routing key is dog.black";
System.out.println("发送了" + message);
this.rabbitmqTemplate.convertAndSend("bootExchange","dog.black", message);
}
}
说明:
(1) 这儿需要引入AmqpTemplate这个工具类,然后利用这个工具类去发送消息;
(2) 基于topic模式的交换机的特点,根据我们设置的Routing Key,可以发现下面的情况;
5.为了能够发送消息,我们编写SpringBootRabbitmqProducerApplicationTests测试类,调用MsgSender类中编写的发送消息的逻辑方法;
二:基于【spring-boot-rabbitmq-consumer】项目,整合RabbitMQ,开发Consumer;
声明: 这儿的内容,为了演示在Spring Boot中使用RabbitMQ,可能会显得死板、僵化、过于工整、啰嗦;只要我们心里能明白这个过程,在实际开发中,可以根据当前业务和项目的需求,灵活的使用;自然,究竟如何在实际的项目中,比较规范的、贴合实际的使用RabbitMQ,还需要积累和总结;
1.在pom.xml中,引入RabbitMQ依赖;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.在application.properties中,配置RabbitMQ;
server.port=8081
spring.application.name=consumer
#配置RabbitMQ的地址和端口号;用户名和密码;虚拟机;
spring.rabbitmq.addresses=1**.***.***.**8:5672
#配置用户名和密码;
spring.rabbitmq.username=admin
spring.rabbitmq.password=password
#虚拟机使用默认的/
spring.rabbitmq.virtual-host=/
#配置连接的超时时间
spring.rabbitmq.connection-timeout=15000
说明:
(1) 能够感受到,Spring Boot为了能够支持RabbitMQ,为了能让用户在Spring Boot项目中 比较方便的 使用RabbitMQ,背后还是做了很多支撑的;
(2) 因为,为了演示,我们会在本机开启两个Spring Boot项目,所以这个项目的端口号设置为了8081;
3.创建Receive1类和Receive2类,去演示接收消息;
(1)Receive1类;
package com.imooc.springbootrabbitmqconsumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 描述:从RabbitMQ中,接收消息
*/
@Component
@RabbitListener(queues = "queue1")
public class Receiver1 {
@RabbitHandler
public void process(String message) {
System.out.println("Receiver1收到了:" + message);
}
}
说明:
(1) 使用@RabbitListener和@RabbitHandler来,帮助接收和处理消息;
(2)Receive2类;
package com.imooc.springbootrabbitmqconsumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 描述:从RabbitMQ中,接收消息
*/
@Component
@RabbitListener(queues = "queue2")
public class Receiver2 {
@RabbitHandler
public void process(String message) {
System.out.println("Receiver2收到了:" + message);
}
}
(3)@RabbitListener和@RabbitHandler注解简介;
例如:当发送map类型时,使用参数是Map类型的process()方法进行处理;
三:运行效果;
1.首先,运行producer项目的test类,去创建队列,创建交换机,建立连接,发送消息;
通过RabbitMQ的管理后台,可以看到交换机和队列都创建了;
2.然后,运行consumer项目,去接收消息;
发现结果符合预期;