Spring Boot 无法连接到rabbitmq

hof1towb  于 2023-01-26  发布在  RabbitMQ
关注(0)|答案(4)|浏览(608)

我有一个这样的RabbitMQ服务器

当我尝试通过Spring Boot amqp连接到此服务器时,我看到com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
我的配置是这样的

# Message
spring.activemq.broker-url=tcp://127.0.0.1:5672
spring.activemq.user=test
spring.activemq.password=test

是的,用户test可以访问/上的虚拟主机,是的,我可以在RabbitMQ GUI上使用test/test登录

    • 编辑**

我看了rabbitmq的日志

{handshake_error,starting,0,
             {amqp_error,access_refused,
                         "PLAIN login refused: user 'guest' - invalid credentials",
                         'connection.start_ok'}}

看起来Spring忽略了我的配置并试图连接guest

bksxznpy

bksxznpy1#

源代码/main/资源/应用程序.yml

spring:
  rabbitmq:
    username: guest
    password: guest
    host: rabbitmq
    port: 5672
    virtual-host: someVirtualHost

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html

35g0bw71

35g0bw712#

Spring属性包括RabbitMQ的特定设置。请尝试将ActiveMQ配置替换为以下内容。
示例:

spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest
7ivaypg9

7ivaypg93#

尝试在spring Boot 属性中更改您的rabbitMQ配置:

spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest
ix0qys7i

ix0qys7i4#

使用springboot的默认设置是不错的,但是如果我们想添加外部的rabbit示例到spring container,那么我们应该遵循如下步骤
application.yml

rabbitmq:
  host: 'hostname'
  vhost: 'vhostname'
  user: 'userName'
  password: 'passwd'
  port: 5672

配置类

@Configuration
    public class RabbitConfig {

        @Value("${rabbitmq.host}")
        private String host;
        @Value("${rabbitmq.vhost}")
        private String vhost;
        @Value("${rabbitmq.user}")
        private String user;
        @Value("${rabbitmq.password}")
        private String password;
        @Value("${rabbitmq.port}")
        private int port;

        @Bean
        public ConnectionFactory connectionFactory() {
            CachingConnectionFactory factory = new CachingConnectionFactory();
            System.out.println("rmqhost is " + host);
            factory.setHost(host);
            factory.setVirtualHost(vhost);
            factory.setUsername(user);
            factory.setPassword(password);
            factory.setPort(port);
            return factory;
        }

        @Bean
        public RabbitAdmin rabbitAdmin() {
            return new RabbitAdmin(connectionFactory());
        }

    }

我们可以为rabbitmqtemplate或rabbitmqListener创建Bean

相关问题