Rabbitmq运用之direction模式

Rabbitmq运用之简单模式

环境安装

erlang :https://www.erlang.org/downloads

rabbitmq 官方下载地址 https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.9.13

个人阿里云盘分享:「rabbitmq」https://www.aliyundrive.com/s/AitgweRxMJE 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

安装好之后在RabbitMQ Command Prompt (sbin dir) 输入rabbitmq-plugins enable rabbitmq_management 启动web 管理页面

在浏览器里面输入地址http://localhost:15672/ 看到如下界面就说明成功了

Rabbitmq运用之direction模式_第1张图片

使用rabbitmqctl --help 可以查看帮助

rabbitmqctl list_user 可以查看用户,找个用户改密码可以登录进网站

一般 guest/guest ,忘记的话可以用 rabbitmqctl change_password guest 重置密码

登录进去之后页面如下,说明已经安装完成了

Rabbitmq运用之direction模式_第2张图片

我们先增加一个用户吧,我起名Henry,密码123456,赋予administrator权限。我这边就不多做介绍

接下来我们开始写代码了。

添加依赖包 RabbitMQ.Client.dll

这里备注一下

以为可以不用定义exchange的,默认使用default现在没办法用,只能自己定义

RabbitMQ.Client.Exceptions.OperationInterruptedException:“The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=403, text=‘ACCESS_REFUSED - operation not permitted on the default exchange’, classId=50, methodId=20”

可能是新版本变动的。

下面是客户端代码,也就是消费端。

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace RabbitmqdefaultClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionFactory connectionFactory = new ConnectionFactory()
            {

                HostName = "127.0.0.1",
                UserName = "Henry",
                Password = "123456",
                Port = 5672,
              

            };
            connectionFactory.AutomaticRecoveryEnabled = true;
            //factory.setUri("amqp://userName:password@hostName:portNumber/virtualHost");
            //创建连接
            IConnection connection = connectionFactory.CreateConnection();
            //创建一个chanel
            IModel model = connection.CreateModel();
            //   ExchangeType.Direct
            //    model.ExchangeDeclare();
          model.ExchangeDeclare("HenryExchange", ExchangeType.Direct);
            model.QueueDeclare("HenryQueue", true , false,false,null);
        
            model.QueueBind("HenryQueue", "HenryExchange", "test");

            EventingBasicConsumer defaultBasicCohnsumer = new EventingBasicConsumer(model);
            defaultBasicCohnsumer.Received += (obj, e) =>
            {
                string body = Encoding.UTF8.GetString(e.Body.ToArray());
                Console.WriteLine(body);


            };
            //这些参数后面讲解autoAck=true 自动确认
            //  string BasicConsume(string queue, bool autoAck, string consumerTag, bool noLocal, bool exclusive, IDictionary arguments, IBasicConsumer consumer);
            model.BasicConsume("HenryTest", true, "", false, false, null, defaultBasicCohnsumer);

            //asicGetResult BasicGet(string queue, bool autoAck);  另外一种直接用方法一个一个获取
            //我个人比较喜欢用事件
            Console.ReadKey();
            Console.WriteLine("Hello World!");
        }
    }
}

下面是服务器端,也就相当推送消息的

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace RabbitmqDefaultServer
{
    class Program
    {
        static void Main(string[] args)
        {

            ConnectionFactory connectionFactory = new ConnectionFactory()
            {

                HostName = "127.0.0.1",
                UserName = "Henry",
                Password = "123456" ,
                Port = 5672

            };
          
            //创建连接
            IConnection connection = connectionFactory.CreateConnection();
            //创建一个chanel
            IModel model = connection.CreateModel();
            for (int i = 0; i < 100; i++)
            {
                String str = "测试数据" + i;

                byte  [] array= Encoding.UTF8.GetBytes(str);
                //将数据放入队列
                model.BasicPublish("World", "test",true,null, array);
            }
            Console.ReadKey();
            Console.WriteLine("Hello World!");
        }

       
    }
}

今天先写到这里,上面的模式就是direction ,后续再讲 fanout topic,headers, rpc等其他内容

        Console.WriteLine("Hello World!");
    }

   
}

}


今天先写到这里,上面的模式就是direction ,后续再讲  fanout topic,headers, rpc等其他内容

你可能感兴趣的:(数据仓库,c#,rabbitmq)