软件下载
首先去官网下载最新版本:http://www.rabbitmq.com/download.html
我下载的是:rabbitmq-server-3.6.9-1.el6.noarch.rpm版本。
还需要其他的一些依赖包需要安装:
erlang-18.1-1.el6.x86_64.rpm
rabbitmq官网上下载到合适的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
- 配置环境变量
# vi /etc/profile 在最后面增加
RABBITMQ_PATH=/usr/local/rabbitmq/sbin
# source /etc/profile
缺省配置启动(后台启动)
# /sbin/service rabbitmq-server start
# ./rabbitmq-server -detached
Starting rabbitmq-server: SUCCESS
说明
如果启动报这个错误:epmd error for host “demo”: timeout ,那么只需要修改一下hosts文件,增加你的主机名,注意,比如你的主机叫 demo.woplus,在hosts中配了 127.0.0.1 demo.woplus 也不行,你需要在hosts中再加一个 127.0.0.1 demo。
配置管理台
第一件事要创建用户,因为缺省的guest/guest用户只能在本地登录,所以先用命令行创建一个admin/admin123,并让他成为管理员。
# ./rabbitmqctl add_user admin admin123
# ./rabbitmqctl set_user_tags admin administrator
启动管理台
# ./rabbitmq-plugins enable rabbitmq_management
现在可以登录到管理台了
创建一个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 安装服务,需以管理员身份cmd执行 - 常用命令
# 服务的安装与启停
rabbitmq-service install # 同 rabbitmq-service.bat install
rabbitmq-service start
rabbitmq-service stop
# 查看mq的状态
rabbitmqctl status
#
rabbitmq-server.bat -detached
# 停止节点
rabbitmqctl stop
# 安装管理插件
rabbitmq-plugins enable rabbitmq_management # 以管理员权限运行
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();
}
}
执行结果如下:
[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 + "'");
}
}
}
执行结果如下:
[x] Received 'Hello World!'