RabbitMQ使用及与spring boot整合
1.MQ
消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法
应用:不同进程Process/线程Thread之间通信
比较流行的中间件:
ActiveMQ
RabbitMQ(非常重量级,更适合于企业级的开发)
Kafka(高吞吐量的分布式发布订阅消息系统)
RocketMQ
在高并发、可靠性、成熟度等方面,RabbitMQ是首选
Kafka的性能(吞吐量、TPS)比RabbitMq要高出来很多,但Kafka主要定位在日志方面,如果业务方面还是建议选择RabbitMq
2.AMQP
Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计
主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全
3.RabbitMQ
RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写
支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX
用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗
(1)安装
需要先安装Erlang ,再安装RabbitMQ
环境:win7
Erlang
下载 :
https://www.erlang-solutions.com/resources/download.html
安装:
双击下载的文件(esl-erlang_22.1~windows_amd64.exe) ,下一步进行安装
安装完后开始菜单多了
RabbitMQ
下载 :
https://www.rabbitmq.com/download.html
安装:
双击下载的文件(rabbitmq-server-3.8.1.exe) ,下一步进行安装
安装完后开始菜单多了
选择开始菜单的RabbitMQ Command Prompt(sbin dir)
进入C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.4.1\sbin输入命令
rabbitmq-plugins enable rabbitmq_management
启动了管理工具
服务启动 net start RabbitMQ
服务停止 net stop RabbitMQ
服务启动后,浏览器打开http://localhost:15672/
使用账号 guest ,密码 guest
能够登录,安装成功
(2)用户管理
Admin选项卡
A.添加用户
用户角色:
超级管理员(administrator)
监控者(monitoring)
策略制定者(policymaker)
普通管理者(management)
其他
B.创建Virtual Hosts
C.设置权限
选中Admin用户,进入权限设置
已添加权限
(3)spring boot整合RabbitMQ
添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId><version>2.2.1.RELEASE</version>
</dependency>
添加配置
#对于rabbitMQ的支持
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=testhost
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
添加RabbitMQ配置类
package com.example.demo.configure;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitMqConfig {public static final String RABBITMQ_QUEUE_NAME = "Queue1";public static final String RABBITMQ_ORDER_QUEUE_NAME = "OrderQueue1";private final static Logger logger = LoggerFactory.getLogger(RabbitMqConfig.class);@Autowiredprivate CachingConnectionFactory cachingConnectionFactory;@Beanpublic Queue commonQueue() {return new Queue(RabbitMqConfig.RABBITMQ_QUEUE_NAME);}@Beanpublic Queue orderQueue() {return new Queue(RabbitMqConfig.RABBITMQ_ORDER_QUEUE_NAME);}@Beanpublic DirectExchange directExchange() {return new DirectExchange("directExchange");}@Beanpublic TopicExchange topicExchange() {return new TopicExchange("topicExchange");}@Beanpublic FanoutExchange fanoutExchange() {return new FanoutExchange("fanoutExchange");}// 建立Queue与Exchange的绑定关系@Beanpublic Binding bindingExchangeMessage(Queue orderQueue, DirectExchange directExchange) {return BindingBuilder.bind(orderQueue).to(directExchange).with(RabbitMqConfig.RABBITMQ_ORDER_QUEUE_NAME);}@Beanpublic RabbitTemplate rabbitTemplate() {cachingConnectionFactory.setPublisherConfirms(true);cachingConnectionFactory.setPublisherReturns(true);RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);rabbitTemplate.setMandatory(true);rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {if (ack)logger.info("消息发送成功: correlationData:({}),ack:({ack}),cause:({})", correlationData,ack, cause);elselogger.info("消息发送失败: correlationData:({}),ack:({ack}),cause:({})", correlationData,ack, cause);});rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> logger.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange,routingKey, replyCode, replyText, message));return rabbitTemplate;}
}package
生产者
package com.example.demo.mq;import com.example.demo.configure.RabbitMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class OrderMaker {private final static Logger logger = LoggerFactory.getLogger(OrderMaker.class);@Autowiredprivate RabbitTemplate rabbitTemplate;public void send(String content){this.rabbitTemplate.convertAndSend(RabbitMqConfig.RABBITMQ_ORDER_QUEUE_NAME,content);}
}package
测试入口
package com.example.demo.controller;import com.example.demo.mq.OrderMaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Demo {@Autowiredprivate OrderMaker orderMaker;@RequestMapping(value = "/testMq",method = RequestMethod.GET,produces = MediaType.ALL_VALUE)public String testMq(String msg){orderMaker.send(msg);System.out.println(msg);return "Successfully.";}
}package
使用postman测试http://127.0.0.1:8080/testMq?msg=hahaha,this is a test
在http://localhost:15672中
OrderQueue1队列有两条消息
查看消息
消费者
package com.example.demo.mq;import com.example.demo.configure.RabbitMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@RabbitListener(queues = RabbitMqConfig.RABBITMQ_ORDER_QUEUE_NAME)
public class OrderListener {private final static Logger logger = LoggerFactory.getLogger(OrderListener.class);@RabbitHandlerpublic void process(String orderMsg){logger.info("订单消费者收到消息:" + orderMsg);}
}package
重新启动
log输出
2019-11-13 14:36:51.500 [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] INFO com.example.demo.mq.OrderListener - 订单消费者收到消息:hahaha,this is a test
2019-11-13 14:36:51.516 [AMQP Connection 127.0.0.1:5672] INFO com.example.demo.configure.RabbitMqConfig - 消息发送成功: correlationData:(null),ack:({ack}),cause:(true)
这样就实现了简单的队列,生产者将消息发送到队列,消费者从队列中获取消息
P:消息的生产者
C:消息的消费者
红色:队列
RabbitMQ使用及与spring boot整合相关推荐
- spring boot整合RabbitMQ —— 十分钟急速上手
安装运行rabbitmq 1.docker安装rabbitmq: docker run -it --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq ...
- Spring Boot 整合 RabbitMQ 实现
Spring Boot 整合 RabbitMQ 实现 1. 开发环境搭建 1.1 pom配置 1.2 application.yml配置 2. 消费者模块(springboot-rabbitmq-co ...
- Spring Boot: Spring Boot 整合 RabbitMQ
前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用来做应 ...
- 【消息中间件】Spring Boot整合RabbitMQ
简介 在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ 尤其是在spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate ...
- Spring Boot 整合——Spring Boot整合kafka整合
Spring Boot 整合之前的内容 项目名称 描述 地址 base-data-mybatis 整合mybatis-plus(实际上官方教程已经很多,只做了自定义插件) 未完成 base-jpa J ...
- Spring Boot整合elasticsearch实现全文检索
文章目录 1.引入 1.1 Luence 1.2 Solr 1.3 ElasticSearch 2. ElasticSearch安装 2.1 云服务器安装 2.1.1. docker安装 2.1.2 ...
- spring boot整合spring security笔记
最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...
- Spring Boot 教程(三): Spring Boot 整合Mybatis
教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...
- 五、spring boot整合mybatis-plus
spring boot整合mybatis-plus 简介 mybatis 增强工具包,简化 CRUD 操作. 文档 http://mp.baomidou.com http://mybatis.plus ...
最新文章
- 怎么关闭wordpress评论
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- python工作太难找-Python工作难找吗?
- 剑指offer 二叉树的镜像
- 【工业级串口服务器E810-DTU】RS485/RS232转以太网,双向透明传输
- sun键盘没有stop键_请教Sun键盘
- c语言中怎样变大输出的字符串,C语言。要输入一个超大数比如 111111111111111111111111111111111111111 怎样用字符串数组求各位的和呢?...
- php自动关闭页面代码,HTML_下载完成后页面不自动关闭的方法,下载完成后页面不自动关闭的 - phpStudy...
- wxt_hillwill的知识脉络
- Linux学习笔记---网线直连电脑和开发板使用nfs下载文件
- Android开发之 Android 的基本组件的概述
- 学习之法 —— 套路
- TF2.0—tf.keras.losses.BinaryCrossentropy
- 解密Twitch:一家游戏直播网站缘何价值10亿刀?
- 解决python中文乱码问题
- 《Spring实战第四版》随书源码导入Eclipse
- 常用零部件表面粗糙度标注及表面处理技术
- vue之ele组件中的select下拉 绑定对象值
- 使用微信公众号openid获取用户信息判断是否关注公众号
- 姐养狗2号前来面基!祝大家新年快乐!