Rabbit MQ org.springframework.amqp.AmqpIOException:java.net.UnknownHostException

时间:2019-03-18 19:21:25

标签: spring-boot spring-amqp

我面临以下代码的未知主机异常。我不确定代码有什么问题。


@Component
@Configuration
public class RabbitListenerContainerFactory {

    static final Logger logger = LoggerFactory.getLogger(RabbitListenerContainerFactory.class);

    @Autowired
    RabbitMqConfig rabbitMqConfig;

    @Autowired
    EPPQ2Subscriber receiver;

    @Autowired
    EPPQ2ChanelAwareSubscriber receiverChanel;

     public RabbitListenerContainerFactory(ConfigurableApplicationContext ctx) {
        printContainerStartMsg();
    }
    private void printContainerStartMsg() {
        logger.info("----------- Scrubber Container Starts   --------------");
    }

    @Bean
    public SimpleMessageListenerContainer queueListenerContainer(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) { 
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueueNames(rabbitMqConfig.getSubscriberQueueName());
       // container.setQueueNames("SampleQueue"); /*This just for testing.. !*/
        container.setMessageListener(listenerAdapter);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setDeclarationRetries(5);// This is default to 3, We can twick this and move this to prop
        container.setPrefetchCount(100); //Tell the broker how many messages to send to each consumer in a single request.
        return container;
    }

     @Bean
     MessageListenerAdapter listenerAdapter(EPPQ2Subscriber receiver) {
         return new MessageListenerAdapter(receiver, "receiveMessage");
     }

     /*@Bean
     MessageListenerAdapter listenerAdapterWithChanel(EPPQ2ChanelAwareSubscriber receiverChanel) {
         return new MessageListenerAdapter(receiverChanel);
     }*/

     @Bean
     ConnectionFactory connectionFactory () {
         final CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
         connectionFactory.setAddresses(Arrays.toString(rabbitMqConfig.getSubscriberHosts()));
            //connectionFactory.setHost(Arrays.toString(rabbitMqConfig.getSubscriberHosts()));
            connectionFactory.setVirtualHost("hydra.services");
            connectionFactory.setPort(rabbitMqConfig.getSubscriberPort());
            connectionFactory.setUsername(rabbitMqConfig.getSubscriberUsername());
            connectionFactory.setPassword(rabbitMqConfig.getSubscriberPassword());
         return connectionFactory;
     }
}

我能够看到兔子在主机上运行 我可以在此处http://hostname:15672/看到管理控制台 电话网正在工作 Telnet主机名15672
超时3 bash -c'cat / dev / tcp / hostname / 15672';回显$?


这是日志:

org.springframework.amqp.AmqpIOException: java.net.UnknownHostException:host-name   

编辑1

    public class RabbitMqConfig {

    private String [] subscriberHosts;
    private int subscriberPort;

    private String [] publisherHosts;
    private int publisherPort;

    private String subscriberUsername;
    private String subscriberPassword;

    private String publisherUsername;
    private String publisherPassword;

    private String subscriberQueueName;
    private String publisherQueueName;
    private String publisherTopic;
    private String routingKey;

    /**
     * 
     * @return -read queue hosts
     */
    public String[] getSubscriberHosts() {
        return subscriberHosts;
    }


    /**
     * 
     * @return -read queue port
     */
    public int getSubscriberPort() {
        return subscriberPort;
    }


    /**
     * 
     * @return -write queue hosts
     */
    public String[] getPublisherHosts() {
        return publisherHosts;
    }


    /**
     * 
     * @return -write queue port
     */
    public int getPublisherPort() {
        return publisherPort;
    }


    /**
     * 
     * @return -Read Queue user name
     */
    public String getSubscriberUsername() {
        return subscriberUsername;
    }


    /**
     * 
     * @return Read Queue password
     */
    public String getSubscriberPassword() {
        return subscriberPassword;
    }


    /**
     * 
     * @return -Write Queue user name
     */
    public String getPublisherUsername() {
        return publisherUsername;
    }


    /**
     * 
     * @return  -Write Queue password
     */
    public String getPublisherPassword() {
        return publisherPassword;
    }


    /**
     * 
     * @return  -Write Queue name
     */
    public String getSubscriberQueueName() {
        return subscriberQueueName;
    }


    /**
     * 
     * @return  --Read Queue name
     */
    public String getPublisherQueueName() {
        return publisherQueueName;
    }

    /**
     * 
     * @return
     */
    public String getPublisherTopic() {
        return publisherTopic;
    }

    /**
     * 
     * @return
     */
    public String getRoutingKey() {
        return routingKey;
    }

@PostConstruct
    public void getAIMCredentails() {
        tempApiCallMock();
    }


    private void tempApiCallMock() {
        String [] host = {"10.30.190.25"};
        this.subscriberHosts = host;
        this.subscriberPort = 5672;
        this.subscriberQueueName = "hydra.Syphon.q1";
        this.subscriberUsername = "dftp_subscriber";
        this.subscriberPassword = "dftp_subscriber";
        this.publisherHosts = host; 
        this.publisherPort = 5672;
        this.publisherUsername = "dftp_publisher";
        this.publisherPassword = "dftp_publisher";
    }

感谢纠正我,我将端口更改为5672,但仍然是同一问题。 和我的telnet主机5672可以工作

1 个答案:

答案 0 :(得分:0)

mvn

是错误的。 connectionFactory.setAddresses(Arrays.toString(rabbitMqConfig.getSubscriberHosts()));期望使用逗号分隔的字符串,而不是数组的字符串表示形式。

查看其JavaDocs:

setAddresses()