軟件下載
首先去官網(wǎng)下載最新版本:http://www.rabbitmq.com/download.html
我下載的是:rabbitmq-server-3.6.9-1.el6.noarch.rpm版本。
還需要其他的一些依賴包需要安裝:
erlang-18.1-1.el6.x86_64.rpm
rabbitmq官網(wǎng)上下載到合適的RPM包舷嗡,http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el6.x86_64.rpm烂斋。
安裝
使用如下命令安裝
# rpm -ihv erlang-18.1-1.el6.x86_64.rpm
# rpm -ihv rabbitmq-server-3.6.9-1.el6.noarch.rpm
下載安裝包(tar)方式
- 解壓
tar -xvf rabbitmq-server-generic-unix-3.8.9.tar
- 配置環(huán)境變量
# vi /etc/profile 在最后面增加
RABBITMQ_PATH=/usr/local/rabbitmq/sbin
# source /etc/profile
缺省配置啟動(后臺啟動)
# /sbin/service rabbitmq-server start
# ./rabbitmq-server -detached
Starting rabbitmq-server: SUCCESS
說明
如果啟動報(bào)這個錯誤:epmd error for host “demo”: timeout 流部,那么只需要修改一下hosts文件适篙,增加你的主機(jī)名张弛,注意鹃两,比如你的主機(jī)叫 demo.woplus草添,在hosts中配了 127.0.0.1 demo.woplus 也不行驶兜,你需要在hosts中再加一個 127.0.0.1 demo。
配置管理臺
第一件事要創(chuàng)建用戶远寸,因?yàn)槿笔〉膅uest/guest用戶只能在本地登錄抄淑,所以先用命令行創(chuàng)建一個admin/admin123,并讓他成為管理員驰后。
# ./rabbitmqctl add_user admin admin123
# ./rabbitmqctl set_user_tags admin administrator
啟動管理臺
# ./rabbitmq-plugins enable rabbitmq_management
現(xiàn)在可以登錄到管理臺了
創(chuàng)建一個mqtest用戶肆资,用于后面Java Client使用。
Window安裝
- 下載
下載地址:http://www.rabbitmq.com/download.html - 安裝
雙擊下載后的.exe文件灶芝,安裝過程與erlang的安裝過程相同
3.安裝RabbitMQ-Plugins
RabbitMQ安裝好后接下來安裝RabbitMQ-Plugins郑原。打開命令行cd唉韭,輸入RabbitMQ的sbin目錄
rabbitmq-plugins enable rabbitmq_management
4.rabbitmq-service.bat install 安裝服務(wù),需以管理員身份cmd執(zhí)行 - 常用命令
# 服務(wù)的安裝與啟停
rabbitmq-service install # 同 rabbitmq-service.bat install
rabbitmq-service start
rabbitmq-service stop
# 查看mq的狀態(tài)
rabbitmqctl status
#
rabbitmq-server.bat -detached
# 停止節(jié)點(diǎn)
rabbitmqctl stop
# 安裝管理插件
rabbitmq-plugins enable rabbitmq_management # 以管理員權(quán)限運(yùn)行
Java Client
我用Maven新建一個工程犯犁,修改POM文件纽哥,增加如下:
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.6</version>
</dependency>
</dependencies>
編寫Sender類
package com.zhanghf;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Sender {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("mqtest");
factory.setPassword("mqtest");
factory.setHost("172.16.0.153");
factory.setVirtualHost("/");
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();
}
}
執(zhí)行結(jié)果如下:
[x] Sent 'Hello World!'
再編寫Consumer類
package com.zhanghf;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
public class Consumer {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("mqtest");
factory.setPassword("mqtest");
factory.setHost("172.16.0.153");
factory.setVirtualHost("/");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
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 + "'");
}
}
}
執(zhí)行結(jié)果如下:
[x] Received 'Hello World!'