RABBITMQ Study and use

Source: Internet
Author: User

RABBITMQ Learn and use RABBITMQ introduction

The MQ full name Message queue, RABBITMQ is based on the AMQP (Advanced Message Queuing Protocol) implementation. Message Queuing is often used to communicate with each other to resolve synchronization problems. MQ is a typical producer-consumer model , and the three most commonly used modes of RABBITMQ are point-to-point mode, publish-subscribe mode, and broadcast mode.

RABBITMQ Application Scenarios
1)信息的发送者和接收者如何维持这个连接,如果一方的连接中断,这期间的数据如何方式丢失?(宕机处理)2)如何降低发送者和接收者的耦合度?(解耦)3)如何让Priority高的接收者先接到数据?4)如何做到load balance?有效均衡接收者的负载?(负载均衡)5)如何有效的将数据发送到相关的接收者?也就是说将接收者subscribe 不同的数据,如何做有效的filter。(路由)6)如何做到可扩展,甚至将这个通信模块发到cluster上?7)如何保证接收者接收到了完整,正确的数据?(稳定性)

Summary: The main two functional decoupling and asynchronous processing of message middleware

RABBITMQ Installation Introduction
1、rabbitMQ是由erlang语言开发的,所以必须先有安装erlang,类似java安装2、rabbitMQ是C/S模式的,所以安装rabbitMQ服务器,傻瓜安装默认端口56723、推荐最好也安装上rabbitMQ 的web管理界面(默认端口15672) 当然使用rabbitctl命令也可以

Official Reference Link: http://www.rabbitmq.com/documentation.html

RABBITMQ Basic concepts and characteristics RABBITMQ characteristics

Feature Highlights
1, reliabliity (reliability):
RabbitMQ offers a variety of features to let's trade off performance with reliability, including persistence, delivery a Cknowledgements, Publisher confirms, and high availability.
2. Flexible Routing (Flexible routing mechanism):
Messages is routed through exchanges before arriving at queues. RabbitMQ features several built-in exchange types for typical routing logic. For more complex routing you can bind exchanges together or even write your own exchange type as a plugin.
3, Clustering (cluster function):
Several RabbitMQ servers on a local network can is clustered together, forming a single logical broker.
4. Highly Available Queues (highly available queue):
Queues can mirrored across several machines in a cluster, ensuring that even in the event of hardware failure your mess Ages is safe.
5, Multi-Protocol (Multi-protocol support):
RabbitMQ supports messaging over a variety of messaging protocols.
6, many clients (multi-client support):
There is RabbitMQ clients for almost all language you can think of.
7. Management UI (Graphical management interface):
RabbitMQ ships with a easy-to use management UI This allows you to monitor and control every aspect of your message broke R.
etc...

Professional terminology

Producer: Application that sends the messages message producer

Consumer: Application that receives the Messages message consumer

Queue: Buffer that store messages the container for cached messages

Message: Information that's sent from the producer to a consumer through RabbitMQ. (The message data that the consumer really needs)

Connection: A connection is a TCP connection between your application and the RabbitMQ broker. (A TCP connection, relatively consumes resources)

Channel: A channel is a virtual connection inside a connection. When you were publishing or consuming messages or subscribing to a queue was it all done over a channel. (A pipe connection, which is a connection within a TCP connection, is relatively resource-efficient)

Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. In order to receive messages, a queue needs to is bound to at least one exchange. (Message routing, a producer sends a message that is not sent directly to the queue but first to a specified route, and then routed to the specified queue by a queue that is bound by the route key.) )

Binding: A binding is a link between a queue and an exchange.

Routing Key: The routing key is a key, the exchange looks at the decide how to route, the message to queues. The routing key is a like address for the message.

AMQP: AMQP (Advanced Message Queuing Protocol) are the Protocol used by RabbitMQ for messaging. (Advanced Message Queuing protocol, RABBITMQ is implemented based on this Protocol)

Virtual Host: A Virtual host provide a to segregate applications using the same RabbitMQ instance. Different users can has Different access privileges to Different vhost and queues and exchanges can is created so they on LY exists in one vhost. (The main function is to isolate)

