zoukankan      html  css  js  c++  java
  • .Net Core中使用RabbitMQ

    (1)、引入依赖

    RabbitMQ.Client

    (2)、编写发布者代码

     1             var connectionFactory = new ConnectionFactory() { HostName="192.168.205.128",UserName="guest",Password="guest"};  //创建连接工厂
     2             var connection = connectionFactory.CreateConnection();  //创建connection
     3             var channel = connection.CreateModel(); //创建channel
     4             //声明交换机
     5             //String exchange,   交换机名称
     6             //String type,  交换机类型
     7             //Boolean durable, 是否持久化
     8             //Boolean autoDelete,   是否自动删除
     9             //IDictionary< String, Object > arguments   交换机参数
    10             channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null);
    11             //声明队列
    12             //String queue, 队列名称
    13             //Boolean durable,  是否持久化
    14             //Boolean exclusive,    是否专有的(排外)
    15             //Boolean autoDelete,   是否自动删除
    16             //IDictionary<String, Object> arguments 队列参数
    17             channel.QueueDeclare("netQueue", true, false, false, null);
    18             //将队列绑定到交换机上
    19             //String queue,   队列名称  
    20             //String exchange,  交换机名称
    21             //String routingKey,    routingKey
    22             //IDictionary< String, Object > arguments   绑定参数
    23             channel.QueueBind("netQueue", "netExchange", "netMessage", null);
    24             //发布消息
    25             //String exchange,  交换机名称
    26             //String routingKey,    routingKey
    27             //IBasicProperties basicProperties,     发布属性
    28             //Byte[] body   消息内容
    29             channel.BasicPublish("netExchange", "netMessage", null, Encoding.UTF8.GetBytes("来自.net的问候"));
    30             Console.ReadKey();

    (3)、编写消费者代码

     1             var connectionFactory = new ConnectionFactory() { HostName = "192.168.205.128", UserName = "guest", Password = "guest" };  //创建连接工厂
     2             var connection = connectionFactory.CreateConnection();  //创建connection
     3             var channel = connection.CreateModel(); //创建channel
     4             //声明交换机
     5             //String exchange,   交换机名称
     6             //String type,  交换机类型
     7             //Boolean durable, 是否持久化
     8             //Boolean autoDelete,   是否自动删除
     9             //IDictionary< String, Object > arguments   交换机参数
    10             channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null);
    11             //声明队列
    12             //String queue, 队列名称
    13             //Boolean durable,  是否持久化
    14             //Boolean exclusive,    是否专有的(排外)
    15             //Boolean autoDelete,   是否自动删除
    16             //IDictionary<String, Object> arguments 队列参数
    17             channel.QueueDeclare("netQueue", true, false, false, null);
    18             //将队列绑定到交换机上
    19             //String queue,   队列名称  
    20             //String exchange,  交换机名称
    21             //String routingKey,    routingKey
    22             //IDictionary< String, Object > arguments   绑定参数
    23             channel.QueueBind("netQueue", "netExchange", "netMessage", null);
    24             //1.直接获取消息
    25             //var result = channel.BasicGet("netQueue", true);
    26             //Console.WriteLine(Encoding.UTF8.GetString(result.Body));
    27             //2.使用事件机制获取消息
    28             EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
    29             consumer.Received += (sender, e) =>
    30             {
    31                 Console.WriteLine(Encoding.UTF8.GetString(e.Body));
    32             };
    33             channel.BasicConsume("netQueue", true, consumer);
    34             Console.ReadKey();
  • 相关阅读:
    js(一)
    css (一)
    html
    Spring MVC(四)
    Spring MVC(三)
    Spring MVC(二)
    Spring MVC(一)
    Druid应用
    C3P0使用
    jdbc(二)
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10388793.html
Copyright © 2011-2022 走看看