【云计算】RabbitMQ安装及Possibly caused by authentication failure

0x01 安装环境

Ubuntu 14.04 64位

0x02 安装

1.进入文件/etc/apt/sources.list
添加如下内容

deb http://www.rabbitmq.com/debian/ testing main

2.添加public key,在命令行中输入

wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc

3.在命令行中输入

sudo apt-key add rabbitmq-signing-key-public.asc

4.安装RabbitMQ

sudo apt-get install rabbitmq-server

在Rabbitmq安装过程中,需要用到erlang,使用以上方法安装时,都可以自动安装这些必要的包

0x03.RabbitMQ常用命令

重启

/etc/init.d/rabbitmq-server restart

启动

/etc/init.d/rabbitmq-server start

停止

/etc/init.d/rabbitmq-server stop

查看消息队列里面内容

rabbitmqctl list_queues

查看rabbitmq服务状态信息

rabbitmqctl -q status

0x04.依赖jar包

amqp-client-2.2.0.jar
commons-cli-1.1.jar
commons-io-1.2.jar
hamcrest-core.jar
junit.jar

0x05 本机启动示例程序

本机程序指的是程序代码运行的机器与RabbitMQ Server安装的机器在同一台机器上,则其指向RabbitMQ时只需要指明localhost即可
官方给出代码如下:
Send端源码如下

import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitMQSend {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }
}


Receive端源代码如下:

import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
public class RabbitMQRecv {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException, ShutdownSignalException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }
}

0x06 远程访问示例程序

远程访问是指运行代码的机器与RabbitMQ Server安装的机器不在同一台机器上,则会遇到第6小节中出现的问题,如果出现,则按照第6小节的解决办法一步步解决后运行程序,可以使其实现远程访问。
Send端源码如下:

import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class RabbitMQSend {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("47.89.44.75");
        factory.setUsername("root");
        factory.setPassword("123456");
        factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }
}

Receive端源码如下:


import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
public class RabbitMQRecv {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException, ShutdownSignalException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("47.89.44.75");
        factory.setUsername("root");
        factory.setPassword("123456");
        factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }
}

0x07 Possibly caused by authentication failure解决方案

在远程机器上安装上RabbitMQ后,然后会一直出现认证失败的错误,即Possibly caused by authentication failure。此错误的导致原因是账号密码错误。官网上给的例子都是在本地使用系统默认的guest用户连接的,而没有给出远程连接的例子。对于这个guest用户是刚刚安装好rabbitmq-server时候,系统自动创建的一个名为“/”的virtual host,同时也会创建一个用户名和密码都是guest的用户,并且拥有“/ virtual host”的所有访问权限。并且此guest只是允许从localhost访问。所以在本机上运行官方示例是没问题的,一旦切换到远程机器访问的话,单纯的修改localhost字段是不行的。
这里的解决办法是新建一个对rabbitmq具有所有权限的用户,然后用此用户的信息进行远程访问。
解决办法如下:

rabbitmqctl add_user root 123456 
rabbitmqctl set_user_tags root administrator 
rabbitmqctl set_permissions -p / root ".*" ".*" ".*"

你可能感兴趣的:(云计算)