1、RabbitMQ概述RabbitMQ是使用ERLANG语言编写的实现了高级消息队列协议(AMQP)的开源消息代理软件亦称面向消息的中间件。常见的消息中间件KAFKA、RabbitMQ、ActiveMQ、RocketMQ(可以处理分布式事务)1.1、什么是RabbitMQ?【1、通过案例理解RabbitMQ】你想和远方的好友实现通信这个时候你给好友写封信我们将写好的信封装成一个信封将信放到邮箱中邮差就会在规定的时间将这封信送到远方好友的手中。寄件人是A系统远方好友是B系统RabbitMQ就是邮箱邮差就是信道。A系统和B系统可以通过RabbitMQ实现通信。【2、为什么要使用RabbitMQ? 】1、RabbitMQ实现了AMQP标准的消息服务器2、可靠性RabbitMQ的持久化支持保证了消息的稳定性3、RabbitMQ使用了Erlang开发语言高并发、高可用、集群部署简单等特性【3、RabbitMQ能做什么】1、业务解耦下订单、扣减库存、生成订单、发红包、发短信、异步操作、日志处理、跨平台通信等。2、流量消峰控制请求量。3、路由灵活RabbitMQ有丰富的Exchange(交换器)通过关键字Routingkey判断消息的具体队列(Queue)。1.2、RabbitMQ消息发送原理1、生产者生产消息发送给服务器端的Exchange交换器。2、Exchange收到消息根据用户发送的RoutingKey将消息转发给匹配的Queue1或Queue2。3、Queue1或Queue2收到消息后将消息发送给订阅者(消费者1或消费者2)。4、消费者收到消息发送ACK给队列确认收到消息。5、消费者收到ACK确认消息然后删除队列中缓存的对应消息。1.3、RabbitMQ在项目中的应用A系统和B系统之间如果不是使用同一架构或者语言编写的这个时候可以通过RabbitMQ进行通信。如A系统可以发送JSON类型的数据B系统获取数据的时候可以解析JSON,达到跨系统交互。2、Springboot整合RabbitMQ2.1、配置工程包信息核心包spring-cloud-starter-bus-amqp?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.7.17/version relativePath/ !-- lookup parent from repository -- /parent groupIdcom.txc/groupId artifactIdspringbootrabbitmqdemo/artifactId version0.0.1-SNAPSHOT/version namespringbootrabbitmqdemo/name descriptionspringbootrabbitmqdemo/description properties java.version1.8/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-amqp/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdorg.springframework.amqp/groupId artifactIdspring-rabbit-test/artifactId scopetest/scope /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId configuration excludes exclude groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /exclude /excludes /configuration /plugin /plugins /build /project2.2、在application.yml文件中配置连接信息spring: rabbitmq: addresses: localhost #IP地址 username: guest #用户名 password: guest #密码 dynamic: true #默认创建一个AmqpAdmin的Bean 默认:true2.3、在工程中创建RabbitMQ消息接收程序RabbitListener(queuesToDeclare Queue(myQueue ))作用1、让监听关联到myQueue队列中2、queuesToDeclare能够实现当myQueue队列不存在的时候自动创建3、queuesToDeclare后面的参数需要的是数组类型使用Queue创建Configuration public class RabbitMQReceive { RabbitListener(queuesToDeclare Queue(myQueue)) public void process(String message){ System.out.println(消费消息message); } }2.4、在工程中创建RabbitMQ消息发送程序创建控制层模拟向RabbitMQ消息中间件发送消息。通过convertAndSend向RabbitMQ发送消息。Controller public class TestRabbitMQController { Resource private AmqpTemplate amqpTemplate; RequestMapping(/sendMessageToQueue) ResponseBody public void sendMessageToQueue(){ amqpTemplate.convertAndSend(myQueue,{code:200,msg:你想发的消息。}); } }2.5、测试结果访问地址http://localhost:8080/sendMessageToQueue3、创建Exchange(交换器)绑定Queue3.1、Exchange交换器的使用场景我们要实现如果是华为的订单就发送到huaweiQueue队列中如果是联想的订单就发送到lenovoQueue队列中。主要就是通过RoutingKey(huaweiKey、lenovoKey)来实现。3.2、在工程中创建RabbitMQ接收器通过RabbitListener实现创建huaweiQueue队列和computerExchange交换器并通过huaweiKey实现队列和交换器绑定。在不指定交换器类型的情况下默认的交换器类型是directRabbitListener(bindings QueueBinding( exchange Exchange(computerExchange), key huaweiKey, value Queue(huaweiQueue) )) public void process1(String message){ System.out.println(消费消息message); }3.3、在工程中创建RabbitmQ消息发送程序convertAndSend参数说明convertAndSend(“交换器名称”,”绑定key”,”需要发送的消息”);RequestMapping(/sendMessageToExchange) ResponseBody public void sendMessageToExchange(){ amqpTemplate.convertAndSend(computerExchange,huaweiKey,{code:200,msg:测试Exchange和Queue绑定}); }3.4、测试结果访问地址http://localhost:8080/sendMessageToExchange