SpringBoot專案整合RabbitMQ
前言:
本篇部落格主要說明SpringBoot專案如何整合RabbitMQ。
一。生產者工程
1.在pom.xml檔案中匯入rabbitmq依賴:
<!-- rabbitmq依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置檔案配置如下:
server:
port: 8001
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: hello
username: guest
password: guest
3.連線rabbitmq配置類如下:
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 ITEM_TOPIC_EXCHANGE = "item_topic_exchange";
//佇列名稱
public static final String ITEM_QUEUE = "item_queue";
//宣告交換機
@Bean("itemTopicExchange")
public Exchange topicExchange(){
return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
}
//宣告佇列
@Bean("itemQueue")
public Queue itemQueue(){
return QueueBuilder.durable(ITEM_QUEUE).build();
}
//繫結佇列和交換機
@Bean
public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
@Qualifier("itemTopicExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
}
}
4.控制器示例程式碼如下:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 傳送訊息的測試類
*/
@RestController
public class SendMsgController {
//注入RabbitMQ的模板
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 測試
*/
@GetMapping("/sendmsg")
public String sendMsg(@RequestParam String msg, @RequestParam String key){
/**
* 傳送訊息
* 引數一:交換機名稱
* 引數二:路由key
* 引數三:傳送的訊息
*/
rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg);
//返回訊息
return "傳送訊息成功!";
}
}
5.啟動類程式碼如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitmqProducerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqProducerApplication.class,args);
}
}
二。消費者工程
1.與生產者工程一樣,在pom.xml檔案中匯入rabbitmq依賴
<!-- rabbitmq依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置檔案配置如下:
server:
port: 8002
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: hello
username: guest
password: guest
3.監聽佇列類程式碼如下:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
/**
* 監聽某個佇列的訊息
* @param message 接收到的訊息
*/
@RabbitListener(queues = "item_queue")
public void myListener1(String message){
System.out.println("消費者接收到的訊息為:" + message);
}
}
4.啟動類程式碼如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitmqConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqConsumerApplication.class,args);
}
}
三。測試
1.先啟動生產者,通過瀏覽器訪問介面傳送三條訊息進入佇列中:
2.開啟rabbit管控臺,可以檢視到新建立的item_topic_exchange
交換機以及所繫結的item_queue
佇列:
3.檢視到item_queue
佇列中有含有三條未消費的訊息:
4.啟動消費者,消費者消費訊息:
至此,springboot與rabbitmq訊息佇列整合成功!
相關文章
- RabbitMQ -springboot整合rabbitmqMQSpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- Springboot整合RabbitMQSpring BootMQ
- Java SpringBoot 整合 RabbitMQJavaSpring BootMQ
- SpringBoot專案整合MinIOSpring Boot
- springboot專案整合mybatisSpring BootMyBatis
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- springboot2.0整合rabbitmqSpring BootMQ
- SpringBoot專案整合日誌Spring Boot
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- RabbitMQ(二)JavaClient SpringBoot整合 Work queuesMQJavaclientSpring Boot
- Springboot2+vue2整合專案Spring BootVue
- SpringBoot + Mybatis + Redis 整合入門專案Spring BootMyBatisRedis
- RabbitMQ簡介以及與SpringBoot整合示例MQSpring Boot
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- SpringBoot專案整合阿里Druid連線池Spring Boot阿里UI
- SpringBoot2.0原始碼分析(三):整合RabbitMQ分析Spring Boot原始碼MQ
- SpringBoot整合RabbitMQ實戰附加死信交換機Spring BootMQ
- SpringBoot 整合 RabbitMQ 實現訊息可靠傳輸Spring BootMQ
- springboot專案中整合ip2region包 FileNotFoundExceptionSpring BootException
- springboot+vue專案如何整合企業微信Spring BootVue
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- SpringBoot整合rabbitMq實現訊息延時傳送Spring BootMQ
- Springboot專案中需整合的依賴集和檔案配置Spring Boot
- 將zookeeper curator與springboot專案進行整合(重點)Spring Boot
- springBoot專案部署整合阿里雲https證書報錯Spring Boot阿里HTTP
- 在前後端分離專案中使用SpringBoot整合Shiro後端Spring Boot
- Flowable與springBoot專案整合及出現的問題Spring Boot
- RabbitMQ-Spring整合RabbitMQMQSpring
- SpringBoot整合RabbitMQ之典型應用場景實戰二Spring BootMQ
- SpringBoot整合RabbitMQ之典型應用場景實戰一Spring BootMQ
- 【Java分享客棧】SpringBoot整合WebSocket+Stomp搭建群聊專案JavaSpring BootWeb
- springboot系列學習(十九):springboot專案整合Druid,Druid到底是什麼,他是在專案中如何使用的Spring BootUI
- SpringBoot系列(十五)整合快取,專案會用得到的技術Spring Boot快取
- SpringBoot專案搭建Spring Boot
- 專案整合管理