-
下载erlang:http://www.erlang.org/downloads
RabbitMQ服务端使用并发式语言erlang编写
-
创建
ERLANG_HOME
环境变量
-
验证erlang安装
输入
erl
出现erlang的版本信息表示erlang语言环境安装成功 -
下载RabbitMQ:http://www.rabbitmq.com/
默认安装目录:
C:/Program Files/....
,这个目录中是存在空格符的
RabbitMQ安装目录最好不要有空格,可以改变安装目录 -
启动RabbitMQ
- 进入RabbitMQ的安装目录: cd rabbitmq_server-3.5.2
- 启动服务:sbin\rabbitmq-service start
验证RabbitMQ安装:sbin\rabbitmqctl status , 如果出现以下日志,说明安装成功,并且RabbitMQ Server已经启动运行正常。
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin>rabbitmqctl status Status of node 'rabbit@Lifeix-pc' ... [{pid,9788}, {running_applications,[{rabbit,"RabbitMQ","3.6.5"}, {mnesia,"MNESIA CXC 138 12","4.12.1"}, {os_mon,"CPO CXC 138 46","2.2.15"}, {rabbit_common,[],"3.6.5"}, {xmerl,"XML parser","1.3.7"}, {ranch,"Socket acceptor pool for TCP protocols.", "1.2.1"}, {sasl,"SASL CXC 138 11","2.4"}, {stdlib,"ERTS CXC 138 10","2.1"}, {kernel,"ERTS CXC 138 10","3.0.1"}]}, {os,{win32,nt}}, {erlang_version,"Erlang/OTP 17 [erts-6.1] [64-bit] [smp:4:4] [async-threads:64]\n"}, {memory,[{total,48083008}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,0}, {queue_procs,2704}, {queue_slave_procs,0}, {plugins,0}, {other_proc,22122752}, {mnesia,61640}, {mgmt_db,0}, {msg_index,51952}, {other_ets,979696}, {binary,23264}, {code,19851942}, {atom,711569}, {other_system,4277489}]}, {alarms,[]}, {listeners, [{clustering,25672,"::"},{amqp,5672,"::"},{amqp,5672,"0.0.0.0"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,6851372646}, {disk_free_limit,50000000}, {disk_free,1131212800}, {file_descriptors,[{total_limit,8092}, {total_used,2}, {sockets_limit,7280}, {sockets_used,0}]}
-
安装 RabbitMQ-Plugins
相当于管理界面,方便在浏览器界面查看RabbitMQ各个消息队列以及exchange的工作情况
- 进入rabbitmq的sbin目录
- 输入:rabbitmq-plugins enable rabbitmq_management 命令,稍等会出现plugins安装成功的提示,默认是安装6个插件
-
如果出现了下面的错误:
解决方法:
1. 命令行输入:sbin\rabbitmq-service stop
停止服务
2. 输入sbin\rabbitmq-service remove
3. 输入sbin\rabbitmq-service install
安装服务
4. 输入sbin\rabbitmq-service start
启动服务
5. 重新输入sbin\rabbitmq-plugins enable rabbitmq_management
-
验证
插件安装完后,在浏览器输入
http://localhost:15672
验证用户名:guest,密码:guest
RabbitMQ中涉及到的几个概念
-
producer:消息生产者
-
consumer:消息消费者
-
virtual host:虚拟主机
在RabbitMQ中,用户只能在虚拟主机的层面上进行一些权限设置,比如我可以访问哪些队列,我可以处理哪些请求等等;
-
broker:消息转发者
- 转发规则
- exchange:交换机
和producer直接进行打交道的,类似于路由器的功能,主要就是进行转发操作,producer到底用哪个exchange进行路由?取决于routing key(路由键),每个消息都有这个键(可以设定)其实就是一字符串
- queue:消息队列
用于存放消息,接收exchange路由过来的消息,可以对队列内容进行持久化操作,queue接收哪个exchange路由的消息?需要用到binding key(绑定键),绑定键会将队列和exchange进行绑定,RabbitMQ提供了多种绑定方式
- exchange:交换机
- 转发规则
-
这些概念间的关系
RabbitMQ简单使用
producer(生产者)端步骤:
- 创建ConnectionFactory,设置一些参数,如hostname,portNumber等
- 利用ConnectionFactory创建一个Connection连接
- 利用Connection创建一个Channel通道
- 创建queue和Channel进行绑定
- 创建消息,发送到队列中
注意,当前没有用到exchange交换机,我们没有创建exchange时,RabbitMQ会创建一个默认的空字符串(exchange)并使用这个exchange
public class Sender {
private final static String QUEUE_NAME = "MyQueue";
public static void main(String[] args) {
send();
}
public static void send()
{
ConnectionFactory factory = null;
Connection connection = null;
Channel channel = null;
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "my first message .....";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println("已经发送消息....."+message);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally{
try {
//关闭资源
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
}
consumer(消费者)端步骤:
- 创建ConnectionFactory,设置参数,如hostname,portNumber等
- 利用ConnectionFactory创建一个Connection连接
- 利用Connection创建一个Channel通道
- 将queue和Channel进行绑定
注意这里的queue名字要和前面producer创建的queue一致
- 创建消费者Consumer来接收消息,同时将消费者和queue进行绑定
public class Receiver {
private final static String QUEUE_NAME = "MyQueue";
public static void main(String[] args) {
receive();
}
public static void receive()
{
ConnectionFactory factory = null;
Connection connection = null;
Channel channel = null;
try {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
System.out.println("11111111111");
String message = new String(body, "UTF-8");
System.out.println("收到消息....."+message);
}};
channel.basicConsume(QUEUE_NAME, true,consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally{
try {
//关闭资源
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
}