RabbitMQ安装及使用
Centos安装方式
注意:Erlang语言与RabbitMQ安装版本必须匹配
RabbitMQ安装与使用
官网地址:https://www.rabbitmq.com/
提前准备:安装Linux必要依赖包
下载RabbitMQ必须安装包
配置文件修改
服务的启动:rabbitmq-server start &
服务的停止:rabbitmqctl stop_app
管理插件:rabbitmq-plugins enable rabbitmq_management
访问地址:http://ip:15672/
准备:
yum install \
build-essential openssl openssl-devel unixODBC unixODBC-devel \
make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz -y
下载:
wget www.rabbitmq.com/releases/erlang/erlang-18.3-1.el7.centos.x86_64.rpm
wget http://repo.iotti.biz/CentOS/7/x86_64/socat-1.7.3.2-5.el7.lux.x86_64.rpm
wget www.rabbitmq.com/releases/rabbitmq-server/v3.6.5/rabbitmq-server-3.6.5-1.noarch.rpm
安装:
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
rpm -ivh socat-1.7.3.2-1.1.el7.x86_64.rpm --nodeps --force
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
配置文件:
vi /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app
比如修改密码、配置等等,例如:loopback_users 中的 <<"guest">>,只保留guest
服务启动和停止:
启动 rabbitmq-server start &
停止 rabbitmqctl app_stop
查看服务是否成功:
yum install lsof
lsof -i:5672
管理插件:rabbitmq-plugins enable rabbitmq_management
访问地址:http://192.168.147.146:15672/
Docker安装
代码如下:
package com.javachz.rabbitmqapi.quickstart;
import com.rabbitmq.client.*;
/**
* @author 52hz
* @site www.javachz.com
* @company xxx公司
* @create 2020-02-28 19:31
*
* 订单微服务、短信微服务
* 接收消息的
*/
public class Consumer {
public static void main(String[] args) throws Exception {
// //连接MQ服务器
//1 创建一个ConnectionFactory, 并进行配置
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.56.138");
//5672是MQ服务器的服务端口,15672是管控台端口
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//2 通过连接工厂创建连接
Connection connection = connectionFactory.newConnection();
//3 通过connection创建一个Channel
Channel channel = connection.createChannel();
//绑定队列Queue
//4 声明(创建)一个队列
String queueName = "test001";
// //参数:队列名称,持久化与否、独占与否、无消息队列是否自动删除、消息参数
// queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
channel.queueDeclare(queueName, true, false, false, null);
// //创建消费者
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
// //设置Channel
// //指定队列中的消费的签收方式
// //参数:队列名称,自动签收,消费者回购
// // basicConsume(String queue, boolean autoAck, Consumer callback)
channel.basicConsume(queueName, true, queueingConsumer);
// //接受消息、
while(true){
//7 获取消息(Delivery:传送)
QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
String msg = new String(delivery.getBody());
System.err.println("从MQ服务器队列中接受的消息: " + msg);
}
}
}
package com.javachz.rabbitmqapi.quickstart;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @author 52hz
* @site www.javachz.com
* @company xxx公司
* @create 2020-02-28 21:39
*/
public class Procuder {
public static void main(String[] args) throws Exception {
//1 创建一个ConnectionFactory, 并进行配置
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.56.138");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//2 通过连接工厂创建连接
Connection connection = connectionFactory.newConnection();
//3 通过connection创建一个Channel
Channel channel = connection.createChannel();
//4 通过Channel发送数据
for(int i=0; i < 5; i++){
String msg = "Hello RabbitMQ!";
//1 exchange 2 routingKey
channel.basicPublish("", "test001", null, msg.getBytes());
}
//5 记得要关闭相关的连接
channel.close();
connection.close();
}
}
运行结果