A Preliminary Study on RabbitMQ
1. What is MQ?
MQ is called Message Queue. MQ is a communication method for applications. Applications write and retrieve data (messages) for applications in the inbound and outbound queues to communicate with each other without dedicated connections. Message transmission refers to the communication between programs by sending data in messages, rather than by directly calling each other. Direct calls are usually used for such remote process calls. Queuing means that applications communicate through queues. The use of the queue removes the need to receive and send applications simultaneously. Among them, more mature MQ products include ibm websphere mq.
2. Features of MQ
A typical example of the MQ consumption-producer model is that one end continuously writes messages to the message queue, while the other end can read or subscribe to messages in the queue. MQ is similar to JMS, but the difference is that JMS is a standard and API definition of sun java Message Middleware service, while MQ is a specific implementation and product that complies with the AMQP protocol.
3. Use Cases
Recently, in the project, some operations that do not require immediate return and time-consuming are extracted for asynchronous processing. This asynchronous processing method greatly saves the server's request response time, this improves the system throughput.
4. What is RabbitMQ?
RabbitMQ is a complete and usable enterprise Message System Based on AMQP. He complies with the Mozilla Public License open-source protocol.
5. RabbitMQ Installation
5.1) install ERLANG
Compile and install Erlang R15B on CentOS
Install Erlang through YUM on CentOS 5.7
Source code compilation and installation of Erlang in CentOS 5.5
First, because RabbitMQ is implemented by ERLANG, the ERLANG (http://www.erlang.org/download.html) source code is downloaded.
Extract the source code to the ERLANG folder $ ERLANG
Install the dependency package:
Yum install tk
Yun install tcl
Yum install unixODBC
Go to $ ERLANG. Compile ERLANG
./Configure-prefix =/usr/local/erlang
./Make
./Make install
Add the erlang bin directory to PATH
5.2) install rabbitMQ
Download RabbitMQ (http://www.rabbitmq.com/download.html) and unzip it to $ RMQ.
Start RabbitMQ
./Bin/rabbitmq-server
6. Write the RabbitMQ producer Client
Package org. corey. mq;
Import com. rabbitmq. client. Channel;
Import com. rabbitmq. client. Connection;
Import com. rabbitmq. client. ConnectionFactory;
Import com. rabbitmq. client. ConnectionParameters;
Import com. rabbitmq. client. MessageProperties;
Public class MQTestor {
Public static void main (String [] args) throws Exception {
ConnectionParameters params = new ConnectionParameters ();
Params. setUsername ("guest ");
Params. setPassword ("guest ");
Params. setVirtualHost ("/");
Params. setRequestedHeartbeat (0 );
ConnectionFactory factory = new ConnectionFactory (params );
Connection conn = factory. newConnection ("192.168.1.101", 5672 );
Channel channel = conn. createChannel ();
Channel. exchangeDeclare ("ex1", "direct", true );
Channel. queueDeclare ("q1", true );
Channel. queueBind ("q1", "ex1", "m1 ");
Byte [] msg = "hello world". getBytes ();
Channel. basicPublish ("ex1", "m1", MessageProperties. PERSISTENT_TEXT_PLAIN, msg );
Channel. close ();
Conn. close ();
}
}
7. Write a Consumer Client
ConnectionParameters params = new ConnectionParameters ();
Params. setUsername ("guest ");
Params. setPassword ("guest ");
Params. setVirtualHost ("/");
Params. setRequestedHeartbeat (0 );
ConnectionFactory factory = new ConnectionFactory (params );
Connection conn = factory. newConnection ("192.168.1.101", 5672 );
Channel channel = conn. createChannel ();
GetResponse res = channel. basicGet ("q1", false );
If (res! = Null ){
System. out. println (new String (res. getBody ()));
Channel. basicAck (res. getEnvelope (). getDeliveryTag (), false );
} Else {
System. out. println ("No message! ");
}
8. Concepts of RabbitMQ
Exchange: The switch determines the message routing rules;
Queue: Message Queue;
Channel: a Channel for reading and writing messages;
Bind: a message is bound with Queue and Exchange, which indicates the message that conforms to the routing rules and will be placed in the message Queue;
9. RabbitMQ message persistence
1) set the vswitch to persistent;
2) set the channel to persistent
3) The setting is persistent when the message is sent.
When we "produce" a persistent message, try to interrupt the MQ service, start the consumer to obtain the message, and the message can still be restored. On the contrary, an exception is thrown.
Install RabbitMQ in CentOS 5.6
Detailed installation record of RabbitMQ client C ++
Try RabbitMQ in Python
Deployment of production instances in the RabbitMQ Cluster Environment
Use PHP + RabbitMQ in Ubuntu
Process of installing RabbitMQ on CentOS
RabbitMQ details: click here
RabbitMQ: click here
This article permanently updates the link address: