无法使用Java客户端通过keepalived VIP向Rabbit集群发送消息

时间:2018-04-24 05:18:04

标签: java rabbitmq haproxy

我已经通过haproxy和keepalive创建了一个高可用的rabbitmq集群。我可以通过haproxy或直接通过rabbitmq向rabbimq集群发送消息。但当 我使用keepalived VIP,这是错误的。我得到一些像这样的例外:

Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:339)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:813)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:767)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:857)
at com.gildata.RabbitProducer.getConnection(RabbitProducer.java:37)
at com.gildata.RabbitProducer.publish1(RabbitProducer.java:42)
at com.gildata.RabbitProducer.main(RabbitProducer.java:27)

我可以通过VIP获得Ping,任何人都可以通过连接VIP告诉我如何向rabbitmq发送消息,非常感谢。这是我的代码。

package com.gildata;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeoutException;

public class RabbitProducer {
    private static final String EXCHANGE_NAME = "exchange_demo";
    private static final String ROUTING_KEY = "routingkey_demo";
    private static final String QUEUE_NAME = "queue_demo";

    //    private static final String IP_ADDRESS = "10.1.12.146";
    //    private static final int PORT = 5670; //146 haprox
    private static final String IP_ADDRESS = "10.1.12.200"; //keepalived VIP
    private static final int PORT = 5672; 


public static void main(String args[]) throws IOException, TimeoutException {
    publish1();
}

private static Connection getConnection() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(IP_ADDRESS);
    factory.setVirtualHost("/");
    factory.setPort(PORT);
    factory.setUsername("testuser");
    factory.setPassword("123456");
    return factory.newConnection();
}

private static void publish1() throws IOException, TimeoutException {
    Connection connection = getConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_NAME,"direct",true,false,null);
    channel.queueDeclare(QUEUE_NAME,true,false,false,null);
    channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY);
    //send a message:hello world!
    String message = "hello world haproxy test2!";
    channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN,
            message.getBytes());
    channel.close();
    connection.close();
}

}

1 个答案:

答案 0 :(得分:0)

最后,我自己解决了这个问题。我发现我需要在配置keepalived.conf中进行一些配置。这是配置:

global_defs {
    notification_email {
            ric@xxx.com
    }   
    notification_email_from cloud@xxx.com
    smtp_server smtp.xxx.com  # NAU Mail Relay Server
    smtp_connect_timeout 300 
    router_id NodeA
    vrrp_skip_check_adv_addr
    vrrp_garp_interval 0
    vrrp_gna_interval 0
}


vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 5
    weight 2
}

vrrp_instance VI_1 {
    state MASTER # MASTER on haproxy1, BACKUP on haproxy2
    interface ens160 #interface to monitor
    virtual_router_id 51
    priority 101 # 101 on haproxy1, 100 on haproxy2
    advert_int 1
    smtp_alert # Activate SMTP notifications
    authentication {
            auth_type PASS
            auth_pass 1111
    }
    track_script {
            chk_haproxy
    }
    track_interface {
            ens160
    }
    virtual_ipaddress {
            10.1.12.200 #virtual ip address
    }
}

#this configuration can solve the problem
virtual_server 10.1.12.200 5672 {
    delay_loop 30
    lb_algo wrr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 10.1.12.146 5670 {
            weight 2
            TCP_CHECK {
                    connect_port 5670
                    connect_timeout 3
            }
    }
}