前言
本章講解RabbitMQ整合SpringBoot時的基本使用
方法
1.環境搭建
1)創建相應springboot工程
2)修改pom文件,添加相關的座標
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<groupId>cn.edu.ccut</groupId>
<artifactId>springboot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- 設定Java的版本 -->
<java.version>1.8</java.version>
<!-- 解決pom.xml首行報錯 -->
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- 配置springBoot的web啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置rabbitmq的啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 配置devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 配置springBoot的test啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.edu.ccut.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
3)修改application.properties文件,添加rabbitmq的相關配置
spring.application.name=springboot-rabbitmq
#rabbitmq config
spring.rabbitmq.host=192.168.1.108
spring.rabbitmq.port=5672
spring.rabbitmq.username=jwang
spring.rabbitmq.password=123456
2.Provider和Consumer的編寫
1)創建配置類,用於隊列的生成
package cn.edu.ccut.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class QueueConfig {
@Bean
public Queue getQueue(){
return new Queue("hello-queue");
}
}
2)編寫生產者Provider代碼
package cn.edu.ccut;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Provider {
@Autowired
private AmqpTemplate rabbitTemplate;
public void sendMsg(String msg){
rabbitTemplate.convertAndSend("hello-queue", msg);
}
}
convertAndxiaoxiSend方法的兩個參數分別爲隊列名稱和要發送的消息
3)編寫消費者代碼
package cn.edu.ccut;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@RabbitListener(queues="hello-queue")
public void receiveMsg(String msg){
System.out.println("this message is "+msg);
}
}
消費者使用@RabbitListener註解監聽指定的隊列發送的消息進行消費!
3.編寫測試類,測試運行
1)編寫測試代碼
package cn.edu.ccut;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class RabbitMQTest {
@Autowired
private Provider provider;
@Test
public void testSendMsg(){
provider.sendMsg("hello rabbitmq !");
}
}
2)運行改代碼,觀察控制檯效果
由此可見,我們的消費者端成功的捕獲了這個消息
3)觀察rabbitmq的控制端效果
該面板記錄了我們所創建的隊列的一些相關的屬性