Dashuang Du.
Dashuang Du.

study and practice, years of it.

RabbitMQ 安装与部署

2018, Jun 19    

本文档介绍RabbitMQ安装,部署,及简要使用(以ubuntu 16.04为例)

官方文档

简介

RabbitMQ是一套开源(MPL)的消息队列服务软件,是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开源实现,由以高性能、 健壮以及可伸缩性出名的 Erlang 写成,支持几乎已知的所有后端语言。

安装与部署

安装 Erlang

  1. Adding repository entry

     wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
    
     sudo dpkg -i erlang-solutions_1.0_all.deb
    

    Alternatively: adding the repository entry manually

    Add following line to your /etc/apt/sources.list

     deb https://mirrors.tuna.tsinghua.edu.cn/erlang-solutions/ubuntu/ xenial contrib
    

    lock erlang version, because of rebbitmq server does not support erlang 1.21

     vi /etc/apt/preferences.d/erlang
        
     Package: erlang* esl-erlang
     Pin: version 1:20.3*
     Pin-Priority: 1000
    

    To verify which distribution you are running, run lsb_release -c in console.

    Next, add the Erlang Solutions public key for “apt-secure” using following commands:

     wget https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc
    
     sudo apt-key add erlang_solutions.asc
    
  2. Installing Erlang

     sudo apt-get update
    
     sudo apt-get install erlang
    

安装 RabbitMq Server

echo "deb https://dl.bintray.com/rabbitmq/debian xenial main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list
wget -O- https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install rabbitmq-server

简单使用(以 php 为例)

  • 安装依赖包

    注意,需要 php 的mbstringbcmath扩展

      composer require php-amqplib/php-amqplib
    
  • 创建一个生产者(send.php

      <?php
      require_once __DIR__ . '/vendor/autoload.php';
      use PhpAmqpLib\Connection\AMQPStreamConnection;
      use PhpAmqpLib\Message\AMQPMessage;
        
      $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
      $channel = $connection->channel();
        
      $channel->queue_declare('hello', false, false, false, false);
        
      $msg = new AMQPMessage('Hello World!');
      $channel->basic_publish($msg, '', 'hello');
        
      echo " [x] Sent 'Hello World!'\n";
        
      $channel->close();
      $connection->close();
    
  • 创建一个消费者(receive.php

      <?php
      require_once __DIR__ . '/vendor/autoload.php';
      use PhpAmqpLib\Connection\AMQPStreamConnection;
        
      $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
      $channel = $connection->channel();
        
      $channel->queue_declare('hello', false, false, false, false);
        
      echo " [*] Waiting for messages. To exit press CTRL+C\n";
        
      $callback = function ($msg) {
        echo ' [x] Received ', $msg->body, "\n";
      };
        
      $channel->basic_consume('hello', '', false, true, false, false, $callback);
        
      while (count($channel->callbacks)) {
          $channel->wait();
      }
    
  • 启动 RabbitMQ 服务

      sudo service rabbitmq-server start
    
      php send.php
    
      php receive.php
    

启动 Web 控制台

sudo rabbitmq-plugins enable rabbitmq_management

访问 http://yourhost:15672

添加用户并设置权限

sudo rabbitmqctl add_user <username> <password>

// 设置标签
sudo rabbitmqctl set_user_tags <username> <tag>

// 查看用户列表
sudo rabbitmqctl list_users

添加 Virtual Hosts :    
sudo rabbitmqctl add_vhost <vhost>    
    
删除 Virtual Hosts :    
sudo rabbitmqctl delete_vhost <vhost>  

// 设置权限
sudo rabbitmqctl set_permissions [-p <vhost>] <user> <conf> <write> <read> 

// 查看权限
sudo rabbitmqctl list_user_permissions <username>

sudo rabbitmqctl list_permissions -p <vhost>

// 清除权限
sudo rabbitmqctl clear_permissions [-p VHostPath] <username>

// 删除用户
sudo rabbitmqctl delete_user <username>

// 修改用户密码
sudo rabbitmqctl change_password <username> <new_password>

for example:

// 创建一个 admin password 的账户
sudo rabbitmqctl add_user admin password


// 赋予 admin 用户 管理员角色
sudo rabbitmqctl set_user_tags admin administrator

// 设置 admin 在 '/' 这个vhost中拥有所有权限
sudo rabbitmqctl  set_permissions -p / admin '.*' '.*' '.*'