以下のVagrantfileを使用してRabbitMQ、Management Web interface、Anaconda、pikaがインストールされた仮想マシンを構築できます。
仮想マシンの構築と同時にpikaを使用したメッセージを送信(put-helloworld.py)・受信(get-helloworld.py)を行うサンプルも実行します。
Management Web interfaceには、http://192.168.55.102:15672/にユーザ名rabbitmq、パスワードrabbitmqでアクセスします。
Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "bento/ubuntu-16.04"
config.vm.hostname = "ub1604rabbitmq"
config.vm.provider :virtualbox do |vbox|
vbox.name = "ub1604rabbitmq"
vbox.cpus = 2
vbox.memory = 1024
vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
end
config.vm.network "private_network", ip: "192.168.55.102", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.102", :netmask => "255.255.255.0"
config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
echo 'deb http://www.rabbitmq.com/debian/ testing main' | tee /etc/apt/sources.list.d/rabbitmq.list
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
apt-get -y install rabbitmq-server
rabbitmq-plugins enable rabbitmq_management
systemctl enable rabbitmq-server
systemctl restart rabbitmq-server
# setup management web interface
rabbitmqctl add_user rabbitmq rabbitmq
rabbitmqctl set_user_tags rabbitmq administrator
rabbitmqctl set_permissions -p / rabbitmq ".*" ".*" ".*"
# install anaconda & pika
wget https://repo.continuum.io/archive/Anaconda3-4.4.0-Linux-x86_64.sh
chmod +x Anaconda3-4.4.0-Linux-x86_64.sh
./Anaconda3-4.4.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install pika
# execute a sample program to put a message.
cat << EOF > put-helloworld.py
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" put 'Hello World!'")
EOF
python put-helloworld.py
# exeucte a sample program to get a message
cat << EOF > get-helloworld.py
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
method_frame, header_frame, body = channel.basic_get('hello')
if method_frame:
print(method_frame, header_frame, body)
channel.basic_ack(method_frame.delivery_tag)
else:
print('no message')
EOF
python get-helloworld.py
echo 'management web interface : http://192.168.55.102:15672/';
echo 'user : rabbitmq password rabbitmq'
SHELL
end
Management Web Interfaceのログイン画面
Management Web Interfaceのログオン後の画面
○関連情報
・RabbitMQに関する他の記事は
こちらを参照してください。