Rabbitmq及Web監控工具的使用

1.Rabbitmq的官方下載地址:https://www.rabbitmq.com/download.htmlhtml

注意:web

    RabbitMQ須要安裝64位支持的Erlang for Windows版本。Erlang版本包括Windows安裝程序Erlang Solutions也 提供二進制64位Erlang版本。瀏覽器

   重要提示:必須使用管理賬戶運行Erlang安裝程序,不然RabbitMQ安裝程序所需的註冊表項將不存在。工具

 

2.web監控工具(下載RabbitMQ時默認包含web監控工具)spa

使用方式:.net

(1)進入到RabbitMQ的安裝地址:code

    輸入命令啓動監控工具:rabbitmq-plugins enable rabbitmq_managementhtm

 

(2)步驟1啓動後打開瀏覽器輸入網址:http://localhost:15672/   帳號密碼都是:guest    blog

 

3..net 實現簡單的消息發送與接收token

(1)建立.net core控制檯項目,新建發送類:Send.cs

注:須要先添加程序集依賴項:RabbitMQ.Client

//創建一個消息,名稱爲:hello
public
class Send { public static void sendMain() { var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.QueueDeclare( queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null ); string message = "Hello Amanda"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish( exchange: "", routingKey: "hello", basicProperties: null, body: body ); Console.WriteLine("[x] Sent {0}", message); } Console.WriteLine("Press [enter] to exit."); Console.ReadLine(); } } }

執行以後能夠經過web監控工具程序查看結果:

    

 

 

(2)建立.net core控制檯項目,新建接收類:Receive.cs

注:須要先添加程序集依賴項:RabbitMQ.Client

public class Receive
{
    public static void ReceiveMain()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(
                    queue:"hello",
                    exclusive:false,
                    autoDelete:false,
                    arguments:null
                    );
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine("[X] Received {0}", message);
                };
                channel.BasicConsume(
                    queue:"hello",
                    autoAck:true,
                    consumer:consumer
                    );
                Console.WriteLine("Press [enter] to exit");
                Console.ReadLine();
            }
        }
    }
}

執行以後能夠經過web監控工具程序查看結果:

相關文章
相關標籤/搜索