一、什么是Exchange
RabbitMQ 是 AMQP(高級消息隊列協議)的標准實現:
從 AMQP 協議可以看出,Queue、Exchange 和 Binding 構成了 AMQP 協議的核心
-
Producer:消息生產者,即投遞消息的程序。
-
Broker:消息隊列服務器實體。
-
Exchange:消息交換機,它指定消息按什么規則,路由到哪個隊列。
-
Binding:綁定,它的作用就是把 Exchange 和 Queue 按照路由規則綁定起來。
-
Queue:消息隊列載體,每個消息都會被投入到一個或多個隊列。
-
-
Consumer:消息消費者,即接受消息的程序。
二、Exchange的類型
RabbitMQ常用的Exchange Type有fanout、direct、topic、headers這四種,本文主要通過direct方式,實現生產者與消費者實例
三、具體操作步驟
- 創建生產者 新建springboot web項目,file->new->project
- 在pom.xml文件添加引用
<!-- 添加springboot對amqp的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- SLf4j 日志記錄--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>1.18.12</version> </dependency>
- 在application.properties文件中添加連接MQ配置,注意端口是5672,不是15672,15672是web瀏覽端口
# 應用名稱
spring.application.name=springboot-rabbitmq-receive
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
- 創建RabbitMQ配置類,注意添加的注解是Configuration
package com.howdy.common.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //注意 這個地方不是 Configurable
public class RabbitMQConf {
@Bean
public Queue helloQueue() {
// 第一個參數是創建的queue的名字,第二個參數是是否支持持久化
return new Queue("hello_queue_test", true);
}
}
- 創建發送消息服務類
package com.howdy.common.service; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @Slf4j public class HelloSender { @Autowired private AmqpTemplate template; public void Send() { String msg = "hello rabbitmq..."; template.convertAndSend("hello_queue_test", msg); log.info("hello_queue_test隊列發送消息:" + msg); } }
- 創建測試方法類
package com.howdy.common.controller; import com.howdy.common.service.HelloSender; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestRabbitMQ { @Autowired private HelloSender helloSender; @GetMapping("/testRabbit") public String testRabbit(){ helloSender.Send(); return "消息發送成功"; } }
- 生產者 完整目錄結構
2、創建消費者項目,大部分與生產者相同,下面我只列出不同的地方
- 創建消費端,消費方法
package com.howdy.common.service; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @Slf4j public class HelloReceive { @RabbitHandler @RabbitListener(queues = "hello_queue_test") //監聽隊列名為hello的隊列 public void ProcessClient(String msg) { log.info("消費端接收到消息:"+msg); } }
- 完整目錄結構
- 最后測試結果,如圖: