【问题标题】:RabbitMQ C# connection trouble: None of the specified endpoints were reachableRabbitMQ C# 连接问题:没有一个指定的端点是可达的
【发布时间】:2019-05-17 17:13:06
【问题描述】:

我有这个代码:

public class Rabbit
    {
        public IConnection GetConnection()
        {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            Uri uri = new Uri("amqp://login:password@1.2.3.4:5672/host");
            connectionFactory.Uri = uri;
            return connectionFactory.CreateConnection();
        }

        public  void Send(string queue, string data)
        {
            using (IConnection connection = GetConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue, false, false, false, null);
                    channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data));

                }
            }
        }

        public  string  Receive(string queue)
        {
            using (IConnection connection = GetConnection())
            {
                using (IModel channelconsumer = connection.CreateModel())
                {
                    //channelconsumer.QueueDeclare(queue, false, false, false, null);
                    BasicGetResult result = channelconsumer.BasicGet(queue, true);
                    if (result != null)
                    {
                        string data = Encoding.UTF8.GetString(result.Body);
                        Console.WriteLine(data);
                        return data;
                    } else
                    {
                        return null;
                    }
                }
            }
        }
    }

我使用它:

var rabbit = new Rabbit();
rabbit.Send("name.sample.example", json); 

运行此代码时出现错误:

RabbitMQ.Client.Exceptions.BrokerUnreachableException:“没有一个指定的端点是可达的”

排队:

return connectionFactory.CreateConnection();

我已登录,密码 100% 正确。 如何解决?

【问题讨论】:

  • 你确定 RabbitMQ 正在运行吗?
  • 是的,它正在运行。我可以毫无问题地登录它

标签: c# rabbitmq


【解决方案1】:

写出不带“host”后缀的URI:

Uri uri = new Uri("amqp://login:password@1.2.3.4:5672/");

说明:amqp 方案 URI 中的最后一个占位符保留用于指定虚拟主机 - 在 RabbitMQ 中,虚拟主机是资源的逻辑分组(例如交换和队列),旨在支持不同客户端的资源分离使用相同的 RabbitMQ 实例。默认情况下,仅定义未命名的“/”虚拟主机。与不存在的虚拟主机名的连接将失败。

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,我使用的凭据没有访问权限。

    此用户无权访问任何虚拟主机。采用 下面的“设置权限”授予访问虚拟主机的权限

    所以我在 RabitMQ 中添加了我的用户名,为其提供访问权限。 现在一切都很好。默认情况下,您在 RabitMQ 中拥有来宾/来宾凭据

    【讨论】:

      【解决方案3】:

      对我来说,解决方案是删除 System.Runtime.CompilerServices.Unsafe 来自 app.config

          <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
        </dependentAssembly>
        <dependentAssembly>
      

      【讨论】:

        猜你喜欢
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多