haproxy rabbitMQ 问题

环境声明:
已经安装好haproxy
已经搭建好rabbitMQ集群
win7平台

haproxy配置文件如下:

 global
 log 127.0.0.1 local0 info
 maxconn 4096
 stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
 daemon
 #pidfile /var/run/haproxy.pid
defaults
 log global
 mode http
 retries 2
 option tcplog
 option dontlognull
# option redispatch
# option abortonclose
 maxconn 2000
 timeout connect 5s
 timeout client 120s
 timeout server 120s
listen rabbitmq_cluster
 bind 0.0.0.0:5670
 mode http
 balance roundrobin
 server rabbit 127.0.0.1:5672 check inter 5000 rise 2 fall 3
 server rabbit_1 127.0.0.1:5673 check inter 5000 rise 2 fall 3
 server rabbit_2 127.0.0.1:5674 check inter 5000 rise 2 fall 3
listen private_monitoring :8100
 mode http
 option httplog
 stats enable
 stats uri /haproxy/admin
 stats auth admin:admin
 stats refresh 10s

尚敏的而配置中已经将haproxy面向前端访问的端口设置成了5670
我在本地(win7)通过telnet检测过远程haproxy的5670端口,是通的
但是,我用java客户端链接haproxy负载时,始终报连接超时
代码如下:

 package com.rabbitmq.cluster_test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;

public class Consumer extends Thread {

    private static final String EXCHANGE_NAME = "exchange_name";
    private static final String ROUTING_KEY = "routing_key";
    private static final String QUEUE_NAME = "queue_name";
    private static Connection conn = null;

    static{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.95.131");
        factory.setPort(5670);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");

        try {
            conn = factory.newConnection();
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            Channel channel = conn.createChannel();
//          channel.exchangeDeclare(EXCHANGE_NAME, "topic");
            channel.queueDeclare(QUEUE_NAME, false, true, true, null);
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(QUEUE_NAME, false, consumer);

            while(true){
                try {
                    Delivery delivery = consumer.nextDelivery();
                    String msg = new String(delivery.getBody());
                    System.out.println("msg = "+msg);

                    Long tag = delivery.getEnvelope().getDeliveryTag();
                    channel.basicAck(tag, false);
                } catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

报错为:

 java.util.concurrent.TimeoutException
    at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:76)
    at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:110)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:292)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:824)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:778)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:868)
    at com.rabbitmq.cluster_test.Consumer.<clinit>(Consumer.java:30)
    at com.rabbitmq.cluster_test.MainTest.main(MainTest.java:9)
Exception in thread "Thread-0" java.lang.NullPointerException
    at com.rabbitmq.cluster_test.Consumer.run(Consumer.java:39)
java.io.IOException
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105)
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:349)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:824)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:778)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:868)
    at com.rabbitmq.cluster_test.Publisher.<clinit>(Publisher.java:25)
    at com.rabbitmq.cluster_test.MainTest.main(MainTest.java:18)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:292)
    ... 5 more
Caused by: java.io.EOFException
    at java.io.DataInputStream.readFully(DataInputStream.java:197)
    at java.io.DataInputStream.readFully(DataInputStream.java:169)
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:116)
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:138)
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:541)
    at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-1" java.lang.NullPointerException
    at com.rabbitmq.cluster_test.Publisher.run(Publisher.java:34)
==over==
总共耗时:9.596秒
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问