Rabbitmq Fanout Exchange C#

时间:2018-08-22 10:35:00

标签: c# .net rabbitmq publish-subscribe

我有一个关于Rabitmq扇出交换的问题,我的问题是我想用Rabbitmq和c#发布发布订阅示例。所以我创建了2个项目,其中一个是Publisher,就是那个

static void Main(string[] args)
{
      try
      {
          var factory = new ConnectionFactory() { HostName = "localhost" };
          using (var connection = factory.CreateConnection())
              using (var channel = connection.CreateModel())
              {
                  channel.ExchangeDeclare("example.exchange", ExchangeType.Fanout, true, false, null);
                  var message = GetMessage(args);
                  var body = Encoding.UTF8.GetBytes(message);
                  channel.BasicPublish(exchange: "example.exchange", routingKey: "", basicProperties: null, body: body);
                  Console.WriteLine(" [x] Sent {0}", message);
              }
        }
        catch (Exception ex)
        {
            Console.Write($"bir hata oluştu{ex.Message}");
        }
}

private static string GetMessage(string[] args)
{
    return ((args.Length > 0)
           ? string.Join(" ", args)
           : "info: Hello World!");
}

我有一个消费者

    static void Main(string[] args)
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange: "example.exchange", 
            type:"fanout",durable:true);

            var queueName = channel.QueueDeclare().QueueName;
            channel.QueueBind(queue: queueName, exchange: "foo.exchange", 
            routingKey: "");

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] {0}", message);
            };
            channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);

            Console.ReadLine();
        }
    }

但是我无法收到消息。为什么会这样?

1 个答案:

答案 0 :(得分:0)

所以我找到了你的问题。首先,您在消费者中的兑换声明是错误的。

您在发布者中声明交换为“ example.exchange”:

channel.ExchangeDeclare("example.exchange", ExchangeType.Fanout, true, false, null);

但是您的使用者中存在“ foo.exchange”:

channel.QueueBind(queue: queueName, exchange: "foo.exchange", routingKey: "");

将“ foo.exchange”更改为“ example.exchange”。

对于您的消费者,我可以使用以下几行来消费消息:

public static void Main(string[] args)
{
    ConnectionFactory factory = new ConnectionFactory();
    factory.UserName = "guest";
    factory.Password = "guest";
    factory.HostName = "localhost";
    factory.VirtualHost = "/";

    var connection = factory.CreateConnection();
    var channel = connection.CreateModel();

    var queueName = "test-queue";
    channel.QueueDeclare(queueName, false, false, false, null);
    channel.QueueBind(queueName, "example.exchange", "", null);

    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
        var bodyy = ea.Body;
        var messagee = Encoding.UTF8.GetString(bodyy);
        Console.WriteLine("received [x] {0}", messagee);
    };
    channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);

    Console.ReadLine();
}