下载erlang环境:http://www.erlang.org/downloads
下载rabbitMQ:http://www.rabbitmq.com/install-generic-unix.html
这里可以看一下Rabbit的文档,每个版本依赖的erlang环境也是不一样的
1.安装erlang:
# 安装依赖,主要是ncurses-devel这个依赖
yum -y install gcc glibc-devel make ncurses-devel openssl-devel xmlto perl wget
# 解压:
tar -xvf otp_src_18.3.tar.gz
#切换到解压目录
cd otp_src_18.3
# 配置环境
./configure --prefix=/usr/local/erlang
# 编译安装
make && install
2.RabbitMQ安装
#解压
xz -d rabbitmq-server-generic-unix-3.6.1.tar.xz
tar -xvf rabbitmq-server-generic-unix-3.6.1.tar
3.开放防火墙端口
firewall-cmd --permanent --add-port=15672/tcp
firewall-cmd --permanent --add-port=5672/tcp
systemctl restart firewalld.service
4.配置环境变量
vi /etc/profile
# erlang environment
ERL_HOME=/usr/local/software/otp_src_20.3
PATH=$ERL_HOME/bin:$PATH
export ERL_HOME PATH
# rabbit mq
export PATH=/usr/local/software/rabbitMQ/rabbitmq_server-3.7.4/sbin:$PATH
# 任意目录下使用erl,可以出现以下结果
[root@iZwz91i5xmdmgfhamgl6dxZ sbin]# erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1>
rabbitMQ启动
./rabbitmq-server -detached # 后端启动
rabbitmqctl status
rabbitmqctl stop
rabbitMQ添加账户
rabbitmqctl add_user admin admin #添加用户,后面两个参数分别是用户名和密码,我这都用superrd了。
# set_permissions [-p ]
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*" #添加权限
rabbitmqctl set_user_tags admin administrator #修改用户角色
网页图形化界面的使用
mkdir /etc/rabbitmq
rabbitmq-plugins enable rabbitmq_management
浏览器中访问:
http://ip:15672
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>5.0.0version>
dependency>
package com.itcloud.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitProducer {
private static final String HOST = "your ip"; //这里填写你的ip地址
private static final Integer PORT = 5672;
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
private static final String EXCHANGE_NAME = "exchange_demo";
private static final String ROUTING_KEY = "routing_key_demo";
private static final String QUEUE_NAME = "queue_demo";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST);
factory.setPort(PORT);
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
Connection connection = null;
Channel channel = null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
String message = "hello world";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
} 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();
}
}
}
}
package com.itcloud.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class RabbitConsumer {
private static final String HOST = "YOUR IP"; // 这里填写你的ip地址
private static final Integer PORT = 5672;
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
private static final String QUEUE_NAME = "queue_demo";
public static void main(String[] args) {
Address[] addresses = new Address[]{new Address(HOST, PORT)};
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
Connection connection = null;
final Channel channel;
try {
connection = factory.newConnection(addresses);
channel = connection.createChannel();
channel.basicQos(64);
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("rev message" + new String(body));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(QUEUE_NAME, consumer);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
静待后续。。。。。。。。。。。。。。。。。。