C# 使用 RabbitMQ

Erlang运行时的下载地址: http://erlang.org/download/

消息队列服务的下载地址:https://www.rabbitmq.com/download.html

 

参考资料:

RabbitMQ教程

C# 操作rabbitmq(一)

C# 操作rabbitmq(二)

C# 操作rabbitmq(三)

C# 操作rabbitmq(四)

C# 操作rabbitmq(五)

 

如何优雅的使用RabbitMQ

rabbitmq_坑

RabbitMQ进阶/管理/配置

示例:https://github.com/das2017/06-RabbitMQDemo

示例:

 

<appSettings>
  <add key="AppID" value="150107"/>    
  <add key="RabbitMQUri" value="amqp://flight:yyabc123@139.198.13.12:4128/Flight" />
</appSettings>

 

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

namespace SendMQ
{
    class Program
    {
        static void Main(string[] args)
        {
            //var factory = new ConnectionFactory();
            //factory.HostName = "127.0.0.1";
            //factory.UserName = "admin";
            //factory.Password = "admin";
            //factory.VirtualHost = "test";
            var factory = new ConnectionFactory { Uri = new Uri(ConfigurationManager.AppSettings["RabbitMQUri"]) };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                string queue = "MQ";
                channel.QueueDeclare(queue, false, false, false, null); //定义一个队列

                while (true)
                {
                    Console.Write("请输入要发送的消息:");
                    var message = Console.ReadLine();
                    var body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish("", queue, null, body); //发送消息

                    Console.WriteLine("已发送的消息: {0}", message);
                }
            }
        }
    }
}

 

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

namespace GetMQ
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory();
            factory.HostName = "127.0.0.1";
            factory.UserName = "admin";
            factory.Password = "admin";
            factory.VirtualHost = "test";
            // var factory = new ConnectionFactory { Uri =new Uri(ConfigurationManager.AppSettings["RabbitMQUri"]) };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                string queue = "MQ";
               channel.QueueDeclare(queue, false, false, false, null); //定义一个队列
                Console.WriteLine("准备接收消息...");
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (s, e) =>
                {
                    var body = e.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine("接收到的消息: {0}", message);
                };
                channel.BasicConsume(queue, true, consumer);  //开启消费者与通道、队列关联
                Console.ReadLine();
            }
        }
    }
}

  

posted @ 2019-09-22 09:37  linyongqin  阅读(400)  评论(0编辑  收藏  举报