欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rabbitmq教程_RabbitMQ教程

程序员文章站 2022-05-21 20:00:24
...
rabbitmq教程_RabbitMQ教程

rabbitmq教程

RABBITMQ is a message broker which works as a common channel for different entities. The main purpose of RABBITMQ is clustering and high availability. Message producer entities put message to the RABBITMQ and then the consumers get it according to work plan. If some of the producers or consumers fail the remainings still works. Advenced Messaging Queuing Protocol (AMQP) is the main standart that is implemented  by RABBITMQ.

RABBITMQ是一个消息代理,它充当不同实体的公共通道。 RABBITMQ的主要目的是集群和高可用性。 消息生产者实体将消息发送到RABBITMQ,然后消费者根据工作计划得到消息。 如果某些生产者或消费者失败了,其余的仍然起作用。 先进消息队列协议(AMQP)是RABBITMQ实现的主要标准。

RABBITMQ gives reliability to the application with acknowledgements and publisher confirms. Messages can be bind to the different entities according to the given logic. Several RABBITMQ servers can be clustered which makes high availability. RABBITMQ can federate several different applications and makes it easy. Here can be given more examples but theses are enough.

RABBITMQ通过确认和发布者确认为应用程序提供可靠性。 可以根据给定的逻辑将消息绑定到不同的实体。 可以将多个RABBITMQ服务器集群,从而实现高可用性。 RABBITMQ可以联合几个不同的应用程序并使其变得容易。 这里可以给出更多示例,但是这些就足够了。

Installation ofRabbitMQ

安装RabbitMQ

Install RABBITMQ to the fedora

将RABBITMQ安装到fedora

$ yum install rabbitmq-server.noarch

为RabbitMQ安装Python库 (Install Python Library For RabbitMQ)

In this tutorial we will use python language. But we need a library which supports AMQP. We choose to use pika so we install it with the following command

在本教程中,我们将使用python语言。 但是我们需要一个支持AMQP的库。 我们选择使用pika,因此我们使用以下命令进行安装

$ yum install python-pika.noarch

We need to firstly check the status of the rabbitmq server. Here we get that it is not started and we start it with default configuration

我们首先需要检查Rabbitmq服务器的状态。 在这里,我们得到它尚未启动,并使用默认配置启动它

启动RabbitMQ服务器守护程序(Start RabbitMQ Server Daemon)

[aaa@qq.com ismail]# systemctl start rabbitmq-server
[aaa@qq.com ismail]# systemctl status rabbitmq-server
rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled)
Active: active (running) since Sat 2014-11-22 04:25:40 EET; 1s ago
Main PID: 9179 (beam.smp)

...

创建生产者 (Create Producer)

We will write some producer to create messages to ht RabbitMQ server. We put following code into p1.py 

我们将写一些生产者来创建消息到ht RabbitMQ服务器。 我们将以下代码放入p1.py

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='Selam')

channel.basic_publish(exchange='',
routing_key='hello',
body='Selam Naber')
print " [x] Sent 'Selam Naber'"
connection.close()

Now run the producer and send the message to the queue.

现在运行生产者并将消息发送到队列。

$ python p1.py
[x] Sent 'Selam Naber'

创建消费者 (Create Consumer)

Here is the consumer which waits after running for a message. File name of the consumer is c1.py 

这是运行消息后等待的使用者。 使用者的文件名是c1.py

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)

channel.basic_consume(callback,
queue='hello',
no_ack=True)

channel.start_consuming()

开始消费生产者消息 (Start Consuming Producers Messages)

If we run producer we get the message

如果我们运行生产者,我们会得到消息

$ python c1.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Selam Naber'
通过Python示例了解更多Memcached前​​置操作

RabbitMQ教程信息图(RabbitMQ Tutorial Infographic)

rabbitmq教程_RabbitMQ教程
RabbitMQ Tutorial Infographic
RabbitMQ教程信息图

翻译自: https://www.poftut.com/rabbitmq-tutorial/

rabbitmq教程