一、什么是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. 详细分析Redis的持久化操作——RDB与AOF

    一.前言   由于疫情的原因,学校还没有开学,这也就让我有了很多的时间.趁着时间比较多,我终于可以开始学习那些之前一直想学的技术了.最近这几天开始学习Redis,买了本<Redis实战>, ...

  2. Java 解析 xml 常见的4中方式:DOM SAX JDOM DOM4J

    Java 四种解析 XML 的特点 1.DOM 解析: 形成了树结构,有助于更好的理解.掌握,且代码容易编写. 解析过程中,树结构保存在内存中,方便修改. 2.SAX 解析: 采用事件驱动模式,对内存 ...

  3. Idea上tomcat部署细节

    ​ 一.On Update action: (1)Update resources:更新项目变更的.jsp,.xml文件等资源文件,而不会更新源码文件:(仅修改项目的JS文件.JSP文件.CSS文件推 ...

  4. Task启动方式及源码探究

    启动Task有几种方式: 1.Task.Run() 2.new TaskFactory.StartNew() 3.var t=new Task();  t.start(); 平时用的最多是第一和第二种 ...

  5. 5、flink常见函数使用及自定义转换函数

    代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 一.flink编程方法 获取执行环境(execution environment) 加载/创建初始数据集 对 ...

  6. 小程序里button边框有黑线解决办法(自定义button样式)

    .go_to_user::after{ border:1px solid transparent; } button的class为go_to_user button{ padding:; box-si ...

  7. Maven Wrapper简介

    文章目录 简介 Maven Wrapper的结构 下载Maven Wrapper 使用 Maven Wrapper简介 简介 开发java项目少不了要用到maven或者gradle,对比gradle而 ...

  8. 【Linux网络基础】TCP/IP 协议簇(各个常见协议介绍)

    一.应用层协议 1. FTP   协议所在层次:应用层协议 名称:FTP协议 协议端口:20,21 协议说明: FTP(File Transfer Protocol,文件传输协议)是TCP/IP协议组 ...

  9. 【Linux常见命令】mv命令

    mv - move (rename) files mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法: mv [OPTION]... [-T] SOURCE DEST mv [OPTIO ...

  10. Java反射详细介绍

    反射 目录介绍 1.反射概述 1.1 反射概述 1.2 获取class文件对象的三种方式 1.3 反射常用的方法介绍 1.4 反射的定义 1.5 反射的组成 1.6 反射的作用有哪些 2.反射的相关使 ...