RabbitMQ Concepts and Java examples

Source: Internet
Author: User
Tags message queue rabbitmq amq

RABBITMQ Introduction
Currently RABBITMQ is an implementation of the AMQP 0-9-1 (Advanced Message Queuing Protocol), written in Erlang, leveraging the distributed nature of Erlang.
Concept Introduction:
Broker: The Message Queuing server entity is simply the case.
Exchange: A message switch that specifies what rules the message is routed to and to which queue.
Queue: A message queue carrier in which each message is put into one or more queues.
Binding: Bind, which is the role of binding exchange and queue according to routing rules.
Routing key: The routing keyword, exchange messages are delivered based on this keyword.
Vhost: Virtual host, a broker can open multiple vhost, as a separate user permissions.
Producer: The message producer is the program that delivers the message.
Consumer: The message consumer is the program that receives the message.
Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task.


in the AMQP model, messages are generated in producer, sent to exchange on MQ, and exchange is routed to the appropriate queue based on the configured route, and the queue sends messages to consumer. Messages from queue to consumer have push and pull two ways. The use of Message Queuing is probably as follows:
1. The client connects to the Message Queuing server and opens a channel.
2. The client declares an exchange and sets the related properties.
3. The client declares a queue and sets the related properties.
4. The client uses routing key to establish a binding relationship between Exchange and queue.
5. Clients post messages to exchange.
When Exchange receives a message, it routes messages to one or more queues based on the key of the message and the binding that has been set. There are several types of exchange, which are called direct switches that are delivered entirely according to key, for example, when the routing key is set to "ABC", the client submits a message that only the key "ABC" is set to be posted to the queue.
set type of Exchange Routing message:
name
Default pre-defined Exchange name
Role Description
Direct Exchange
(Empty string) and Amq.direct
According to the routing key specified by the binding, the message that matches the key is sent to the binding queue
Fanout Exchange
Amq.fanout
sends the same message to all the same exchange Bingding Queue
Topic Exchange
Amq.topic
The Key is pattern-matched based on the routing key,exchange specified by the binding posterior to the corresponding queue, when the pattern matches the symbol " # "matches one or more words, and the symbol" * "matches exactly one word.
Headers Exchange
Amq.match (and amq.headers in RabbitMQ)
is similar to direct exchange, where routing key routing is no longer used. Instead, a match is routed to the specified queue using headers (message attributes).

Reference:
Http://www.choudan.net/2013/07/25/OpenStack-RabbitMQ (%e4%b8%80). html
http://stephansun.iteye.com/blog/1452853
http://www.diggerplus.org/archives/3110
http://backend.blog.163.com/blog/static/202294126201322563245975/
http://lynnkong.iteye.com/blog/1699684

Characteristics:
1. Broker Persistence: When Exchange and queue are declared as durable, the configuration of Exchange and queue is saved on the server side of the disk, so that after the service stops rebooting, Exchange and queue and their corresponding binding configurations are not lost;
2. Message persistence: When message deliver mode attribute (message properties) is set to 2 o'clock, each message that is not consumed is saved on disk and can be saved after the service restarts.
Message in File Save reference: http://my.oschina.net/hncscwc/blog/182083
3. CLUSTER:RABBITMQ supports multiple nodes (each nodes is a RABBITMQ instance) to form a cluster, and the effect of accessing any node in cluster is the same. That is, any message can be produced and consumed on any nodes (the message of production or consumption will be relayed between nodes).
4. MIRRORED-QUEUE:RABBITMQ on the basis of cluster, the message that supports the same queue is stored on more than one nodes, so that when some nodes fail, the configuration of message and broker is guaranteed not to be lost.


Installation and Configuration
1. Installing Erlang
sudo apt-get install TK tcl UnixODBC Erlang
sudo vim/etc/profile
Add Export path= $PATH:/usr/lib/erlang/bin/

2. Installing RABBITMQ
sudo apt-get install Rabbitmq-server

sudo vim/etc/profile add export path= $PATH:/usr/lib/rabbitmq/bin
Source/etc/profile

RABBITMQ Basic Configuration (ports, etc.) reference: http://my.oschina.net/hncscwc/blog/302339

3. Users and Permissions
Before the formal application, we first create a vhost in RABBITMQ, add a user, and set the user's permissions. Using the RABBITMQCTL Client tool, create the "/mq_test" Vhost under the root directory:
Rabbitmqctl Add_vhost/mq_test
Create a user name "test", set the password "Test123″:
Rabbitmqctl Add_user Test test123
Set PYH user to/pyhtest this vhost has full privileges:
Rabbitmqctl set_permissions-p/mq_test Test ". *" ". *" ". *"
The following three "*" represents the Pyh user with/pyhtest configuration, write, read all permissions

Reference: http://my.oschina.net/hncscwc/blog/262246

4. Configure the Open Web Management plug-in
Cat <<EOF>>/etc/rabbitmq/enabled_plugins
[Rabbitmq_management].
Eof
You can view the operation through http://localhost:15672/

5. Start
Run Rabbitmq-server with root privileges or use/etc/init.d/rabbitmq-server start|restart|stop

6. Start multiple nodes on a single machine (simulate the cluster)
Vim start_rabbitmq_cluster.sh

#!/bin/bash

if [[$#! = 1]]
Then
echo "Usage: $ process_num"
Exit 1
Fi

Host_name= ' hostname '
Start_num=0
Process_num=$1
end_num=$ ((start_num + process_num-1))

rabbitmq_node_port=5672 rabbitmq_nodename= "Rabbit" rabbitmq_server_start_args= "-rabbitmq_management listener [{ port,15672}] "rabbitmq-server-detached

For ((i=$ (start_num+1); i<= $END _num; i++))
Do
rabbitmq_prot=$ ((i + 5672))
manage_port=$ ((i + 15672))
Node_name= "Rabbit_$i"
Echo $RABBITMQ _prot
Echo $MANAGE _port
Echo $NODE _name
rabbitmq_node_port= $RABBITMQ _prot rabbitmq_server_start_args= "-rabbitmq_management listener [{PORT, $MANAGE _port}] "Rabbitmq_nodename= $NODE _name rabbitmq-server-detached
Sleep 3

Rabbitmqctl-n $NODE _name Stop_app
Rabbitmqctl-n $NODE _name Reset
echo "Join cluster"
Rabbitmqctl-n $NODE _name join_cluster [email protected] $HOST _name
Rabbitmqctl-n $NODE _name Start_app
Done

Rabbitmqctl cluster_status-n Rabbit


chmod a+x start_rabbitmq_cluster.sh
Start_rabbitmq_cluster.sh 3
After starting, you can view the configuration of the cluster nodes through the Rabbitmqctl-n rabbit cluster_status, or view them in the Web Administration page

7. How to handle when configuring network Partion

Cat<<eof>>/usr/local/rabbitmq/rabbitmq_server-3.1.0/etc/rabbitmq/rabbitmq.conf
[
{rabbit, [{cluster_partition_handling, pause_minority}]}
].
Eof
Reference: http://my.oschina.net/hncscwc/blog/174417


Java code example
Producer
Consumer

RabbitMQ Concepts and Java examples

Related Article

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.