SpringBoot與rabbitmq的結(jié)合的示例
消息中間件對(duì)于我們系統(tǒng)之間的解耦合,消峰等都有極大的幫助。spring boot 也集成了此部分的內(nèi)容,集成最為容易的是rabbitmq。今天我們就以rabbitmq為例說(shuō)明。
老規(guī)矩,先看下pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
AMQP,即Advanced Message Queuing Protocol,一個(gè)提供統(tǒng)一消息服務(wù)的應(yīng)用層標(biāo)準(zhǔn)高級(jí)消息隊(duì)列協(xié)議,是應(yīng)用層協(xié)議的一個(gè)開(kāi)放標(biāo)準(zhǔn),為面向消息的中間件設(shè)計(jì)?;诖藚f(xié)議的客戶(hù)端與消息中間件可傳遞消息,并不受客戶(hù)端/中間件不同產(chǎn)品,不同的開(kāi)發(fā)語(yǔ)言等條件的限制,spring-boot-starter-amqp引入的就是rabbitmq。有個(gè)前提,你的機(jī)子上要首先先安裝rabbitmq的server,然后執(zhí)行 rabbitmq-server server就啟動(dòng)了。啟動(dòng)后,我們就可以配置我們的客戶(hù)端程序了。首先看下我們的配置文件
spring.application.name: spirng-boot-rabbitmq spring.rabbitmq.host: 127.0.0.1 spring.rabbitmq.port: 5672 spring.rabbitmq.username: guest spring.rabbitmq.password: guest
配置了服務(wù)器的IP,端口,用戶(hù)名,密碼等基礎(chǔ)信息,保證我們能連上服務(wù)器。
增加一個(gè)Rabbitmq的配置類(lèi)
package com.shuqi;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue Queue() {
return new Queue("hello");
}
}
創(chuàng)建了一個(gè)名稱(chēng)叫做hello的隊(duì)列,然后producer可以往hello的隊(duì)列里放數(shù)據(jù),consumer可以從hello的隊(duì)列里消費(fèi)數(shù)據(jù)??聪聀roducer的處理程序
package com.shuqi.controller;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private AmqpTemplate rabbitTemplate;
@RequestMapping("/hello")
public String hello(@RequestParam String name){
rabbitTemplate.convertAndSend("hello","hello "+name);
return "消息發(fā)送成功";
}
}
通過(guò)controller生產(chǎn)消息,通過(guò)AmqpTemplate發(fā)送消息。有了生產(chǎn)者我們看下消費(fèi)者
package com.shuqi.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "hello")
@Slf4j
public class HelloConsumer {
@RabbitHandler
public void process(String hello) {
log.info("接收到的消息:message:{}",hello);
}
}
@RabbitListener(queues = "hello") 表示是一個(gè)Rabbitmq的監(jiān)聽(tīng)器,監(jiān)聽(tīng)的隊(duì)列名稱(chēng)是hello,說(shuō)明數(shù)據(jù)可定會(huì)過(guò)來(lái),數(shù)據(jù)過(guò)來(lái)了,通過(guò) @RabbitHandler 修飾的方法來(lái)處理過(guò)來(lái)的數(shù)據(jù)。打印一下。下面我們啟動(dòng)項(xiàng)目看看效果。
在瀏覽器中輸入 http://localhost:8080/hello?name=shuqi 看到下面的結(jié)果

看下控制臺(tái)輸出的日志
2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer : 接收到的消息:message:hello shuqi
說(shuō)明消息已經(jīng)被consumer接收并處理掉了。大家可以把玩下。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例
- Spring Boot中使用RabbitMQ的示例代碼
- spring boot使用RabbitMQ實(shí)現(xiàn)topic 主題
- spring boot中使用RabbitMQ routing路由詳解
- Spring Boot整合RabbitMQ開(kāi)發(fā)實(shí)戰(zhàn)詳解
- 淺談spring-boot-rabbitmq動(dòng)態(tài)管理的方法
- springboot集成rabbitMQ之對(duì)象傳輸?shù)姆椒?/a>
- springboot整合rabbitmq的示例代碼
- Spring boot集成RabbitMQ的示例代碼
相關(guān)文章
Java通過(guò)調(diào)用FFMPEG獲取視頻時(shí)長(zhǎng)
這篇文章主要為大家詳細(xì)介紹了Java通過(guò)調(diào)用FFMPEG獲取視頻時(shí)長(zhǎng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
JSON序列化Redis讀取出錯(cuò)問(wèn)題解決方案
這篇文章主要介紹了JSON序列化Redis讀取出錯(cuò)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
java實(shí)現(xiàn)文件上傳的詳細(xì)步驟
文件上傳是用戶(hù)將本地文件通過(guò)Web頁(yè)面提交到服務(wù)器的過(guò)程,涉及客戶(hù)端、服務(wù)器端、上傳表單等組件,在SpringBoot中,通過(guò)MultipartFile接口處理上傳文件,并將其保存在服務(wù)器,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
springboot對(duì)接第三方微信授權(quán)及獲取用戶(hù)的頭像和昵稱(chēng)等等
這篇文章主要介紹了springboot對(duì)接第三方微信授權(quán)及獲取用戶(hù)的頭像和昵稱(chēng)等等,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot 使用hibernate validator校驗(yàn)
這篇文章主要介紹了SpringBoot 使用hibernate validator校驗(yàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
解決@MapperScan和@Mapper共存之坑X(jué)xxMapper?that?could?not?be?fo
這篇文章主要介紹了解決@MapperScan和@Mapper共存之坑X(jué)xxMapper?that?could?not?be?found問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
MyBatis常見(jiàn)報(bào)錯(cuò)問(wèn)題及解決方案
這篇文章主要介紹了MyBatis常見(jiàn)報(bào)錯(cuò)問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

