跳至主要内容

RabbitMQ Learning (1): Basic

This blog is the learning note of RabbitMQ, would like to share here and learn with all of we.

Basic Concepts

Channel

Channel is used by rabbit client to communicate with server. The channel is backed by TCP, which can increase the reliability without extra effort. Besides that, this abstraction layer, channel layer, over TCP connection, can be used to reuse same connection and provide privacy at the same time.

Queue Config

A message queue has following common settings:
  • name: used by client to refer server queue
  • exclusive: limit only one consumer for this queue
  • auto-delete: last consumer remove subscription, the queue is deleted

Queue Creation

Queue is located in the server of rabbit, what we have is just a stub. If we just restart our clients, queue will not be affected.
  • consumer creation: we can endure the miss of producer’s message
  • producer & consumer: we can’t ignore this

Queue Usage

  • message is waiting here
  • load balance

Consume vs Get

There exists two method to get message from queues: consume and get. The first is meant to subscribe the queue and get message again and again. On the other hand, get will just get one message and all is done.

Queue with No Consumer

If a queue has no consumer, the message will wait in the queue util a consumer register and receive it.
we may use the following command to verify it after sending the message:
rabbitmqctl list_queues

Queue With Multiple Consumer

If a queue has multiple consumer, rabbit will deliver the message following the two rules:
  • Use Round-Robin algorithm
  • Every message will only send to one consumer

Consumer ACK

Without ACK from consumer during some period, rabbit (queue) will send message to another consumer so that if our machine is down, another host can help.
If a specific host haven’t respond with ACK, rabbit will not send new message to him.
This behavior may cause the duplicate consumption of a message, that host consume the message but fail to respond the ACK back to the queue.

Exchange Config

Exchange is the core of message passing, which routing our message using routing key to different queues. This add another abstraction to message system, which add more functionality.
If we just have queues to communicate, we can only make direct communication between queues. By adding this layer, we can broadcast, we can subscribe and consume, we can also simulate direct communication.
Exchange has following common config:
  • name: used to refer to some exchange
  • durable: used to indicate whether this exchange will persist the message in disk
  • type: this is very important, and we describe it in following section.

Exchange Type

The following is the four type of exchanges in RabbitMQ:
  • headers: match headers of msg rather than message’s routing key which may decrease the performance of message queue;
  • topic: enable message from different sources go to same queue;
  • fanout: is used to broadcast to all queues; can be used to extend some functionality by adding new queue to same exchange;
  • direct: exchange name is “”; routing key is the queue name;

Binding

As long as we have exchange set, we need to define the relationship between queue and exchange, which is binding.
Binding is used by queue to listen/get message from exchange, i.e. it is often used by consumer.
bind(queueName, exchange, routingRule)

|   topic exchange  |
| log.      alert.  |
| critical  critical|
   /       |         \
  /        |          \
 log.*   *.critical    alert.*
 /         |             \
q1        q2             q3

Durability

In order to keep the message in case of server crash, i.e. make the message durable, we need to make the following config:
  • make exchange durable
  • make queue durable
  • message deliver in persistent mode
Process:
  • set message delivery mode as persistent;
  • send this message to a durable exchange: which use write ahead log to ensure persistence;
  • deliver to a durable queue;

Server Management

Node Basics

The following is some basics:
  • Definition: the instance of a Erlang VM
  • Different Erlang application can run on same node
  • Different nodes can invoke method like at same node

Log & Cmd & Config

Log file location:
/var/log/rabbitmq/rabbit@[hostname].log
Command line to start/stop rabbitmq server and the whole Erlang server:
rabbitmqctl stop
rabbitmqctl stop_app
Rabbitmq config file location:
/etc/rabbitmq/rabbitmq.config

vhost and Access Control

vhost

vhost can be used to separate different users of server: vhost is a mini RabbitMQ server, can add access control.

Permissions

rabbitmqctl add_user xxx pw
rabbitmqctl set_permissions -p host username ".*" ".*" ".*"
Written with StackEdit.

评论