我有一个关于Rabitmq扇出交换的问题,我的问题是我想要一个发布订阅示例,使用rabbitmq和c#.So,我创建了两个项目,一个是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();
}
}但我找不到messages.Why?
发布于 2018-08-23 09:53:32
所以我发现了你的问题。首先,您在消费者中的交换声明是错误的。
您在发布服务器中声明exchange为"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();
}https://stackoverflow.com/questions/51964937
复制相似问题