一、什么是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. tensorflow版线性回归

    import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf def linearregression(): X ...

  2. JWT验证机制【Python版Flask或自己写的后端可以用】【刘新宇】

    JWT Json Web Token(JWT) JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用JWT在两个组织之间传递安全可靠的信息. 官方定义:JSON Web T ...

  3. 关于phpstorm、idea、gogland等等ide全家桶设置

    2017-08-29 16:30:55 Preferences => IDE Settings => Editor => Code Completion => Autopopu ...

  4. swoole--服务平滑重启

    参考来源:https://wiki.swoole.com/wiki/page/p-server/reload.html shell代码: echo "loading..." pid ...

  5. linux下的.ssh文件夹路径等

    1.linux下的.ssh文件夹在~下,直接cd ~/.ssh即可 2.tp经过gd类处理过的水印图片格式为png 3.前端扒下别人家的网站如果自己本地打开有出现相同的代码段则有可能是js动态添加的, ...

  6. 理解分布式一致性:Raft协议

    理解分布式一致性:Raft协议 什么是分布式一致性 Leader选举 日志复制流程 term选举周期 timeout 选举和选举timeout 选举分裂 日志复制和心跳timeout 在分布式系统中, ...

  7. 学习 .net core 3----蒋金楠 笔记 构建 Asp.net core Web应用

    前言:准备系统的学习一下.net core 所以购买了 蒋金楠的 ASP.NET CORE 3 书籍,为了加深印象 特此笔记,会持续更新到学习完为止 使用  命令行   dotnet  new  co ...

  8. MySQL5.7中InnoDB不可不知的新特性

    讲师介绍  赖铮 Oracle InnoDB团队 Principle Software Developer 曾任达梦.Teradata高级工程师,主要负责研发数据库执行引擎和存储引擎,十年以商数据库内 ...

  9. 剑指offer---05---用栈实现队列

    题意 给了两个栈去实现队列   分析 两个栈如下情况       1       2 4    3 这个时候就不能够把4插入到第二个弹出栈了否则弹出顺序出错. 所以这个时候就应该等第二个栈空了的时候再 ...

  10. 编写C#程序的IDE

    编写C#程序,在Windows平台下,除了昂贵的Visual Studio.NET这个正宗的工具外,你还了解哪些? 听说有个Eclipse,IBM投钱开发的开源工具,有人也做了个for .NET的pl ...