SpringBoot與rabbitmq的結(jié)合的示例
消息中間件對于我們系統(tǒng)之間的解耦合,消峰等都有極大的幫助。spring boot 也集成了此部分的內(nèi)容,集成最為容易的是rabbitmq。今天我們就以rabbitmq為例說明。
老規(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,一個提供統(tǒng)一消息服務的應用層標準高級消息隊列協(xié)議,是應用層協(xié)議的一個開放標準,為面向消息的中間件設(shè)計。基于此協(xié)議的客戶端與消息中間件可傳遞消息,并不受客戶端/中間件不同產(chǎn)品,不同的開發(fā)語言等條件的限制,spring-boot-starter-amqp引入的就是rabbitmq。有個前提,你的機子上要首先先安裝rabbitmq的server,然后執(zhí)行 rabbitmq-server server就啟動了。啟動后,我們就可以配置我們的客戶端程序了。首先看下我們的配置文件
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
配置了服務器的IP,端口,用戶名,密碼等基礎(chǔ)信息,保證我們能連上服務器。
增加一個Rabbitmq的配置類
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)建了一個名稱叫做hello的隊列,然后producer可以往hello的隊列里放數(shù)據(jù),consumer可以從hello的隊列里消費數(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ā)送成功";
}
}
通過controller生產(chǎn)消息,通過AmqpTemplate發(fā)送消息。有了生產(chǎn)者我們看下消費者
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") 表示是一個Rabbitmq的監(jiān)聽器,監(jiān)聽的隊列名稱是hello,說明數(shù)據(jù)可定會過來,數(shù)據(jù)過來了,通過 @RabbitHandler 修飾的方法來處理過來的數(shù)據(jù)。打印一下。下面我們啟動項目看看效果。
在瀏覽器中輸入 http://localhost:8080/hello?name=shuqi 看到下面的結(jié)果

看下控制臺輸出的日志
2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer : 接收到的消息:message:hello shuqi
說明消息已經(jīng)被consumer接收并處理掉了。大家可以把玩下。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot對接第三方微信授權(quán)及獲取用戶的頭像和昵稱等等
這篇文章主要介紹了springboot對接第三方微信授權(quán)及獲取用戶的頭像和昵稱等等,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
SpringBoot 使用hibernate validator校驗
這篇文章主要介紹了SpringBoot 使用hibernate validator校驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
解決@MapperScan和@Mapper共存之坑XxxMapper?that?could?not?be?fo
這篇文章主要介紹了解決@MapperScan和@Mapper共存之坑XxxMapper?that?could?not?be?found問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

