1 安装
1.1 首先 arbbitmq是用爱尔兰这种语言去编写的,所以,需要这种语言支持,那就需要下载以下几个包去安装来搭建环境
下载并安装erlang,http://www.erlang.org/download.html 如果自己的系统是windows 64位的就下载最下面的那个,如果是三十二位的则下载另外一个
1.2 配置环境变量
将你的安装好的目录的bin目录增加到path中,我的是安装到了 D:\Program Files\erl5.10.3\bin
1.3 .下载RabbitMQ
http://www.rabbitmq.com/releases/rabbitmq-server/v3.1.5/rabbitmq-server-3.1.5.zip
1.4.解压RabbitMQ的zip包,运行sbin/rabbitmq-server.bat,启动RabbitMQ服务器。
2 RabbitMQ服务端是用AMPQ协议的, 而客户端支持多种语言(Java, .NET, C/C++,Erlang......)。下面我们准备用java来写一个hello world,测试RabbitMQ是否安装OK。
2.1.先要去http://www.rabbitmq.com/java-client.html下载java的客户端,rabbitmq-java-client-bin-2.8.1.zip。解压把里面的3个jar包拷贝到我们的java工程吧。
代码如下:
发送类:
- package com.abin.test;
- import java.io.IOException;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class Send {
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] args) throws IOException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- 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();
- }
- }
接收类:
- package com.abin.test;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.QueueingConsumer;
- public class Reqv {
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- 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 + "'");
- }
- }
- }
2.2 测试 先运行Reqv,再运行Send两次,看到如下消费者能够接受到2条消息了,表明RabbitMQ的环境OK了。
3 Rabbit还自带监控功能.
3.1. cmd进到sbin目录,键入rabbitmq-plugins enable rabbitmq_management启用监控管理,然后重启Rabbitmq服务器。 (此处可以到windows 的服务中找到rabbitmq 服务进行重启)
8.2. 打开网址http://localhost:15672,用户名和密码都是guest。
8.3. 这次我们关掉Receive,再次运行Send,然后点击管理网页上的Queue。
点击hello进去,并点击Get Message,可以看到Hello World消息的确已经传送到RabbitMQ的服务器端了
参考:http://www.cnblogs.com/astroboyx/archive/2012/04/09/2739902.html
https://i-blog.csdnimg.cn/blog_migrate/af91a5d526214a3472b2e8bbcc677ad7.png