一、什么是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的更多相关文章

  1. 基于Spring Cloud和Netflix OSS构建微服务,Part 2

    在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...

  2. 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践

    由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...

  3. 基于 Spring Cloud 完整的微服务架构实战

    本项目是一个基于 Spring Boot.Spring Cloud.Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目. @作者:Sheldon地址:ht ...

  4. 干货|基于 Spring Cloud 的微服务落地

    转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...

  5. 基于Spring Cloud的微服务落地

    微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...

  6. 关于RabbitMQ以及RabbitMQ和Spring的整合

    转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...

  7. 基于Spring Boot和Spring Cloud实现微服务架构学习

    转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...

  8. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

  9. 基于Spring Boot和Spring Cloud实现微服务架构

    官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...

  10. Weshop基于Spring Cloud开发的小程序商城系统

    WESHOP | 基于微服务的小程序商城系统 Weshop是基于Spring Cloud(Greenwich)开发的小程序商城系统,提供整套公共微服务服务模块,包含用户中心.商品中心.订单中心.营销中 ...

随机推荐

  1. react: typescript integrate withRouter

    define interface: export interface INav { nav: string } export interface IModuleItem { state?: strin ...

  2. word2sequence 把字符串转换数字编码

    地址:http://ai.stanford.edu/~amaas/data/sentiment/,这是一份包含了5万条流行电影的评论数据,其中训练集25000条,测试集25000条. 1.准备数据 d ...

  3. php环境兼容性问题---压缩格式及其配置简介

    php环境兼容性问题-- 内容编码错误 无法显示您尝试查看的页面,因为它使用了无效或者不支持的压缩格式. 请联系网站的所有者以告知此问题. 以前也遇到过同样的问题,记得是PHP代码ob_start(' ...

  4. Python与Excel交互——Xlwings实战

    这一期直接来实战. 比如说,我们在一个快递网站上爬取了几个快递的轨迹信息,我们需要将数据保存下来,一个常规做法是把数据保存在数据库里(Mysql,MongoDB,Redis),另一个是用Excel的形 ...

  5. 《Arduino实战》——3.4 小结

    本节书摘来异步社区<Arduino实战>一书中的第3章,第3.4节,作者:[美]Martin Evans ,Joshua Noble ,Jordan Hochenbaum,更多章节内容可以 ...

  6. 《Android的设计与实现:卷I》——第1章 1.2.2动态视角的体系结构

    1.2.2 动态视角的体系结构静态的体系结构是从横向分层的角度诠释Android是什么.如果静态的体系结构不足以让读者理解Android的运行机制,我们可以看看Google工程师Sans Serif是 ...

  7. 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)

    ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...

  8. Android APK 重签名

    对APK 进行在线 加固后,Apk体积一般会变大,而且Apk会无法直接安装,因为缺少了你的签名.是的,你需要对这个Apk进行重签名. 如何重签名 重签名的方法,一般来说,有两种,第一种是用JDK自带的 ...

  9. mysql查询表内所有字段名和备注

    select distinct column_name as 字段名,column_comment as 字段备注 from information_schema.columns where tabl ...

  10. 【MIT6.828】centos7下使用Qemu搭建xv6运行环境

    title:[MIT6.828]centos7下使用Qemu搭建xv6运行环境 date: "2020-05-05" [MIT6.828]centos7下搭建xv6运行环境 1. ...