springboot接入mq的方法示例
下面以 RabbitMQ 為例,介紹如何在 Spring Boot 中接入 MQ。
1、添加依賴:在 pom.xml 文件中添加 RabbitMQ 的相關(guān)依賴。可以根據(jù)需要選擇合適的版本,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>2、配置 RabbitMQ 連接信息:在 application.properties(或 application.yml)中配置 RabbitMQ 的連接信息,例如:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
3、創(chuàng)建消息發(fā)送者:創(chuàng)建一個類來發(fā)送消息,例如 MessageSender。
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
? ? private final AmqpTemplate amqpTemplate;
? ? @Autowired
? ? public MessageSender(AmqpTemplate amqpTemplate) {
? ? ? ? this.amqpTemplate = amqpTemplate;
? ? }
? ? public void sendMessage(String exchange, String routingKey, Object message) {
? ? ? ? amqpTemplate.convertAndSend(exchange, routingKey, message);
? ? }
}在 MessageSender 類中,通過自動注入 AmqpTemplate 對象,使用 convertAndSend() 方法發(fā)送消息到指定的交換機和路由鍵。
4、創(chuàng)建消息接收者:創(chuàng)建一個類來接收消息,例如 MessageReceiver。
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
? ? @RabbitListener(queues = "myQueue")
? ? public void receiveMessage(String message) {
? ? ? ? // 處理接收到的消息
? ? ? ? System.out.println("Received message: " + message);
? ? }
}在 MessageReceiver 類中,使用 @RabbitListener 注解指定需要監(jiān)聽的隊列,然后定義一個方法來處理接收到的消息。
5、配置交換機和隊列:如果需要自定義交換機和隊列,可以創(chuàng)建一個配置類來進行配置,例如:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
? ? @Bean
? ? public Queue myQueue() {
? ? ? ? return new Queue("myQueue");
? ? }
? ? @Bean
? ? public FanoutExchange fanoutExchange() {
? ? ? ? return new FanoutExchange("myExchange");
? ? }
? ? @Bean
? ? public Binding binding(Queue myQueue, FanoutExchange fanoutExchange) {
? ? ? ? return BindingBuilder.bind(myQueue).to(fanoutExchange);
? ? }
}可以在配置類中使用 @Bean 注解來創(chuàng)建隊列、交換機和綁定規(guī)則。
6、使用消息發(fā)送者發(fā)送消息:在需要發(fā)送消息的地方,通過依賴注入 MessageSender,然后調(diào)用 sendMessage() 方法發(fā)送消息,例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
? ? private final MessageSender messageSender;
? ? @Autowired
? ? public MyController(MessageSender messageSender) {
? ? ? ? this.messageSender = messageSender;
? ? }
? ? @GetMapping("/send-message")
? ? public void sendMessage() {
? ? ? ? String exchange = "myExchange";
? ? ? ? String routingKey = "";
? ? ? ? Object message = "Hello, RabbitMQ!";
? ? ? ? messageSender.sendMessage(exchange, routingKey, message);
? ? }
}我們通過依賴注入 MessageSender 對象,在需要發(fā)送消息的方法中調(diào)用 sendMessage() 方法發(fā)送消息。
我們可以在 Spring Boot 中接入 RabbitMQ,并使用消息發(fā)送者發(fā)送消息,消息接收者監(jiān)聽隊列并處理接收到的消息。同時,還可以根據(jù)需要進行交換機和隊列的配置。
到此這篇關(guān)于springboot接入mq的方法示例的文章就介紹到這了,更多相關(guān)springboot接入mq內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Lambda List轉(zhuǎn)Map代碼實例
這篇文章主要介紹了Java Lambda List轉(zhuǎn)Map代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
springboot+angular4前后端分離 跨域問題解決詳解
這篇文章主要介紹了springboot+angular4前后端分離 跨域問題解決詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解
這篇文章主要介紹了Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解,Sentinel Dashboard中添加的規(guī)則數(shù)據(jù)存儲在內(nèi)存,微服務停掉規(guī)則數(shù)據(jù)就消失,在?產(chǎn)環(huán)境下不合適,我們可以將Sentinel規(guī)則數(shù)據(jù)持久化到Nacos配置中?,讓微服務從Nacos獲取規(guī)則數(shù)據(jù),需要的朋友可以參考下2023-09-09
java WebSocket客戶端斷線重連的實現(xiàn)方法
在工作中是否會遇到實用websocket客戶端連接服務端的時候,網(wǎng)絡波動,服務端斷連的情況,本文可以直接使用的斷線重連,感興趣的可以了解一下2021-10-10
SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn)
本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-11-11

