RabbitMQ连接被拒绝

问题描述 投票:0回答:1

我无法鸣叫RabbitMQ。RabbitMQ不在本地计算机上。

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;

@Configuration
@EnableRabbit
public class AMQPConfig {

    @Autowired
    private RabbitMQProperties rabbitMQProperties;

    @Bean
    Queue queue() {
        return new Queue(rabbitMQProperties.getQueueName(), false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(rabbitMQProperties.getExchangeName());
    }



    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(rabbitMQProperties.getRoutingKey());
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        System.out.println( " ques name is ------- "  + rabbitMQProperties.getQueueName() );
        container.setQueueNames(rabbitMQProperties.getQueueName());
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
        return new MappingJackson2MessageConverter();
    }

    @Bean
    public RabbitTemplate amqpTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(RabbitMqListener listener) {
        return new MessageListenerAdapter(listener, "listen");
    }

}

我的application.properties看起来像

spring.rabbitmq.password=pass
spring.rabbitmq.port=15671
spring.rabbitmq.username=user
spring.rabbitmq.host=https://urltologinscreen

我可以使用来访问Rabbitmq guihttps://urltologinscreen:15671

我收到以下错误

ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为bean的错误在类路径资源[pathhidden / rabbitmq / AMQPConfig.class]中定义的“容器”:不满意的依赖性通过表达方法“容器”参数0;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.springframework.amqp.rabbit.connection.ConnectionFactory'的合格bean可用:预期至少有1个有资格作为自动装配候选者的bean。依赖注释:{}

  Parameter 0 of method amqpTemplate in pathhidden.rabbitmq.AMQPConfig required a bean of type

'org.springframework.amqp.rabbit.connection.ConnectionFactory'找不到。

我该如何解决此问题?

spring spring-boot rabbitmq
1个回答
0
投票

我的猜测是,您的属性应如下所示:

spring.rabbitmq.password=pass
spring.rabbitmq.username=user
spring.rabbitmq.host=urltologinscreen

不需要指定端口或在主机中使用http

© www.soinside.com 2019 - 2024. All rights reserved.