在使用java连接rabbitmq时报如下错误:
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)
出现这种错误的情况有多种,比如guest用户访问时只允许localhost访问,或者用户名密码错误等。
我的rabbitmq运用到了haproxy和keepalived来实现高可用的负载均衡,直接通过haproxy机器的IP访问时是没问题的,但是使用keepalived的VIP来访问就有问题,后来通检查发现需要在keepalived.conf做相应的配置,keepalived.conf配置如下: MASTER上的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
}
}
#虚拟服务器定义块
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
}
}
}
BACKUP上的keepalived.conf
global_defs {
notification_email {
ric@xxx.com
}
notification_email_from cloud@xxx.com
smtp_server smtp.gildata.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 BACKUP # MASTER on haproxy1, BACKUP on haproxy2
interface ens160 #interface to monitor
virtual_router_id 51
priority 100 # 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
}
}
#虚拟服务器定义块
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.151 5670 {
weight 2
TCP_CHECK {
connect_port 5670
connect_timeout 3
}
}
}
注意其中的虚拟服务器定义块这一部分,当时就是由于缺少这一部分的配置,使得程序一直报错。 Java客户端连接的时候IPAddress使用的是VIP,port使用的是5672