【问题标题】:Why I cannot create queue in rabbitmq in Windows?为什么我不能在 Windows 的 rabbitmq 中创建队列?
【发布时间】:2019-09-13 14:17:49
【问题描述】:

我是 RabbitMQ 的新手。

我在 Windows 10 上安装了 RabbitMQ 服务器。我可以在 Web 浏览器中登录服务器。当我运行下面的客户端代码(使用 AMQP-CPP 库)时,不会调用 channel.onSuccesschannel.onError。而且,我在网络浏览器中看不到我声明的 my-queue 队列和 my-exchange 交换。

如果我理解正确,我需要添加一些事件循环(?)。但是,我找不到任何适用于 Windows 的示例。你能解释一下可能是什么问题吗?

int main()
{
    // create an instance of your own tcp handler
    MyTcpHandler myHandler;

    // address of the server
    //AMQP::Address address("amqp://guest:guest@localhost:5672/");
    AMQP::Address address("localhost", 15672, AMQP::Login("guest", "guest"), "");

    // create a AMQP connection object
    AMQP::TcpConnection connection(&myHandler, address);

    // and create a channel b
    AMQP::TcpChannel channel(&connection);

    // use the channel object to call the AMQP method you like
    channel.declareExchange("my-exchange", AMQP::fanout)
        .onSuccess([]()
    {
        std::cout << "declared exchange " << std::endl;
    }).onError([](const char *message)
    {
        std::cout << "error: " << message << std::endl;

    });

    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    std::cout << "Press Enter..." << std::endl;
    std::getchar();

    return 0;
}

MyTcpHandler

class MyTcpHandler : public AMQP::TcpHandler
{
public:

    virtual void onConnected(AMQP::TcpConnection *connection) {}
    virtual void onError(AMQP::TcpConnection *connection, const char *message) {}
    virtual void onClosed(AMQP::TcpConnection *connection) {}
    virtual void monitor(AMQP::TcpConnection *connection, AMQP::tcp::Socket fd, int flags) {}
};

【问题讨论】:

    标签: c++ windows rabbitmq amqp


    【解决方案1】:

    你连接错误的端口 15672 是管理插件端口,你需要连接端口 5672 这是 AMQP 端口

    请更正如下代码

    AMQP::Address address("localhost", 5672, AMQP::Login("guest", "guest"), "");
    

    【讨论】:

    • 当您连接时,您是否在管理控制台 Web 浏览器的连接选项卡下看到一个新连接?
    • 不,我不知道。这就是为什么我认为我需要事件循环才能工作/连接。
    • 您是否尝试过此示例github.com/CopernicaMarketingSoftware/AMQP-CPP/blob/master/…,如果没有,您可以尝试并确保您的设置正确
    • 我刚刚尝试了LibBoostAsioHandler 的示例,但没有成功。
    • @theateist ,我认为设置可能存在问题......因为该示例运行良好......
    【解决方案2】:

    您可以使用 HareDu 2 Broker API 使用以下代码来完成。文档可以在这里找到:https://github.com/ahives/HareDu2

    var result = _container.Resolve<IBrokerObjectFactory>()
                    .Object<Queue>()
                    .Create(x =>
                    {
                        x.Queue("your_queue");
                        x.Configure(c =>
                        {
                            c.IsDurable();
                            c.AutoDeleteWhenNotInUse();
                            c.HasArguments(arg =>
                            {
                                arg.SetQueueExpiration(1000);
                                arg.SetPerQueuedMessageExpiration(2000);
                            });
                        });
                        x.Targeting(t =>
                        {
                            t.VirtualHost("your_vhost");
                            t.Node("your_node");
                        });
                    });
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 2015-06-16
      • 2018-04-08
      • 2022-06-10
      • 1970-01-01
      相关资源
      最近更新 更多