RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.
The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data‒messages.
RabbitMQ是一个消息代理:它接受和转发消息。你可以把它想象成一个邮局:当你把你想要寄的邮件放到一个邮箱里,你可以确定邮递员先生或女士最终会把邮件送到你的收件人那里。在这个类比中,RabbitMQ是一个邮箱、一个邮局和一个邮递员。RabbitMQ和邮局之间的主要区别在于它不处理纸张,而是接受、存储和转发二进制数据消息块。
RabbitMQ, and messaging in general, uses some jargon.
RabbitMQ和消息传递通常使用一些术语。
- Producing means nothing more than sending. A program that sends messages is a producer :
- 生产无非就是发送。发送消息的程序是一个生产者
- A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:
- 队列是RabbitMQ内部邮箱的名称。尽管消息通过RabbitMQ和应用程序传递,但它们只能存储在队列中。队列只受主机内存的限制。磁盘限制,它本质上是一个大的消息缓冲区。许多生产者可以发送到一个队列的消息,而许多消费者可以尝试从一个队列接收数据。这就是我们表示队列的方式
- Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:
- “消费”和“接受”有相似的含义。消费者是一个主要等待接收消息的程序
Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don't. An application can be both a producer and consumer, too.
请注意,生产者、消费者和代理不必驻留在同一主机上;实际上,在大多数应用程序中,它们不会。应用程序也可以既是生产者又是消费者。
In this part of the tutorial we'll write two programs in Java; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the Java API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging. In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.
在本教程的这一部分,我们将用Java编写两个程序;发送单个消息的生产者和接收消息并将其打印出来的消费者。我们将忽略Java API中的一些细节,只关注这个非常简单的东西作为开始。这是一个“你好世界”的消息传递。在下图中,“P”是我们的生产者,“C”是我们的消费者。中间的盒子是一个队列——RabbitMQ代表使用者保存的消息缓冲区。
![(P) -> [|||] -> (C)](https://p3-juejin.byteimg.com...
The Java client library
RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the Java client provided by RabbitMQ.
Download the client library and its dependencies (SLF4J API and SLF4J Simple). Copy those files in your working directory, along the tutorials Java files.
Please note SLF4J Simple is enough for tutorials but you should use a full-blown logging library like Logback in production.
(The RabbitMQ Java client is also in the central Maven repository, with the groupId com.rabbitmq and the artifactId amqp-client.)
Now we have the Java client and its dependencies, we can write some code.
Sending 发送
![(P) -> [|||]](https://p3-juejin.byteimg.com...
We'll call our message publisher (sender) Send and our message consumer (receiver) Recv. The publisher will connect to RabbitMQ, send a single message, then exit.
我们将调用消息发布者(发送方)Send和消息使用者(接收方)Recv。发布者将连接到RabbitMQ,发送一条消息,然后退出。
The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a RabbitMQ node on the local machine - hence the localhost. If we wanted to connect to a node on a different machine we'd simply specify its hostname or IP address here.
连接抽象套接字连接,并为我们处理协议版本协商和身份验证等。在这里,我们连接到本地机器上的RabbitMQ节点——也就是本地主机。如果我们想连接到另一台机器上的节点,我们只需在这里指定它的主机名或IP地址。
Next we create a channel, which is where most of the API for getting things done resides. Note we can use a try-with-resources statement because both Connection and Channel implement java.io.Closeable. This way we don't need to close them explicitly in our code.
接下来,我们创建一个通道,用于完成任务的大部分API都驻留在这里。注意,我们可以使用try-with-resources语句,因为连接和通道都实现java.io.Closeable。这样,我们就不需要在代码中显式地关闭它们。
To send, we must declare a queue for us to send to; then we can publish a message to the queue, all of this in the try-with-resources statement:
要发送,我们必须声明一个要发送到的队列;然后我们可以向队列发布一条消息,所有这些都在try-with-resources语句中完成
package io.moocstudent.rabbitmqlearning.demo;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @Author: zhangQi * @Date: 2020-11-20 11:28 */public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();//get new connection from factory
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
String message = "Hello World!";
channel.basicPublish("",QUEUE_NAME,null,message.getBytes(StandardCharsets.UTF_8));
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
有了发送者,自然还需要接收者
Receiving 接收者
That's it for our publisher. Our consumer listens for messages from RabbitMQ, so unlike the publisher which publishes a single message, we'll keep the consumer running to listen for messages and print them out.
这就是我们的出版商。我们的使用者侦听来自RabbitMQ的消息,因此与发布单个消息的发布者不同,我们将保持使用者运行侦听消息并将其打印出来。
topic类型demo [java]
https://github.com/moocstudent/rabbitmq-topic1