【问题标题】:How to configure RabbitMQ connection with spring-rabbit?如何使用 spring-rabbit 配置 RabbitMQ 连接?
【发布时间】:2017-07-01 05:13:45
【问题描述】:

我正在关注 this guide 以了解如何将 spring-rabbit 与 RabbitMQ 一起使用。但是在本指南中,RabbitMQ 配置是默认的(本地主机服务器和凭据作为访客/访客)。如果我想使用 ip 地址和凭据连接到远程 RabbitMQ,我该怎么办?我不知道在我的应用程序中的何处设置这些信息。

【问题讨论】:

    标签: spring-boot rabbitmq spring-amqp spring-rabbit


    【解决方案1】:

    该指南的应用程序是 Spring Boot 应用程序。

    将文件application.properties 添加到src/main/resources

    然后您可以根据Spring Boot Documentation 配置rabbitmq 属性 - 向下滚动到rabbitmq 属性...

    ...
    spring.rabbitmq.host=localhost # RabbitMQ host.
    ...
    spring.rabbitmq.password= # Login to authenticate against the broker.
    spring.rabbitmq.port=5672 # RabbitMQ port.
    ...
    spring.rabbitmq.username= # Login user to authenticate to the broker.
    ...
    

    要连接到集群,请使用

    spring.rabbitmq.addresses= # Comma-separated list of addresses to which the client should connect.
    

    例如server1:5672,server2:5672.

    如果您不想使用引导自动配置,请自行声明 CachingConnectionFactory @Bean 并根据需要进行配置。

    【讨论】:

    • 这很奇怪,我按照你的建议将这些属性添加到application.properties 中。但是,这些属性似乎尚未加载到正在运行的应用程序中。我错过了什么?
    • 我刚刚下载了指南,将application.properties 添加到src/main/resources 以获取complete 版本并将其指向远程rabbitmq 实例,它工作得很好——我看到在rabbit 上创建了队列实例和输出消息。您如何运行指南?
    • 我的错,对不起,你的解决方案就像一个魅力。它在这里不起作用,因为我在我的配置类中添加了@Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage");}
    • 您如何以编程方式实现这一目标?
    • 不清楚你的意思;见the documentationsamples。最好提出一个全新的问题,而不是在对旧问题的评论中提出新问题。
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多