第一節、 RabbitMQ最常用到的五大模式
- 前言
- RabbitMQ簡介
- RabbitMQ五大模式實戰
- 總結
前言
Spring AMQP 專案是應用了spring的核心概念到AMQP協議訊息解決方案中,我們提供了一個“template”作為更高級別的抽象去發送和接收訊息,我也提供了訊息驅動類的支持,使用依賴注入和宣告式編程可以更好的管理AMQP源代碼,此專案中你可以看到和SpringFramework中JMS一些相似的地方,
RabbitMQ簡介
-
訊息佇列是應用程式和應用程式之間的一種通信方法,
-
RabbitMQ : erlang語言開發、 基于AMQP協議,
-
同類產品:ActiveMQ、 ZeroMQ、 RabbitMQ、 RocketMQ、 Kafka,
-
物理模型

-
Broker 訊息佇列服務行程、 Exchange訊息佇列交換機,Queue 訊息佇列、 Producer 訊息生產者、 Consumer 訊息消費者,
-
六種模式: 簡單模式、 作業模式、 發布與訂閱模式、 路由模式、通配符模式、 遠程呼叫模式(基本不會用到),
-
關鍵詞:{Broker: 服務器物體、 Exchange :訊息交換機、 Queue: 訊息佇列載體、Binding: 系結 、Routing Key: 路由關鍵字、 VHost: 虛擬主機、Producer: 訊息生產者 、 Consumer: 訊息消費者、Channel: 訊息通道 }
-
關鍵概念:由Exchange、Queue、RoutingKey三個才能決定一個從Exchange到Queue的唯一的線路,
RabbitMQ五大模式實戰
此次是基于SpringBoot開發的RabbitMQ應用程式,利用SpringBoot的自動配置和起步依賴會讓你更快更方便的構建專案,
讓我們實戰開始,
- 準備階段
- 啟動一臺RabbitMQ服務器
- 此次使用的是SpringBoot專案
- 應用的pom依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 配置application.yml檔案
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
server:
port: 8082
- 啟動類和目錄結構是SpringBoot常規設定,這里不再贅述,
注意:啟動類名設定為RabbitmqProducerApplication
- 簡單模式
- 簡單模式組態檔
@Configuration
public class RabbitSimpleConfig {
@Bean
public Queue simpleQueue(){
return new Queue("simpleQueue");
}
}
- 簡單模式生產者部分
@SpringBootTest(classes = RabbitmqProducerApplication.class)
public class ProducerTest {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void simpleProduct(){
for (int num = 0; num < 20; num++) {
rabbitTemplate.convertAndSend("simpleQueue", "簡單模式"+num);
}
}
}
- 簡單模式消費者部分
@Component
public class MessageListener {
@RabbitListener(queues = "simpleQueue")
public void simpleListener(String message){
System.out.println("簡單模式監聽器:"+message);
}
}
- 作業模式
- 作業模式組態檔
@Bean
public Queue workQueue(){
return new Queue("workQueue");
}
- 作業模式生產者部分
@Test
public void workProduct(){
for (int num = 0; num < 20; num++) {
rabbitTemplate.convertAndSend("workQueue", "作業模式"+num);
}
}
- 作業模式消費者部分
@RabbitListener(queues = "workQueue")
public void workListener1(String message) {
System.out.println("作業模式監聽器1:" + message);
}
@RabbitListener(queues = "workQueue")
public void workListener2(String message) {
System.out.println("作業模式監聽器2:" + message);
}
- 發布訂閱模式
- 發布訂閱模式組態檔
//配置交換器
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//配置佇列
@Bean
public Queue fanoutQueue1() {
return new Queue("fanoutQueue1", true, false, false, null);
}
@Bean
public Queue fanoutQueue2() {
return new Queue("fanoutQueue2", true, false, false, null);
}
//配置系結
@Bean
public Binding fanoutBinding1(FanoutExchange fanoutExchange, Queue fanoutQueue1) {
return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
}
@Bean
public Binding fanoutBinding2(FanoutExchange fanoutExchange, Queue fanoutQueue2) {
return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
}
- 發布訂閱模式生產者部分
@Test
public void FanoutProduct(){
for (int num = 0; num < 10; num++) {
rabbitTemplate.convertAndSend("fanoutExchange","","發布訂閱模式"+num);
}
}
- 發布訂閱模式消費者部分
@RabbitListener(queues = "fanoutQueue1")
public void fanoutListener1(String message) {
System.out.println("發布訂閱監聽器1:" + message);
}
@RabbitListener(queues = "fanoutQueue2")
public void fanoutListener2(String message) {
System.out.println("發布訂閱監聽器2:" + message);
}
- 路由模式
- 路由模式組態檔
//配置交換機
@Bean
public DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
//配置佇列
@Bean
public Queue directQueue1() {
return new Queue("directQueue1", true, false, false, null);
}
@Bean
public Queue directQueue2() {
return new Queue("directQueue2", true, false, false, null);
}
//配置系結
@Bean
public Binding directBinding1(Queue directQueue1, DirectExchange directExchange) {
return BindingBuilder.bind(directQueue1).to(directExchange).with("one");
}
@Bean
public Binding directBinding2(Queue directQueue2, DirectExchange directExchange) {
return BindingBuilder.bind(directQueue2).to(directExchange).with("two");
}
- 路由模式生產者部分
@Test
public void directProduct1() {
for (int num = 0; num < 5; num++) {
rabbitTemplate.convertAndSend("directExchange","one", "發送到路由佇列1訊息"+num);
}
}
@Test
public void directProduct2() {
for (int num = 0; num < 5; num++) {
rabbitTemplate.convertAndSend("directExchange","two", "發送到路由佇列2訊息"+num);
}
}
- 路由模式消費者部分
@RabbitListener(queues = "directQueue1")
public void fanoutListener1(String message) {
System.out.println("路由模式監聽器1:" + message);
}
@RabbitListener(queues = "directQueue2")
public void fanoutListener2(String message) {
System.out.println("路由模式監聽器2:" + message);
}
- 通配符模式
- 通配符模式組態檔
//配置佇列
@Bean
public Queue topicQueue1() {
return new Queue("topicQueue1");
}
@Bean
public Queue topicQueue2() {
return new Queue("topicQueue2");
}
//配置交換器
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
//配置系結
@Bean
public Binding topicBinding1(Queue topicQueue1, TopicExchange topicExchange) {
return BindingBuilder.bind(topicQueue1).to(topicExchange).with("topic.*");
}
@Bean
public Binding topicBinding2(Queue topicQueue2, TopicExchange topicExchange) {
return BindingBuilder.bind(topicQueue2).to(topicExchange).with("topic.#");
}
- 通配符模式生產者部分
/*
* 通配符模式測驗
* */
@Test
public void topicProduct() {
rabbitTemplate.convertAndSend("topicExchange","topic.one", "routkey為topic.one的訊息");
rabbitTemplate.convertAndSend("topicExchange","topic.one.two", "routkey為topic.one.two的訊息");
}
- 通配符模式消費者部分
@RabbitListener(queues = "topicQueue1")
public void fanoutListener1(String message) {
System.out.println("通配符監聽器1:" + message);
}
@RabbitListener(queues = "topicQueue2")
public void fanoutListener2(String message) {
System.out.println("通配符監聽器2:" + message);
}
總結
以上就是SpringBoot+RabbitMQ五大模式的簡單使用實體,到目前為止RabbitMQ也是Sping AMQP的唯一實作,下一節將會講解RabbitMQ可視化管理界面,可視化管理界面幫助我們可以直觀地看到RabbitMQ服務器的運行情況,感謝支持,你的支持是我前進的動力!!!更多優質內容請關注我的微信公眾號“浩哥分享”,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/232673.html
標籤:其他
