rabbitMQ基于spring-rabbitnq
一、什么是MQ
MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ等等。
二、MQ特点
MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取或者订阅队列中的消息。MQ和JMS类似,但不同的是JMS是SUN JAVA消息中间件服务的一个标准和API定义,而MQ则是遵循了AMQP协议的具体实现和产品。
三、使用场景
在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了服务器的请求响应时间,从而提高了系统的吞吐量。
四、相关基础概念
Exchange:交换机,决定了消息路由规则;
Queue:消息队列;
Channel:进行消息读写的通道;
Bind:绑定了Queue和Exchange,意即为符合什么样路由规则的消息,将会放置入哪一个消息队列;
五、本人是用的是spring-rabbitmq实现的相关配置如下:
1、生产者配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--<util:properties id="rabbit" location="classpath*:rabbitmq.properties"/>--> <!-- 配置ConnectionFactory -->
<rabbit:connection-factory id="connectionFactory"
host="#{config['rabbit.host']}"
username="#{config['rabbit.user']}"
password="#{config['rabbit.pwd']}"
port="#{config['rabbit.port']}"
virtual-host="#{config['rabbit.virtualHost']}"/>
<!-- 等同new一个RabbitAdmin -->
<rabbit:admin connection-factory="connectionFactory"/>
<!-- 声明一个队列 -->
<rabbit:queue id="handle" name="#{config['rabbit.queue.handle.name']}"/> <!-- 声明一个topic类型的exchange,并把上面声明的队列绑定在上面,routingKey="foo.*" -->
<rabbit:topic-exchange name="ex_sshmsgcentor">
<rabbit:bindings>
<rabbit:binding queue="handle" pattern="#{config['rabbit.queue.handle.name']}"/>
<!-- 这里还可以继续绑定其他队列 -->
</rabbit:bindings>
</rabbit:topic-exchange> <!-- 声明一个rabbitTemplate,指定连接信息,发送消息到myExchange上,routingKey在程序中设置,此处的配置在程序中可以用set修改 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="ex_sshmsgcentor"/> </beans>
# rabbitMQ 相关配置
rabbit.host=127.0.0.1
rabbit.user=test
rabbit.pwd=test
rabbit.port=
rabbit.virtualHost=vir_test
#需要发送到mqtt的消息,通配符
rabbit.queue.handle.name=test
调用方法:
@Resource
private AmqpTemplate amqpTemplate; public void pushRabbitMQ{
logger.info("push rabbitMq message:[" + messageCentent + "]");
MessageProperties messageProperties = new MessageProperties();
Message message = new Message(messageCentent.getBytes(), messageProperties);
logger.info("message:[" + messageCentent + "]");
logger.info("PATTERN:" + ConfigUtils.RABBIT_QUEUE_HANDLE_NAME);
amqpTemplate.send(EXCHANGE_NAME, ConfigUtils.RABBIT_QUEUE_HANDLE_NAME, message);
}
ps:本项目生产者和消费者是不同的项目
2、消费者配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--<util:properties id="rabbit" location="classpath*:rabbitmq.properties"/>--> <!-- 配置ConnectionFactory -->
<rabbit:connection-factory id="connectionFactory"
host="#{config['rabbit.host']}"
username="#{config['rabbit.user']}"
password="#{config['rabbit.pwd']}"
port="#{config['rabbit.port']}"
virtual-host="#{config['rabbit.virtualHost']}"/>
<!-- 等同new一个RabbitAdmin -->
<rabbit:admin connection-factory="connectionFactory"/>
<!-- 声明一个队列 -->
<rabbit:queue id="handle" name="#{config['rabbit.queue.handle.name']}"/> <!-- 声明一个topic类型的exchange,并把上面声明的队列绑定在上面,routingKey="foo.*" -->
<rabbit:topic-exchange name="ex_sshmsgcentor">
<rabbit:bindings>
<rabbit:binding queue="handle" pattern="#{config['rabbit.queue.handle.name']}"/>
<!-- 这里还可以继续绑定其他队列 -->
</rabbit:bindings>
</rabbit:topic-exchange> <!-- 声明一个rabbitTemplate,指定连接信息,发送消息到myExchange上,routingKey在程序中设置,此处的配置在程序中可以用set修改 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="ex_sshmsgcentor"/> <!-- 配置监听容器,指定消息处理类,处理方法,还可以配置自动确认等-->
<rabbit:listener-container connection-factory="connectionFactory"
acknowledge="manual" max-concurrency="#{config['rabbit.consumer.max.concurrency']}"
concurrency="#{config['rabbit.consumer.concurrency']}" prefetch="#{config['rabbit.consumer.prefetch']}">
<rabbit:listener ref="messageReceiver" queue-names="#{config['rabbit.queue.handle.name']}"/>
<!-- 可以继续注册监听 -->
</rabbit:listener-container>
<bean id="messageReceiver" class="com.ssh.servicenode.pushtaskserver.consumer.MessageConsumer"/> </beans>
# rabbitMQ 相关配置
rabbit.host=192.168.1.211
rabbit.user=devmsgcentor
rabbit.pwd=devmsgcentor
rabbit.port=
rabbit.virtualHost=vir_devmsgcentor
#需要发送到mqtt的消息,通配符
rabbit.queue.handle.name=queue_devmsgcentor
#监听者并发数
rabbit.consumer.concurrency =
#监听者最大并发数
rabbit.consumer.max.concurrency =
#每个监听者从队列中预取数
rabbit.consumer.prefetch =
辅助网页
rabbitMQ基于spring-rabbitnq的更多相关文章
- 基于Spring Cloud和Netflix OSS构建微服务,Part 2
在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- 基于 Spring Cloud 完整的微服务架构实战
本项目是一个基于 Spring Boot.Spring Cloud.Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目. @作者:Sheldon地址:ht ...
- 干货|基于 Spring Cloud 的微服务落地
转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...
- 基于Spring Cloud的微服务落地
微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...
- 关于RabbitMQ以及RabbitMQ和Spring的整合
转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 基于Spring Boot和Spring Cloud实现微服务架构
官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...
- Weshop基于Spring Cloud开发的小程序商城系统
WESHOP | 基于微服务的小程序商城系统 Weshop是基于Spring Cloud(Greenwich)开发的小程序商城系统,提供整套公共微服务服务模块,包含用户中心.商品中心.订单中心.营销中 ...
随机推荐
- React Hooks: use modal
useModal: export const useModal = (initTitle: string, initContent: string | React.ReactElement) => ...
- Java标识符中常见的命名规则
标识符:就是给类,接口,方法,变量等起名字.组成规则:A:英文字母大小写B:数字字符C:$和_注意事项:A:不能以数字开头B:不能使Java中的关键字C:Java语言严格区分大小写常见的命名规则:见名 ...
- centos分配IP脚本--写的第一个shell脚本
IDC小菜鸟一枚,非科班出身.常常有客户的centos服务器需要分配15个IP甚至30个IP.每次需要手动分配十分麻烦,于是花了一天时间学了shell脚本,写了这个脚本. #!/bin/bash re ...
- 如何在Vue项目中优雅的使用sass
开始之前,请先确保有一个基于webpack模板的项目(vue-cli脚手架一键安装~) 1.打开项目终端,安装sass的依赖包 npm install --save-dev sass-loader / ...
- 我想solo自己一个人!
区域赛之后你就该走了,现在你告诉我,没精力不打了,我真谢谢你! 今年就TM的没有一点舒心的地方! 父母分居, 队友出走, 队伍解散, 白天家里两个外甥很吵, 鼻窦炎复发, 喜欢的妹子也追不到, 整夜失 ...
- 数学--数论--HDU2136 Largest prime factor 线性筛法变形
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- Python爬虫---爬取抖音短视频
目录 前言 抖音爬虫制作 选定网页 分析网页 提取id构造网址 拼接数据包链接 获取视频地址 下载视频 全部代码 实现结果 待解决的问题 前言 最近一直想要写一个抖音爬虫来批量下载抖音的短视频,但是经 ...
- Android原生多语言切换方案,兼容Android10
前言 一个应用若需要国际化,至少需要支持中文和英语这两种语言,而同时随着谷歌的系统的更新,安卓系统可以设置当前语言的首选语言.因此,本文立足于此,多语言的切换方案为:App固定的文字内容,跟随系统,中 ...
- springboot之异常处理
我在使用springboot的时候,运行主类结果报错 : 异常错误:java.sql.SQLException: The server time zone value '?й???????' is u ...
- MATLAB矩阵的表示
矩阵是matlab中最基本的数据对象. l 矩阵的建立 l 冒号表达式 l 结构矩阵和单元矩阵 1.矩阵的建立 (1)利用直接输入法建立矩阵:将矩阵的元素用中括号括起来,按矩阵行的顺序输入各元素 ...