下载erlang
http://www.erlang.org/downloads/19.3
解压与安装
- 安装依赖包
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel libtool libtool-ltdl-devel
- 解压
tar zxvf otp-src_19.2.tar.gz
- 配置
cd opt_src_19.3
./otp_build autoconf
./configure --prefix=/opt/erlang
- 编译与安装
make
make install
- 环境变量
sudo vim /etc/profile
ERLANG_HOME=/opt/erlang
export PATH=$PATH:$ERLANG_HOME/bin
export ERLANG_HOME
- 重新加载
source /etc/profile
- 测试
erl
安装rabbitmq
-
下载
http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.12/
解压
xz -d rabbitmq-server-generic-unix-3.6.12.tar.xz
tar -xvf rabbitmq-server-generic-unix-3.6.12.tar -C /opt
- 命名
cd /opt
mv rabbitmq_server-3.6.12 rabbitmq
- 环境变量
export PATH=$PATH:/opt/rabbitmq/sbin
export RABBITMQ_HOME=/opt/rabbitmq
** 重新加载
source /etc/profile
- web插件
rabbitmq-plugins enable rabbitmq_management
- 开放防火墙
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload
- 后台运行
rabbitmq-server -detached
-detached 参数是为了能够让rabbitmq服务以守护的方式在后台运行,不会因为当前的shell窗口关闭而影响
- 启动服务
rabbitmq 安装后默认不是以后台进程的方式启动的,要想使 rabbitmq 在系统启动时默认以后台进程的方式启动,请以管理员身份执行以下命令:
chkconfig rabbitmq-server on
- 停止
rabbitmqctl stop
-
访问
用户名密码都是guest/guest
发现登录不了
这个帐户有限制,默认只能在本地网络(localhost),远程网络访问受限,添加一个用户
- 添加用户
rabbitmqctl add_user root root
用户名密码都 是root
- 设置权限 为adminstrator
rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
- 设置root 为管理员角色
rabbitmqctl set_user_tags root administrator
-
再次访问
测试
com.rabbitmq
amqp-client
4.2.1
- 生产者
package com.ghg.mq01.producer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
public class RabbitProducer {
private static final String EXCHANGE_NAME = "exchange_demo";
private static final String ROUTING_KEY = "routingkey_demo";
private static final String QUEUR_NAME = "queue_demo";
//rabbitmq的服务地址
private static final String IP_ADDRESS = "10.18.200.199";
//RabbitMq 服务端 默认端口号为5672
private static final int PORT = 5672;
//用户名
private static final String USER_NAME = "root";
//密码
private static final String PASSWORD = "root";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
/**
* 设置ip
* port
* 用户名
* 密码
*/
connectionFactory.setHost(IP_ADDRESS);
connectionFactory.setPort(PORT);
connectionFactory.setUsername(USER_NAME);
connectionFactory.setPassword(PASSWORD);
/**
* 创建连接
*/
Connection connection = connectionFactory.newConnection();
/**
* 创建信道
*/
Channel channel = connection.createChannel();
/**
* 创建一个type=direct 持久化的 非自动删除的交换器
*/
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
/**
* 创建一个持久化 百排他的 非自动删除的队列
*/
channel.queueDeclare(QUEUR_NAME, true, false, false, null);
/**
* 将交换器与队列通过路由键绑定
*/
channel.queueBind(QUEUR_NAME, EXCHANGE_NAME, ROUTING_KEY);
/**
* 发送一条持 久化消息
*/
String message="Hello RabbitMq!";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN , message.getBytes());
/**
* 关闭资源
*/
channel.close();
connection.close();
}
}
- 消费者
package com.ghg.mq01.cunsumer;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
public class RabbitConsumer {
private static final String EXCHANGE_NAME = "exchange_demo";
private static final String ROUTING_KEY = "routingkey_demo";
private static final String QUEUR_NAME = "queue_demo";
//rabbitmq的服务地址
private static final String IP_ADDRESS = "10.18.200.199";
//RabbitMq 服务端 默认端口号为5672
private static final int PORT = 5672;
//用户名
private static final String USER_NAME = "root";
//密码
private static final String PASSWORD = "root";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Address[] addresses = new Address[] {new Address(IP_ADDRESS, PORT)};
/**
* 1.建立连接工厂
*/
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername(USER_NAME);
connectionFactory.setPassword(PASSWORD);
/**
* 2.创建连接 和生产者有一点不同
*/
Connection connection = connectionFactory.newConnection(addresses);
/**
* 3.创建信道
*/
final Channel channel = connection.createChannel();
/**
* 4.设置客户端最多接收示被ack的消息个数
*/
channel.basicQos(64);
Consumer consumer =new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
System.out.println("接收消息 : "+new String(body));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
/**
* 回调
*/
channel.basicConsume(QUEUR_NAME, consumer);
/**
* 关闭资源
*/
TimeUnit.SECONDS.sleep(5);
channel.close();
connection.close();
}
}