RabbitMQ 入门

时间:2022-12-14 03:14:39
简介
 
RabbitMQ是一个Message Broker,核心思想就是接受消息,转发消息。
实现的协议:AMQP。
 
术语(Jargon)
 
P,Producing,制造和发送信息的一方。
Queue,消息队列。
C,Consuming,接收消息的一方。
 

Simple Demo
 
RabbitMQ 入门
发送方
 
    ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); //(如果没有就)创建Queue
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());//以byte的方式发布
System.out.println(" [x] Sent '" + message + "'"); channel.close();
connection.close();
接收方
 
    ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null);//看一下Queue是否存在 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 + "'");
}
 

订阅/发布Demo
 
发送消息给多个订阅者
核心思想:消息发送给exchange,每个接收方创建匿名Queue绑定到exchange,exchange发送消息给每个接收方。
 
Exchanges
 
在RabbitMQ完整的模型中,消息只能发送给一个exchange。
exchange一方面接收消息,另一方面push给queues。
 
RabbitMQ 入门
exchange类型
> rabbitmqctl list_exchanges
direct
topic
headers
fanout 广播消息给已知队列
 
RabbitMQ 入门
发送方
 
String EXCHANGE_NAME = "logs";

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 设置 exchange 类型
channel.exchangeDeclare(EXCHANGE_NAME /*exchange名称*/, "fanout"/*类型*/);
// 发布消息时,指定 exchange 名称
channel.basicPublish( EXCHANGE_NAME , "", null, message.getBytes());
channel.close();
connection.close();
 
接收方(可多个同时运行)
 
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 设置exchange名称和类型
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
// 创建一个临时的、带有随机名称的Queue,用来与 exchange 绑定
String queueName = channel.queueDeclare().getQueue();
hannel.queueBind(queueName, EXCHANGE_NAME, ""); // 绑定
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
}
 

Install
 
@Windows
1 先安装Erlang。
2 官方网下载 .exe。
 

管理
 
命令行管理
http://www.rabbitmq.com/man/rabbitmqctl.1.man.html
 
WebUI管理
> rabbitmq-plugins enable rabbitmq_management
重启后访问 http://localhost:15672/
guest,guest
 
角色
management
policymaker
monitoring
administrator
 
添加用户并分配角色
> rabbitmqctl add_user name pass
> rabbitmqctl set_user_tags name administrator
 
插件管理
启用插件
> rabbitmq-plugins enable plugin-name
 
 
配置文件
etc\rabbitmq.config
 

Refs