NET Distributed System four: RABBITMQ Message Queuing application

Source: Internet
Author: User
Tags rabbitmq

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

Route add default GW 192.168.1.1

(2) Installing Erlang

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

Erl

(4) Successful Installation

    

  2, install RABBITMQ service side

(1) Download the installation package

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:

RPM--import Https://www.rabbitmq.com/rabbitmq-signing-key-public.ascyum Install rabbitmq-server-3.6.0-1.noarch.rpm

(3) Enable Web management plug-in

Rabbitmq-plugins Enable Rabbitmq_management

(4) Start RABBITMQ

Chkconfig rabbitmq-server on/sbin/service rabbitmq-server start

(5) Firewall open port

# 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/

Rabbitmqctl add_user Test testrabbitmqctl set_user_tags test Administrator
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

[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 + ", operation Log, Time:" + 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.

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.

NET Distributed System four: RABBITMQ Message Queuing application

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.