Users: It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance. Users can also is assigned permissions to specific virtual hosts. (RABBITMQ service is based on C/s mode, usually the connection requires authentication)

RABBITMQ Development Use development steps

1. Get a connection to the RABBITMQ server

 ConnectionFactory factory = new ConnectionFactory();//创建连接工厂对象 factory.setHost(hostName);//指定主机名 factory.setPort(portNumber);//指定端口号 factory.setVirtualHost(virtualHost);//指定RabbitMQ服务器的虚拟主机 factory.setUsername(username);//指定连接用户名 factory.setPassword(password);//指定连接用户密码 Connection conn = factory.newConnection();//创建连接

Note: Since this conneciton connection is a relatively resource-intensive TCP connection, we typically create a pipe connection inside it to operate the RABBITMQ.

//open a channel Channel channel = conn.createChannel();

2. Declaring routing and using route types and declaring queue names

3. Send message publish or accept message consume

RabbitMQ Consumer two ways to get messages (poll, subscribe)
1, Subscribe subscription method
The code is as follows:

Consumer consumer = new DefaultConsumer(channel) {        public void handleDelivery(String consumerTag, com.rabbitmq.client.Envelope envelope,                com.rabbitmq.client.AMQP.BasicProperties properties, byte[] body) throws java.io.IOException {            //使用以上的几个入参来处理信息            // doSomething ...        };    };

2, Poll API mode (polling mode)

channel.basicGet(String queue, boolean autoAck);

Note: The use of polling is less efficient than the recommended way to subscribe

RabbitMQ Start:http://www.rabbitmq.com/getstarted.html

Note in real-world development, it is common to use RABBITMQ with spring to make development easier.

RABBITMQ three ways of routing

One, direct Exchange (straight route)

Any messages sent to direct Exchange will be forwarded to the queue specified in Routekey.

1.一般情况可以使用rabbitMQ自带的Exchange:"(该Exchange的名字为空字符串,下文称其为default Exchange)。2.这种模式下不需要将Exchange进行任何绑定(binding)操作3.消息传递时需要一个“RouteKey”,可以简单的理解为要发送到的队列名字。4.如果vhost中不存在RouteKey中指定的队列名,则该消息会被抛弃。



ii. fanout Exchange (broadcast routing)

Any messages sent to Fanout Exchange will be forwarded to all the queue with that Exchange binding (binding).

1.可以理解为路由表的模式2.这种模式不需要RouteKey3.这种模式需要提前将Exchange与Queue进行绑定,一个Exchange可以绑定多个Queue,一个Queue可以同多个Exchange进行绑定。4.如果接受到消息的Exchange没有与任何Queue绑定,则消息会被抛弃



iii. Topic Exchange (topic subscription mode routing)

Any messages sent to topic Exchange will be forwarded to all queues that are concerned with the specified topic in Routekey

1.这种模式较为复杂,简单来说,就是每个队列都有其关心的主题,所有的消息都带有一个“标题”(RouteKey),Exchange会将消息转发到所有关注主题能与RouteKey模糊匹配的队列。2.这种模式需要RouteKey,也许要提前绑定Exchange与Queue。3.在进行绑定时,要提供一个该队列关心的主题,如“#.log.#”表示该队列关心所有涉及log的消息(一个RouteKey为”MQ.log.error”的消息会被转发到该队列)。4.“#”表示0个或若干个关键字,“*”表示一个关键字。如“log.*”能与“log.warn”匹配,无法与“log.warn.timeout”匹配;但是“log.#”能与上述两者匹配。5.同样,如果Exchange没有发现能够与RouteKey匹配的Queue,则会抛弃此消息。

Reference

1, Https://en.wikipedia.org/wiki/RabbitMQ
2, http://www.rabbitmq.com/
3, https://www.cloudamqp.com/blog/2015-05-18-part1-rabbitmq-for-beginners-what-is-rabbitmq.html
4, http://www.gaort.com/index.php/archives/366
5, http://blog.csdn.net/yangbutao/article/details/10395599
6, https://www.ibm.com/developerworks/cn/opensource/os-cn-rabbit-mq/
7, http://www.ibm.com/developerworks/cn/websphere/library/techarticles/0805_wenhongt/

RABBITMQ Study and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.