代码改变世界

Rabbitmq及Web监控工具的使用

2019-08-12 22:58  石吴玉  阅读(9465)  评论(0编辑  收藏  举报

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

注意:

    RabbitMQ需要安装64位支持的Erlang for Windows版本。Erlang版本包括Windows安装程序Erlang Solutions也 提供二进制64位Erlang版本。

   重要提示:必须使用管理帐户运行Erlang安装程序,否则RabbitMQ安装程序所需的注册表项将不存在。

 

2.web监控工具(下载RabbitMQ时默认包含web监控工具)

使用方式:

(1)进入到RabbitMQ的安装地址:

    输入命令启动监控工具:rabbitmq-plugins enable rabbitmq_management

 

(2)步骤1启动后打开浏览器输入网址:http://localhost:15672/   账号密码都是:guest    

 

3..net 实现简单的消息发送与接收

(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监控工具程序查看结果: