跳至主要内容

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.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (