共计 4778 个字符,预计需要花费 12 分钟才能阅读完成。
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();
}
}
}