今天来和朋友们一起学习下,SpringBoot怎么整合RabbitMQ。目前消息组件大致有三种:.activemq, rabbitmq, kafka。这三者各有优缺点,RabbitMQ相比之下是处于其他二者之间的一个消息组件。RabbitMQ依赖于erlang,在linux下安装的话,要先安装erlang环境。下面来看看怎么SpringBoot 怎么整合RabbitMQ吧。

  1. 想要使用RabbitMQ ,pom依赖是少不了的~
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.再来看看application.yml文件的内容

spring:rabbitmq:username: rabbitpassword: 123456host: localhostport: 5672virtual-host: /#手动ACK 不开启自动ACK模式,目的是防止报错后未正确处理消息丢失 默认 为 nonelistener:simple:acknowledge-mode: manual

RabbitMQConfig的内容(注册)

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitMQConfig { public static final String DEFAULT_MAIL_QUEUE = "dev.mail.register.default.queue"; public static final String MANUAL_MAIL_QUEUE = "dev.mail.register.manual.queue"; @Bean public Queue defaultMailQueue (){ // Queue queue = new Queue(Queue名称,消息是否需要持久化处理) return new Queue(DEFAULT_MAIL_QUEUE, true); } @Bean public Queue manualMailQueue(){ return new Queue(MANUAL_MAIL_QUEUE, true); } }

搞两个监听器(使用@RabbitListener注解)来监听下这两种消息 (怎么感觉自己现在说话一股土味儿,最近吃土吃多了么~ 好吧,写的代码估计也是土味的吧)(监听队列)


import com.developlee.rabbitmq.config.RabbitMQConfig;
import com.developlee.rabbitmq.entity.MailEntity;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.io.IOException; @Component public class MailHandler { private static final Logger logger = LoggerFactory.getLogger(MailHandler.class); /** * <p>TODO 该方案是 spring-boot-data-amqp 默认的方式,不太推荐。具体推荐使用 listenerManualAck()</p> * 默认情况下,如果没有配置手动ACK, 那么Spring Data AMQP 会在消息消费完毕后自动帮我们去ACK * 存在问题:如果报错了,消息不会丢失,但是会无限循环消费,一直报错,如果开启了错误日志很容易将磁盘空间耗完 * 解决方案:手动ACK,或者try-catch 然后在 catch 里面讲错误的消息转移到其它的系列中去 * spring.rabbitmq.listener.simple.acknowledge-mode=manual * <p> * * @param mail 监听的内容 */ @RabbitListener(queues = {RabbitMQConfig.DEFAULT_MAIL_QUEUE}) public void listenerAutoAck(MailEntity mail, Message message, Channel channel) { //TODO 如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉 final long deliveryTag = message.getMessageProperties().getDeliveryTag(); try { logger.info("listenerAutoAck 监听的消息-{}", mail.toString()); //TODO 通知MQ 消息已被成功消费,可以ACK了 channel.basicAck(deliveryTag, false); } catch (IOException e) { //处理失败, 重新压入MQ. try { channel.basicRecover(); } catch (IOException e1) { e1.printStackTrace(); } } } @RabbitListener(queues = {RabbitMQConfig.MANUAL_MAIL_QUEUE}) public void listenerManualAck(MailEntity mail, Message message, Channel channel) { logger.info("listenerManualAck 监听的消息-{}", mail.toString()); try { //TODO 通知MQ 消息已被成功消费,可以ACK了 channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (Exception e) { //如果报错,容错处理, } } }

再来一波测试代码,测试下......(实例化队列)

import com.developlee.rabbitmq.config.RabbitMQConfig;
import com.developlee.rabbitmq.entity.MailEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Lee * @// TODO 2018/6/22-11:20 * @description */ @RestController @RequestMapping(value = "/mail") public class MailController { private final RabbitTemplate rabbitTemplate; @Autowired public MailController(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; } /** * this.rabbitTemplate.convertAndSend(RabbitConfig.DEFAULT_MAIL_QUEUE, mailEntity); * 对应 {@link MailHandler#listenerAutoAck}; * this.rabbitTemplate.convertAndSend(RabbitConfig.MANUAL_MAIL_QUEUE, mailEntity); * 对应 {@link MailHandler#listenerManualAck}; */ @GetMapping("/default") public void defaultMailMsg() { MailEntity mailEntity = new MailEntity(); mailEntity.setId("1"); mailEntity.setName("First Mail Message"); mailEntity.setTitle("RabbitMQ with Spring boot!"); mailEntity.setContent("Come on! Let's study Micro-Service together!"); this.rabbitTemplate.convertAndSend(RabbitMQConfig.DEFAULT_MAIL_QUEUE, mailEntity); this.rabbitTemplate.convertAndSend(RabbitMQConfig.MANUAL_MAIL_QUEUE, mailEntity); } }

MailEntity.java

import java.io.Serializable;public class MailEntity implements Serializable { private static final long serialVersionUID = -2164058270260403154L; private String id; private String name; private String title; private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }

启动项目 ,浏览器地址栏输入http://localhost:8080/mail。 something you will find in your heart。

 

转载于:https://www.cnblogs.com/huangwentian/p/10375151.html

精通SpringBoot---整合RabbitMQ消息队列相关推荐

  1. SpringBoot整合RabbitMQ消息队列

    RabbitMQ 一.RabbitMQ介绍 1.1 现存问题 服务调用:两个服务调用时,我们可以通过传统的HTTP方式,让服务A直接去调用服务B的接口,但是这种方式是同步的方式,虽然可以采用Sprin ...

  2. SpringBoot整合RabbitMQ 消息可靠投递、手动ack、延迟队列、死信队列、消息幂等性保障、消息积压

    1.消息可靠投递 在使用 RabbitMQ 的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败场景.RabbitMQ 为我们提供了两种方式用来控制消息的投递可靠性模式. confirm 确认模式 ...

  3. springboot整合redis消息队列

    前言 消息队列作为一种常用的异步通信解决方案,而redis是一款高性能的nosql产品,今天就给大家介绍一下,如何使用redis实现消息队列,并整合到springboot. 两个消息模型 1. 队列模 ...

  4. SpringBoot使用RabbitMQ消息队列

    RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的 ...

  5. SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门

    1.Windows下安装RabbitMQ的步骤详解+图解(erlang+RabbitMQ) 2.SpringBoot集成RabbitMQ参考文章 1.RabbitMQ介绍 RabbitMQ是实现AMQ ...

  6. SpringBoot整合activeMQ消息队列手动签收(Session.CLIENT_ACKNOWLEDGE)为什么失效啊?

    今天在家隔离办公,不太忙,然后就琢磨起来消息队列activeMQ的消息事务来解决分布式事务,但是奈何在SpringBoot整合activeMQ时,其消费者手动签收消息时出现了问题-->当acti ...

  7. SpringBoot整合MQ消息队列

    SpringBoot整合MQ 借鉴的文章 1.什么是MQ 2.消息队列可以做什么 3.下载安装MQ 4.SpringBoot整合MQ的步骤 借鉴的文章 https://www.jianshu.com/ ...

  8. RabbitMQ消息队列(六):SpringBoot整合之通配符模式

    RabbitMQ消息队列(六):SpringBoot整合之通配符模式 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AeZQrNHS-1660220618697)(E: ...

  9. SpringBoot笔记:消息队列(RabbitMQ)

    什么是消息队列 消息队列就是消息存储的容器,Java里面有两种 JMS:Sun公司出品,有两种模式,点对点和发布订阅. AMQP:消息队列的一个协议,其实现有RabbitMQ,stormMQ等 我们会 ...

最新文章

  1. 随机森林 java_机器学习weka,java api调用随机森林及保存模型
  2. [题解]UVA10054 The Necklace
  3. 文件和目录之stat、fstat和lstat函数
  4. python get argument_Python-Web框架-get_argument方法
  5. python多个函数_什么是在Python中使用多个构造函数的干净的、pythonic的方法?
  6. jQuery对checkbox的操作(转载)
  7. 计算所有1-100之间数字之和(偶数之和)代码
  8. 主成分分析(PCA)-最大方差解释
  9. NB-IoT 的“前世今生”
  10. 北大主场夺金ACM-ICPC全球总决赛,总教练罗国杰分享背后“秘笈”
  11. JPA唯一索引更新删除的问题
  12. 《HeadFirst SQL》笔记
  13. CMMI3级认证过程记录
  14. 本周大新闻|Elbit推飞行员专属AR头盔,苹果第二代MR将分高低配
  15. Vue实现tab导航栏,支持左右滑动
  16. ps做手机计算机界面,用PS制作手机UI界面设计
  17. Spring Boot自定义starter
  18. Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
  19. 自定义小部件Widget的探讨
  20. 背篼酥课堂第八课--APP开发--app图形化编程

热门文章

  1. 团队计划(4.27)
  2. iphone 数据存储之属性和归档archive
  3. JavaScript 弹出窗口总结
  4. iRobot 公司招聘,机器人、SLAM、视觉感知、路径规划方向
  5. CV新赛事:口罩佩戴检测
  6. ICCV2019 | 腾讯优图13篇论文入选,其中3篇被选为Oral
  7. 阿里达摩院-视觉方向(校招、社招、实习),欢迎各路大神
  8. 内推 | 旷视研究院深度学习实习生招聘(含内推邮箱)
  9. Github | 标星20k+ Facebook 开源高效词表征学习库fastText
  10. SSE指令集学习之旅(一)