rabbitmq series (a) acquaintance rabbitmq

Why use messaging middleware

Example: If we have developed a commodity buying website. The purpose of this website is to be buying commodities at some point in time, but require users to register, it will also give the user the phone and mailbox to send a verification code at the time of registration in order to complete registration information. Traditional practices should be like this. In two ways, namely to enable the two threads in parallel, when the user submits the information, respectively, to send messages and text messages. In this way it is significantly faster than a serial manner. When we added to the message queue, the processing of FIG follows:

Serial Parallel .jpg

After the addition of messaging middleware, we just need to keep registration information library, add a message to the message queue gets the job done. Then mail services and messaging services are going to consume news.

Message queue .png

When the user registration is complete, it is time to buy goods, we all go to buy an item when a large amount of this. Message Queuing queue length may be provided to ensure the stability of the system. When the queue is full, when the user requests are no longer processed.

So we can summarize the characteristics of the message queue:

  1. Asynchronous processing - after user registration information submitted directly to return a response. Then mail service and SMS service to listen to the message queue has to take the initiative process
  2. Application of Decoupling - user registration process is divided into three services, registration services, mail services, messaging services without disturbing each other.
  3. Clipping flow - control of user requests, the system ran to prevent collapse.

Common messaging middleware Introduction

ActiveMQ : the Apache produced, a strong ability to open source message bus, fully supports the jms messaging middleware specification. api rich, widely used in small and medium enterprises in traditional industries. Disadvantages: poor service performance and data storage performance.

Kafka used to live : the Apache top-level project, the pursuit of high throughput. It is for the purpose of beginning a log collection and transmission. Does not support transactions, the message is repeated, missing, error no strict request. Traffic data for a large amount of data collected by Internet services.

RocketMQ:阿里开源中间件,目前已孵化成apache顶级项目,纯java开发,思路起源于kafka,对消息的可靠性传输和事务性做了优化。特点:高吞吐量、高可用。适合大规模分布式系统应用。目前在阿里集团被广泛使用,用于交易、充值,流计算,日志处理,消息推送等。现在推出了商业版,有些功能对外不开发。

RabbitMQ:是一个开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,RabbitMQ是使用ErLang语言来编写的,并且基于AMQP协议。erlang语言开发,性能较好,高并发。社区活跃度高,网上资料比较多。

什么是AMQP协议

AMQP(Advanced Message Queuing Protocol,高级消息队列协议)是一个进程间传递异步消息网络协议

涉及到的具体概念:

  • server - 又称broker,接收客户端的链接,实现amqp实体服务。
  • Connection - 链接,应用程序跟broker的网络链接。
  • channel - 网络信道,几乎所有的操作都是在channel中进行,数据的流转都要在channel上进行。channel是进行消息读写的通道。客户端可以建立多个channel,每个channel代表一个会话任务。
  • message - 消息,服务器与应用程序之间传送的数据。由properties和body组成。properties可以对消息进行修饰,比如消息的升级,延迟等高级特性。body就是消息体的内容。
  • virtual host - 虚拟主机,用于进行逻辑隔离,最上层的消息路由,一个虚拟地址里面可以有多个交换机。exchange和消息队列message quene。
  • exchange - 交换机,接收消息,根据路由器转发消息到绑定的队列。
  • binding - 绑定,交换机和队列之间的虚拟链接,绑定中可以包含routing key。
  • routing key - 一个路由规则,虚拟机可以用它来确定如何路由一个特定消息。
  • quene - 消息队列,保存消息并将它们转发给消费者。

详细了解AMQP协议可参考这篇文章--------详解AMQP协议

交换机简单介绍

exchange:接收消息,并根据路由键转发消息所绑定的队列。

交换机的属性:

name:交换机的名称

type:交换机的类型direct,topic,fanout,headers

durability:是否需要持久化,true为持久化。

auto delete:当最后一个绑定到exchange上的队列删除后,自动删除该exchange。

internal:当前exchange是否用于rabbitMQ内部使用,默认为false。

arguments:可扩展参数。用户自自定义的交换机时,用到的参数。

交换机常用的类型为direct,topic,fanout。headers不常用。

direct(直连交换机):

  1. 所有发送到directExchange的消息被转发到RouteKey中指定的Queue

  2. rabbitmq有一个自带的exchange叫default exchange,这个交换机是direct类型的。rabbitmq会让路由键跟队列名相等进行绑定。

topic(主题交换机):

  1. 所有发送到topic exchange的消息被转发到所有关心RouteKey的Queue上

  2. Exchange将RouteKey和某些队列进行模糊匹配,此时队列需要绑定一个Topic

模糊匹配可以使用通配符:

#可以匹配一个或多个词

*只能匹配一个词

比如:"log.#"可以匹配到“log.info.oa”。“log.*”只会匹配到“log.error”

Fanout(扇型交换机):

  1. 不处理路由键,只需要简单的将队列绑定到交换机上。

  2. 发送到交换机的消息都会被转发到与该交换机绑定的所有队列上

  3. fanout switch forwards messages is the fastest.

If the article helpful, please remember to focus on points like yo ~
Welcome to my public concern number: Byte legend, technical articles for daily push them to learn.

Guess you like

Origin www.cnblogs.com/zhixie/p/12185574.html