欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot系列教程之7步集成RabbitMQ的方法

 更新時間:2018年11月12日 11:44:59   作者:JackieZheng  
RabbitMQ 即一個消息隊列,主要是用來實現(xiàn)應(yīng)用程序的異步和解耦,同時也能起到消息緩沖,消息分發(fā)的作用。下面這篇文章主要給大家介紹了關(guān)于Spring Boot之7步集成RabbitMQ的相關(guān)資料,需要的朋友可以參考下

前言

RabbitMQ是一種我們經(jīng)常使用的消息中間件,RabbitMQ是實現(xiàn)AMQP(高級消息隊列協(xié)議)的消息中間件的一種,最初起源于金融系統(tǒng),用于在分布式系統(tǒng)中存儲轉(zhuǎn)發(fā)消息,在易用性、擴展性、高可用性等方面表現(xiàn)不俗。RabbitMQ主要是為了實現(xiàn)系統(tǒng)之間的雙向解耦而實現(xiàn)的。當生產(chǎn)者大量產(chǎn)生數(shù)據(jù)時,消費者無法快速消費,那么需要一個中間層。保存這個數(shù)據(jù)。

AMQP,即Advanced Message Queuing Protocol,高級消息隊列協(xié)議,是應(yīng)用層協(xié)議的一個開放標準,為面向消息的中間件設(shè)計。消息中間件主要用于組件之間的解耦,消息的發(fā)送者無需知道消息使用者的存在,反之亦然。AMQP的主要特征是面向消息、隊列、路由(包括點對點和發(fā)布/訂閱)、可靠性、安全。

RabbitMQ是一個開源的AMQP實現(xiàn),服務(wù)器端用Erlang語言編寫,支持多種客戶端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系統(tǒng)中存儲轉(zhuǎn)發(fā)消息,在易用性、擴展性、高可用性等方面表現(xiàn)不俗。

今天這篇,我們來看看Spring Boot是如何集成RabbitMQ,發(fā)送消息和消費消息的。同時我們介紹下死信隊列。

集成RabbitMQ

集成RabbitMQ只需要如下幾步即可

1、添加maven依賴

<!--rabbitmq-->

<dependency>

 <groupId>org.springframework.boot</groupId>

 <artifactId>spring-boot-starter-amqp</artifactId>

</dependency>

2、添加配置文件application.yaml

在application.yaml添加配置內(nèi)容如下

spring: rabbitmq:
 host: 192.168.1.161
 port: 5672
 username: guest
 password: guest
 cache:
 channel: size: 10
 listener:
 type: simple
 simple:
 acknowledge-mode: auto
 concurrency: 5
 default-requeue-rejected: true
 max-concurrency: 100
 retry:
 enabled: true # initial-interval: 1000ms
 max-attempts: 3 # max-interval: 1000ms
 multiplier: 1
 stateless: true # publisher-confirms: true</pre>

注意:

這里最基本的配置只需要配置host,port,username和password四個屬性即可

其他屬性都有各自的含義,比如retry是用于配置重試策略的,acknowledge-mode是配置消息接收確認機制的。

3、編寫配置類

編寫RabbitConfig配置類,采用Java Configuration的方式配置RabbitTemplate、Exchange和Queue等信息,具體如下所示

package com.jackie.springbootdemo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

@Configuration public class RabbitMQConfig implements InitializingBean { @Autowired
 SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory;

 @Override
 public void afterPropertiesSet() throws Exception {
 simpleRabbitListenerContainerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
 } @Bean("jackson2JsonMessageConverter")
 public Jackson2JsonMessageConverter jackson2JsonMessageConverter(ConnectionFactory connectionFactory) {
 return new Jackson2JsonMessageConverter();
 } @Bean("rabbitTemplate")
 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
 public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,
  @Qualifier("jackson2JsonMessageConverter") Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
 RabbitTemplate template = new RabbitTemplate(connectionFactory);
 template.setMessageConverter(new Jackson2JsonMessageConverter());
 return template;
 } // --------------------- 聲明隊列 ------------------------
 @Bean
 public Queue demoQueue() {
 return new Queue("demo_queue");
 } // --------------------- 聲明exchange ------------------------ @Bean
 public DirectExchange demoExchange() {
 return new DirectExchange("demo_exchange");
 } // --------------------- 隊列綁定 ------------------------
 @Bean
 public Binding bindingAlbumItemCreatedQueue(DirectExchange demoExchange,
  Queue demoQueue) {
 return BindingBuilder.bind(demoQueue).to(demoExchange).with("100");
 } }

注意

這里聲明了Direct模式的Exchange,聲明一個Queue,并通過routing-key為100將demo_queue綁定到demo_exchange,這樣demo_queue就可以接收到demo_exchange發(fā)送的消息了。

4、編寫消息發(fā)送類

package com.jackie.springbootdemo.message;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component public class Sender implements RabbitTemplate.ConfirmCallback { private RabbitTemplate rabbitTemplate;

 /**
 * 構(gòu)造方法注入 */ @Autowired
 public Sender(RabbitTemplate rabbitTemplate) {
 this.rabbitTemplate = rabbitTemplate;
 rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果為單例的話,那回調(diào)就是最后設(shè)置的內(nèi)容
 } public void sendMsg(String content) {
 rabbitTemplate.convertAndSend("demo_exchange", "100", content);
 } /**
 * 回調(diào) */ @Override
 public void confirm(CorrelationData correlationData, boolean ack, String cause) {
 System.out.println(" 回調(diào)id:" + correlationData);
 if (ack) {
 System.out.println("消息成功消費");
 } else {
 System.out.println("消息消費失敗:" + cause);
 }
 } }

