第一節、 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服務器的運行情況。感謝支持,你的支持是我前進的動力!!!更多優質內容請關注我的微信公衆號“浩哥分享”。