SpringBoot項(xiàng)目嵌入RocketMQ的實(shí)現(xiàn)示例
在Spring Boot中嵌入RocketMQ可以通過添加相應(yīng)的依賴來完成。
首先需要在pom.xml文件中引入spring-boot-starter-amqp依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>然后,在application.properties或者application.yml配置文件中設(shè)置RocketMQ連接信息:
# RocketMQ服務(wù)器地址 rocketmq.name-server=127.0.0.1:9876 # 生產(chǎn)者分組 rocketmq.producer.group=my-group
最后,創(chuàng)建消息發(fā)送者(Producer)和消息接收者(Consumer)類,并使用@Autowired注解將其自動(dòng)裝載到Spring容器中。示例如下:
創(chuàng)建消息發(fā)送者類:
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@GetMapping("/send")
public String send(String message) {
rocketMQTemplate.convertAndSend("test-topic", message);
return "Message: '" + message + "' sent.";
}
}創(chuàng)建消息接收者類:
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
@Service
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "my-consumer_test-topic")
public class ConsumerService implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.printf("------- StringConsumer received: %s \n", message);
}
}當(dāng)調(diào)用/send接口時(shí),會(huì)向"myQueue"隊(duì)列發(fā)送消息;
而MessageReceiver則會(huì)監(jiān)聽該隊(duì)列,并處理接收到的消息。
查看控制臺(tái)的輸出來驗(yàn)證消息消費(fèi)者是否可以正常接收消息
到此這篇關(guān)于SpringBoot項(xiàng)目嵌入RocketMQ的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot嵌入RocketMQ內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 淺談Springboot整合RocketMQ使用心得
- springBoot整合RocketMQ及坑的示例代碼
- SpringBoot整合RocketMQ實(shí)現(xiàn)消息發(fā)送和接收的詳細(xì)步驟
- Springboot RocketMq實(shí)現(xiàn)過程詳解
- 解決SpringBoot整合RocketMQ遇到的坑
- SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息
- Springboot詳解RocketMQ實(shí)現(xiàn)消息發(fā)送與接收流程
- SpringBoot集成RocketMQ的使用示例
- SpringBoot+RocketMQ實(shí)現(xiàn)延遲消息的示例代碼
相關(guān)文章
java實(shí)現(xiàn)點(diǎn)擊按鈕彈出新窗體功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)點(diǎn)擊按鈕彈出新窗體功能,舊窗體不進(jìn)行操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
idea項(xiàng)目實(shí)現(xiàn)移除和添加git
本文指導(dǎo)讀者如何從官網(wǎng)下載并安裝Git,以及在IDEA中配置Git的詳細(xì)步驟,首先,用戶需訪問Git官方網(wǎng)站下載適合自己操作系統(tǒng)的Git版本并完成安裝,接著,在IDEA中通過設(shè)置找到git.exe文件以配置Gi2024-10-10
如何使用 IntelliJ IDEA 編寫 Spark 應(yīng)用程序(Sc
本教程展示了如何在IntelliJIDEA中使用Maven編寫和運(yùn)行一個(gè)簡(jiǎn)單的Spark應(yīng)用程序(例如WordCount程序),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11
Java?MyBatis傳出參數(shù)resultType和resultMap解讀
這篇文章主要介紹了Java?MyBatis傳出參數(shù)resultType和resultMap解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
java基礎(chǔ)之Collection與Collections和Array與Arrays的區(qū)別
這篇文章主要介紹了java基礎(chǔ)之Collection與Collections和Array與Arrays的區(qū)別的相關(guān)資料,本文主要說明兩者的區(qū)別以防大家混淆概念,需要的朋友可以參考下2017-08-08
使用spring的restTemplate注意點(diǎn)
這篇文章主要介紹了使用spring的restTemplate注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