注意

發(fā)送內(nèi)容content,路由到routing-key為100上,則我們就可以在demo_queue隊列中看到發(fā)送的消息內(nèi)容了

confirm函數(shù)是回調(diào)函數(shù),這里因為沒有消費者,且acknoledge-mode是auto(其他兩種值分別是none和manual),所以ack是false。

5、編寫發(fā)送消息測試類

package com.jackie.springbootdemo;

import com.jackie.springbootdemo.message.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootDemoApplication.class) @WebAppConfiguration public class RabbitApplicationTests { @Autowired
 Sender sender;

 @Test
 public void contextLoads() throws Exception {
 sender.sendMsg("test");
 } } 

運行該測試類,我們可以看到如下結(jié)果

6、編寫消息消費類

package com.jackie.springbootdemo.message;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component public class Receiver { @RabbitListener(queues = "demo_queue")
 public void created(String message) {
 System.out.println("orignal message: " + message);
 } } 

注意

消息消費類也非常簡單,添加注解@RabbitListener,指定要監(jiān)聽的隊列名稱即可

除了注解@RabbitListener,我們經(jīng)常還能看到@RabbitHandler,這兩個注解可以配合起來使用。

@RabbitListener 標注在類上面表示當有收到消息的時候,就交給 @RabbitHandler 的方法處理,具體使用哪個方法處理,根據(jù) MessageConverter 轉(zhuǎn)換后的參數(shù)類型,形如

@RabbitListener(queues = "demo_queue") public class Receiver { @RabbitHandler public void processMessage1(String message) {
 System.out.println(message);
 } @RabbitHandler
 public void processMessage2(byte[] message) {
 System.out.println(new String(message));
 } }

7、運行消息發(fā)送測試類

從執(zhí)行結(jié)果可以看到,因為有了消費者,所以這次打印的結(jié)果是"消息消費成功"

而且,我們看到Receiver類將消息消費并打印出消息的內(nèi)容為"test"。

代碼已經(jīng)提交至項目rome:https://github.com/DMinerJackie/rome (本地下載

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Lombok插件的安裝與簡單使用步驟

    Lombok插件的安裝與簡單使用步驟

    這篇文章主要介紹了Lombok插件的安裝與簡單使用步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 基于Java判斷網(wǎng)絡(luò)是否正常代碼實例

    基于Java判斷網(wǎng)絡(luò)是否正常代碼實例

    這篇文章主要介紹了基于Java判斷網(wǎng)絡(luò)是否正常代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Java?Spring?boot實現(xiàn)生成二維碼

    Java?Spring?boot實現(xiàn)生成二維碼

    大家好,本篇文章主要講的是Java?Spring?boot實現(xiàn)生成二維碼,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • SpringBoot中Mybatis + Druid 數(shù)據(jù)訪問的詳細過程

    SpringBoot中Mybatis + Druid 數(shù)據(jù)訪問的詳細過程

    Spring Boot 底層都是采用 SpringData 的方式進行統(tǒng)一處理各種數(shù)據(jù)庫,SpringData也是Spring中與SpringBoot、SpringCloud 等齊名的知名項目,下面看下SpringBoot Mybatis Druid數(shù)據(jù)訪問的詳細過程,感興趣的朋友一起看看吧
    2021-11-11
  • java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例

    java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例

    這篇文章主要介紹了java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例方法的相關(guān)資料
    2017-02-02
  • SpringBoot整合mybatisplus和druid的示例詳解

    SpringBoot整合mybatisplus和druid的示例詳解

    這篇文章主要介紹了SpringBoot整合mybatisplus和druid的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-08-08
  • IDEA 配置 JRebel 熱部署的方法(推薦)

    IDEA 配置 JRebel 熱部署的方法(推薦)

    這篇文章主要介紹了IDEA 配置 JRebel 熱部署的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 分享Java死鎖的4種排查工具

    分享Java死鎖的4種排查工具

    這篇文章主要介紹了分享Java死鎖的4種排查工具,死鎖指的是兩個或兩個以上的運算單元,都在等待對方停止執(zhí)行,以取得系統(tǒng)資源,但是沒有一方提前退出,就稱為死鎖,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-05-05
  • Java二維數(shù)組實戰(zhàn)案例

    Java二維數(shù)組實戰(zhàn)案例

    這篇文章主要介紹了Java二維數(shù)組,結(jié)合具體案例形式分析了java二維數(shù)組定義、遍歷、計算等相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • SpringBoot實現(xiàn)redis延遲隊列的示例代碼

    SpringBoot實現(xiàn)redis延遲隊列的示例代碼

    延時隊列場景在我們?nèi)粘I(yè)務(wù)開發(fā)中經(jīng)常遇到,它是一種特殊類型的消息隊列,本文就來介紹一下SpringBoot實現(xiàn)redis延遲隊列的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02

最新評論