SpringBoot整合消息隊(duì)列RabbitMQ
簡介
在Spring項(xiàng)目中,可以使用Spring-Rabbit去操作RabbitMQ
https://github.com/spring-projects/spring-amqp
尤其是在spring boot項(xiàng)目中只需要引入對應(yīng)的amqp啟動器依賴即可,方便的使用RabbitTemplate發(fā)送消息,使用注解接收消息。
一般在開發(fā)過程中:
生產(chǎn)者工程:
- application.yml文件配置RabbitMQ相關(guān)信息;
- 在生產(chǎn)者工程中編寫配置類,用于創(chuàng)建交換機(jī)和隊(duì)列,并進(jìn)行綁定
- 注入RabbitTemplate對象,通過RabbitTemplate對象發(fā)送消息到交換機(jī)
消費(fèi)者工程:
- application.yml文件配置RabbitMQ相關(guān)信息
- 創(chuàng)建消息處理類,用于接收隊(duì)列中的消息并進(jìn)行處理
生產(chǎn)端
1. 創(chuàng)建生產(chǎn)者SpringBoot工程(maven)
2. 引入start,依賴坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>3. 編寫yml配置,基本信息配置
4. 定義交換機(jī),隊(duì)列以及綁定關(guān)系的配置類
5. 注入RabbitTemplate,調(diào)用方法,完成消息發(fā)送
添加依賴
修改pom.xml文件內(nèi)容為如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.4.RELEASE</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot-rabbitmq-producer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
啟動類
package com.itheima.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
配置RabbitMQ
配置文件
創(chuàng)建application.yml,內(nèi)容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
綁定交換機(jī)和隊(duì)列
創(chuàng)建RabbitMQ隊(duì)列與交換機(jī)綁定的配置類com.itheima.rabbitmq.config.RabbitMQConfig
package com.itheima.rahhitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration /// 配置類
public class RabbitMQConfig {
public static final String EXCHAGE_NAME = "boot_topic_exchange";
public static final String QUEUE_NAME = "boot_queue";
// 交換機(jī)
@Bean("bootExchange")
public Exchange bootExchange(){
// 構(gòu)建交換機(jī)對象
return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build();
}
//Queue 隊(duì)列
@Bean("bootQueue")
public Queue bootQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
//隊(duì)列和交換機(jī)的關(guān)系 Binding
/**
* 1 知道那個隊(duì)列
* 2 知道那個交換機(jī)
* 3 routingKey
*/
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
搭建消費(fèi)者工程
創(chuàng)建工程
生產(chǎn)端
1. 創(chuàng)建生產(chǎn)者SpringBoot工程
2. 引入start,依賴坐標(biāo)
org.springframework.boot
spring-boot-starter-amqp
編寫yml配置,基本信息配置
定義交換機(jī),隊(duì)列以及綁定關(guān)系的配置類
注入RabbitTemplate,調(diào)用方法,完成消息發(fā)送
添加依賴
修改pom.xml文件內(nèi)容為如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.4.RELEASE</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot-rabbitmq-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
啟動類
package com.itheima.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
配置RabbitMQ
創(chuàng)建application.yml,內(nèi)容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
消息監(jiān)聽處理類
編寫消息監(jiān)聽器com.itheima.rabbitmq.listener.MyListener
package com.itheima.rabbitmq.listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
/**
* 監(jiān)聽某個隊(duì)列的消息
* @param message 接收到的消息
*/
@RabbitListener(queues = "item_queue")
public void myListener1(String message){
System.out.println("消費(fèi)者接收到的消息為:" + message);
}
}
測試
在生產(chǎn)者工程springboot-rabbitmq-producer中創(chuàng)建測試類,發(fā)送消息:
package com.itheima.rabbitmq;
import com.itheima.rabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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
public class RabbitMQTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 為item.insert");
rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 為item.update");
rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品刪除,routing key 為item.delete");
}
}
先運(yùn)行上述測試程序(交換機(jī)和隊(duì)列才能先被聲明和綁定),然后啟動消費(fèi)者;在消費(fèi)者工程springboot-rabbitmq-consumer中控制臺查看是否接收到對應(yīng)消息。
SpringBoot提供了快速整合RabbitMQ的方式
基本信息再yml中配置,隊(duì)列交互機(jī)以及綁定關(guān)系在配置類中使用Bean的方式配置
生產(chǎn)端直接注入RabbitTemplate完成消息發(fā)送
消費(fèi)端直接使用@RabbitListener完成消息接收
到此這篇關(guān)于SpringBoot整合消息隊(duì)列RabbitMQ的文章就介紹到這了,更多相關(guān)SpringBoot整合RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA 2019.2.2配置Maven3.6.2打開Maven項(xiàng)目出現(xiàn) Unable to import Maven
這篇文章主要介紹了IDEA 2019.2.2配置Maven3.6.2打開Maven項(xiàng)目出現(xiàn) Unable to import Maven project的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
如何使用@Value和@PropertySource注入外部資源
這篇文章主要介紹了如何使用@Value和@PropertySource注入外部資源的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java小項(xiàng)目之迷宮游戲的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Java小項(xiàng)目之迷宮的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java連接 JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查)
這篇文章主要介紹了Java連接 JDBC基礎(chǔ)知識,包括操作數(shù)據(jù)庫之增刪改查操作,需要的朋友可以參考下2021-04-04
java解析php函數(shù)json_encode unicode 編碼問題
這篇文章主要介紹了java解析php函數(shù)json_encode unicode 編碼問題,需要的朋友可以參考下2016-04-04
Java?Lambda表達(dá)式常用的函數(shù)式接口
這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對常用的函數(shù)式接口的介紹,具有一的的參考價值需要的小伙伴可以參考一下2022-04-04
Spring?Aop+Redis實(shí)現(xiàn)優(yōu)雅記錄接口調(diào)用情況
通常情況下,開發(fā)完一個接口,無論是在測試階段還是生產(chǎn)上線,我們都需要對接口的執(zhí)行情況做一個監(jiān)控,所以本文為大家整理了Spring統(tǒng)計接口調(diào)用的多種方法,希望對大家有所幫助2023-06-06
Spring監(jiān)聽器及定時任務(wù)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring監(jiān)聽器及定時任務(wù)實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07

