C # Queue RabbitMQ with both good and bad (with source end of the text): Q and simple application message queue MQ (II)

The last chapter we talk about the queue (Queue), this chapter we talk about Message Queue message queue, referred to as MQ.

definition:

  MQ is MessageQueue, short message queue (Message Queue is a popular open source system, using erlang language development). MQ communication method is an application of the application.

By reading and writing the application and dequeue the message communication without a dedicated connection to link them.

Message passing between program is transmitted through a communication data in the message, rather than through direct calls to communicate with each other, generally used techniques of remote procedure calls.

 It refers to the application queue by the queue to communicate. Application of queues for receiving and sending data simultaneously.

Features:

  MQ consumers - on behalf of the producer of the model. One end of the write messages to the message queue, the other end may be read or subscribe to messages in the queue.

MQ follow the protocol AMQP (Advanced Message Queuing Protocol: full compliance with that client applications and the messaging middleware server interoperability specification possible) is embodied and products.

application:

  When using MQ, we do not need real-time information return. Access to information and return information for asynchronous processing.

For example: In the project, we need to use CAN bus to obtain real-time information about the car from the car system, but there is no need to return the car information.

For example, get a car tire pressure, but we do not have the information or the result of a return to the car.

    To use RabbitMQ C # project to obtain real-time data, then you need to install the client libraries: RabbitMQ.Client.dll, mentioned below.

Alternate download path:

Link: https: //pan.baidu.com/s/1zcQmPnBF7WcD8sqV4W54pw 
extraction code: 6962 


EDITORIAL:

This will need to install RabbitMQ service, download and install the Erlang environment, introducing RabbitMQ.client.dll dynamic libraries. Here are the official website you can download the appropriate content.

I use this windows 64-bit, here I am finishing the installation program in the Baidu network disk, official website very slow to open Erlang I Baidu network disk download

Link: https: //pan.baidu.com/s/1zcQmPnBF7WcD8sqV4W54pw
extraction code: 6962 


 

installation

We need to install RabbitMQ service:

Download the official website address: http://www.rabbitmq.com/download.html .

After the download is complete click Next to have.

If it does not Erlang environment will pop up the following tips:

 

Download and install the Erlang environment, the installation has been to point the next step

Address: http://www.erlang.org/downloads

If you open the page slowly or not open, go to download I order also, but I have a windows 64 a.

Link: https: //pan.baidu.com/s/1zcQmPnBF7WcD8sqV4W54pw 
extraction code: 6962 

After installation is complete we need to configure the environment variables, as follows:

Right-click on [computer], Properties, Advanced System Settings, Advanced, Environment variables,

Create a system variable.

Entry

Variable name: ERLANG_HOME,

Variable value: C: \ Program Files \ erl9.3

Variable value is the path you just install the Erlang

 

 Then find the environment variables inside the Path, click Edit at the end, plus the value of the variable;% ERLANG_HOME% \ bin ;, remember a semi-colon (semicolon)

 

 

After a successful installation of the service you will see in service. .

 

Then install RabbitMQ, also has been click Next to, 

Here after the preparatory work done, then we write the code.

Some children's shoes do not know why you need to install RabbitMQ services and Erlang environment, I am here to look at the popularity of simple details, please look at Baidu.

RabbitMQ is to achieve the Advanced Message Queuing Protocol (AMQP) is an open source message broker software (also known as message-oriented middleware).

RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。

所有主要的编程语言均有与代理接口通讯的客户端库。

.


 代码实例:

为了讲解效果更佳,我们新建两个控制台应用程序MessageQueueClient(生产者)和MessageQueueServer(消费者),

不要急着建立,看下面的代码依次建立。

生产者 :

新建控制台应用程序MessageQueueClient,引用动态文件库RabbitMQ.Client.dll,可以去百度下载一个,上面的网盘路径里面有。

入队代码编写:

using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MessageQueueClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //生产者
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "127.0.0.1";
            //默认端口
            factory.Port = 5672;
            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    //在MQ上定义一个持久化队列,如果名称相同不会重复创建
                    channel.QueueDeclare("MyRabbitMQ", true, false, false, null);
                    while (true)
                    {
                        string message = string.Format("{0}", Console.ReadLine());  //Console.ReadLine()为控制台输入的内容,我们可以用其他方式获取
                        byte[] buffer = Encoding.UTF8.GetBytes(message);
                        IBasicProperties properties = channel.CreateBasicProperties();  
                        properties.DeliveryMode = 2;
                        channel.BasicPublish("", "MyRabbitMQ", properties, buffer);  //入队
                        Console.WriteLine("入队成功:" + message);
                    }
                }
            }
        }
    }
}

控制台入队操作,控制台这一步可以结合实际代码需求进行入队。

这里就入队成功了,接下来我们出队,也就是读取数据,这里和readis有点像,我们之前安装的RabbitMQ服务就是在这里用到了。

 

生产者 :

新建控制台应用程序MessageQueueServer,引用动态文件库RabbitMQ.Client.dll,可以去百度下载一个,上面的网盘路径里面有。

出队代码编写:

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MessageQueueServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //消费者
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "127.0.0.1";
            //默认端口
            factory.Port = 5672;
            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    //在MQ上定义一个持久化队列,如果名称相同不会重复创建
                    channel.QueueDeclare("MyRabbitMQ", true, false, false, null);

                    //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息
                    channel.BasicQos(0, 1, false);

                    Console.WriteLine("Listening...");

                    //在队列上定义一个消费者
                    QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
                    //消费队列,并设置应答模式为程序主动应答
                    channel.BasicConsume("MyRabbitMQ", false, consumer);

                    while (true)
                    {
                        //阻塞函数,获取队列中的消息
                        BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        byte[] bytes = ea.Body;
                        string str = Encoding.UTF8.GetString(bytes);

                        Console.WriteLine("读取队列消息:" + str.ToString());
                        //回复确认
                        channel.BasicAck(ea.DeliveryTag, false);
                    }
                }
            }
        }
    }
}

运行代码,读取队列里面的内容,遵循先入先出原则。

这样队列的数据就读取到了。

 

总结:

这是一个简单的消息队列的应用,写的比较粗浅,具体需要结合实际应用项目编写。

另外感谢大家的支持^_^

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiongze520/p/10967874.html