RABBITMQ Message Queuing application
One of the core middleware of the message Communication component net Distributed system, the application of the system with high concurrency, the decoupling of the various components of the dependent scenarios. This framework uses Message Queuing middleware mainly in two aspects: one is to solve some high concurrency of business processing, and the other is to transfer the system log through Message Queuing. At present, the industry uses more Message Queuing components such as RABBITMQ, ActiveMQ, MSMQ, Kafka, Zeromq, and so on, the MQ component annotation of system architecture and RABBITMQ as Message queue middleware.
Figure 1-Message Queuing component
I. Introduction of RABBITMQ
RABBITMQ is an open source Message Queuing component developed by Erlang based on the AMQP (Message Queuing protocol). is an excellent Message Queuing component that consists of two parts: the server side and the client, and the client supports multiple language drivers, such as. Net, JAVA, Erlang, and so on. RABBITMQ and other Message Queuing components performance comparison, not introduced here, the Internet has a lot of information.
Ii. Introduction to the principle of RABBITMQ
Figure 2-RABBITMQ Structure
RabbitMQ Middleware is divided into service-side (RabbitMQ Server) and clients (RabbitMQ client), the server can be understood as a message of the agent consumer, the client is also divided into the message producer (Producer) and the message consumer (Consumer).
1, the message producer (Producer): The main production message and the message based on the TCP protocol, through the establishment of connection and channel, the message to RABBITMQ Server, for the Producer is basically completed the work.
2. Service side (RabbitMQ Server): primarily responsible for handling message routing, distribution, queue, cache, and dequeue. Mainly consists of three parts: Exchange, Routingkey, Queue.
(1) Exchange: Used to receive messages sent by the message producer, there are three types of exchange:direct, fanout,topic, different types of implementation of different routing algorithms;
A. Direct exchange: A message with routing key histograms is pushed directly into the corresponding queue, and a routing key with the same name is created by default when the queue is created.
B. Fanout Exchange: is a broadcast mode that ignores Routingkey rules.
C. Topic Exchange: Apply a theme to pattern-matching routing based on key, for example: If abc* is pushed into the queue for all abc*, if ABC. #则推入到abc. Xx.one, Abc.yy.two corresponds to the queue.
(2) Routingkey: Is the RABBITMQ implementation of the rules for routing distribution to each queue, and in conjunction with binging provides for exchange use to push messages into the queue;
(3) Queue: Is the message queue, you can define multiple queues as needed, set the properties of the queue, such as: message removal, message caching, callback mechanism and other settings to achieve communication with consumer;
3, the message consumer (Consumer): mainly responsible for the consumption of queue messages, also based on the TCP protocol, through the establishment of connection and channel and queue transmission messages, a message can be more than Consumer consumption;
4, Key noun description : Connection, Channel, binging, etc.;
(1) Connection: The connection between the client and the server is established.
(2) Channel: is based on connection to establish a communication channel, because each time connection establish TCP protocol communication overhead and performance consumption is large, so once established connection, use multiple channel channels communication to reduce overhead and improve performance.
(3) Binging: is a bundle definition that binds exchange and queue, defining routingkey related policies.
Iii. RABBITMQ Installation and deployment
The above introduction to RABBITMQ, next we build a Message Queuing service practice. The RABBITMQ server can run on Windows, Linux, and Mac platforms, and the client supports a variety of technology implementations. This time we will build on the Linux CentOS7 platform.
1. Installation of Erlang operating environment
Because RABBITMQ is developed using Erlang technology, you need to install the Erlang runtime environment before you can install the Message Queuing service.
(1) Configure the system to access the public network, set the default gateway
1 |
route add default gw 192.168.1.1 |
(2) Installing Erlang
12 |
su -c ‘rpm -Uvh http://download.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm‘ sudo yum install erlang |
(3) Check if Erlang is installed successfully
(4) Successful Installation
2, install RABBITMQ service side
(1) Download the installation package
1 |
wget http: //www.rabbitmq.com/releases/rabbitmq-server/v3.6.0/rabbitmq-server-3.6.0-1.noarch.rpm |
(2) Install and configure the RABBITMQ server, 3.6.0 Version:
12 |
rpm --import https: //www.rabbitmq.com/rabbitmq-signing-key-public.asc yum install rabbitmq-server-3.6.0-1.noarch.rpm |
(3) Enable Web management plug-in
1 |
rabbitmq-plugins enable rabbitmq_management |
(4) Start RABBITMQ
12 |
chkconfig rabbitmq-server on /sbin/service rabbitmq-server start |
(5) Firewall open port
123 |
# firewall-cmd --permanent --zone=public --add-port=5672/tcp # firewall-cmd --permanent --zone=public --add-port=15672/tcp # firewall-cmd --reload |
(6) RABBITMQ create Guest account by default, only for localhost login page administrator, native access address: http://localhost:15672/
12 |
rabbitmqctl add_user test test rabbitmqctl set_user_tags test administrator<br>rabbitmqctl set_permissions -p / test ".*" ".*" ".*" |
RabbitMQ Administrator page.
Iv. Application of RABBITMQ
This section describes the Web App production log, which is transmitted through RABBITMQ, and then the log service receives Message Queuing messages.
Figure 3-Functional structure
The system uses the official client, which is referenced by NuGet.
1. Web application Production business log
12345678910 |
[HttpPost]
public
ActionResult Create()
{
this
.HttpContext.Session[
"mysession"
] = DateTime.Now.ToString(
"yyyy-MM-dd hh:mm:ss"
);
var
txt = Request.Form[
"txtSite"
].ToString();
RabbitMQHelper helper =
new
RabbitMQHelper();
helper.SendMsg(txt +
",操作日志,时间:" + DateTime.Now.ToString(
"yyyy-MM-dd hh:mm:ss"
));
return
RedirectToAction(
"Index"
);
}
|
Page.
2. Log service receives log messages
A log processing service is developed based on window form and the received messages are printed out.
123456789101112131415161718192021 |
private
void
btnReceive_Click(
object
sender, EventArgs e)
{
isConnected =
true
;
using (
var
channel = connection.CreateModel())
{
channel.QueueDeclare(
"MyLog"
,
false
,
false
,
false
,
null
);
var
consumer =
new
QueueingBasicConsumer(channel);
channel.BasicConsume(
"MyLog"
,
true
, consumer);
while
(isConnected)
{
var
ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var
body = ea.Body;
var
message = Encoding.UTF8.GetString(body);
txtMsg.Text += message +
"\r\n"
;
}
}
}
|
3, RABBITMQ page monitoring situation
The RABBITMQ comes with a page monitoring tool that allows you to monitor the MQ situation:
V. Summary
This article about the introduction of RABBITMQ construction and application of the system, RABBITMQ is relatively simple, but also provides interface monitoring tools to facilitate operation and maintenance personnel monitoring.
Author: Andon
Source: Http://www.cnblogs.com/Andon_liu
About the focus on Microsoft Platform Project architecture, management. Familiar with design patterns, domain drivers, architecture design, agile development and project management. is mainly engaged in ASP, Wcf/web API, SOA, MSSQL, Redis aspects of project development, architecture, management work. If you have questions or suggestions, please learn to discuss together!
This article is copyrighted by the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link.
If you have questions, you can email: [Email protected] contact me, thank you.
Classification:. NET Distributed Systems
RABBITMQ Message Queuing application