首页 > 解决方案 > RabbitMQ.Client.Exceptions.BrokerUnreachableException:'指定的端点都不可到达'

问题描述

我无法让 RabitMq 工作并收到此错误。我发布这个是为了让其他有同样问题的人最终可以比我更快地找到解决方案——我用了几个小时来找到问题的解决方案。

图像1

标签: c#consolerabbitmq

解决方案


谢谢@evgenirusev - https://github.com/devmentors/DNC-DShop/issues/8

RabitMQ - Docker - https://hub.docker.com/_/rabbitmq

图像1

我试图让 RabitMq“Hello World”教程工作,但不知道为什么它不工作。

这对我有用: -希望它对某人有所帮助

1. 在 Docker 终端“客户端”中制作 RabitMq Image - new Image - docker run -d --hostname my-rabit --name ecomm-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management

2.比我192.168.99.100:15672用端口复制“ ”的IP:15672并在RabitMq中使用它作为hostname -“从docker复制kitematic app

3. 在 RabitMQ 控制台 App 发送和接收 - factory.HostName = "192.168.99.100";

4. 在 RabitMQ 控制台应用程序发送和接收 - factory.Port = AmqpTcpEndpoint.UseDefaultPort;

5. 别忘了——Nuget——“RabbitMQ.Client” <PackageReference Include="RabbitMQ.Client" Version="6.2.1" />

收到:

 ConnectionFactory factory = new ConnectionFactory();
        //factory.UserName = "user";
        //factory.Password = "password";
        //factory.VirtualHost = "/";
        factory.HostName = "192.168.99.100";
        factory.Port = AmqpTcpEndpoint.UseDefaultPort;
        IConnection connection = factory.CreateConnection();


        //using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);

            Console.WriteLine(" [*] Waiting for messages.");

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body.ToArray();
                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();

发送:

    ConnectionFactory factory = new ConnectionFactory();
    //factory.UserName = "user";
    //factory.Password = "password";
    //factory.VirtualHost = "/";
    factory.HostName = "192.168.99.100";
    factory.Port = AmqpTcpEndpoint.UseDefaultPort;
    IConnection connection = factory.CreateConnection();

    //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 World!";
        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();

推荐阅读