当前位置: 首页 > wzjs >正文

阿里巴巴吧网站建设百度推广官网

阿里巴巴吧网站建设,百度推广官网,初中做网站的软件,天津住房与城乡建设厅网站概述 RabbitMQ 是一个开源的消息代理和队列服务器,实现了 AMQP 0-9-1 标准。它可以在完全不同的应用程序之间传递消息。本教程将带你从零开始学习如何使用 RabbitMQ。 环境准备 1. 安装 RabbitMQ - 在命令行中运行 sudo apt-get install rabbitmq-server (Ubunt…

概述

RabbitMQ 是一个开源的消息代理和队列服务器,实现了 AMQP 0-9-1 标准。它可以在完全不同的应用程序之间传递消息。本教程将带你从零开始学习如何使用 RabbitMQ。

环境准备

1. 安装 RabbitMQ

- 在命令行中运行 `sudo apt-get install rabbitmq-server` (Ubuntu) 或者通过官方文档获取其他系统的安装指南。

2. 启动服务

- 运行 `sudo service rabbitmq-server start`。

3. 安装客户端库

- 对于 Python: `pip install pika`

第一步:发送简单消息

发送端

```python

import pika

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

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!')

print(" [x] Sent 'Hello World!'")

connection.close()

```

接收端

```python

import pika

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

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

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=True)

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

channel.start_consuming()

```

第二步:工作队列

生产者

```python

import pika

import sys

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

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = ' '.join(sys.argv[1:]) or "Hello World!"

channel.basic_publish(

exchange='',

routing_key='task_queue',

body=message,

properties=pika.BasicProperties(

delivery_mode=2, # make message persistent

))

print(" [x] Sent %r" % message)

connection.close()

```

消费者

```python

import pika

import time

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

time.sleep(body.count(b'.'))

print(" [x] Done")

ch.basic_ack(delivery_tag=method.delivery_tag)

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

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

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

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue', on_message_callback=callback)

channel.start_consuming()

```

第三步:发布/订阅

发布端

```python

import pika

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

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

message = "Info: Hello World!"

channel.basic_publish(exchange='logs', routing_key='', body=message)

print(" [x] Sent %r" % message)

connection.close()

```

接收端 1

```python

import pika

import uuid

def on_message(channel, method, props, body):

print("[x] %r" % body)

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

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=on_message, auto_ack=True)

channel.start_consuming()

```

接收端 2

```python

import pika

import uuid

def on_message(channel, method, props, body):

print("[x] %r" % body)

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

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=on_message, auto_ack=True)

channel.start_consuming()

```

第四步:路由模式

发送端

```python

import pika

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

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

severity = sys.argv[1] if len(sys.argv) > 1 else 'info'

message = ' '.join(sys.argv[2:]) or 'Hello World!'

channel.basic_publish(

exchange='direct_logs', routing_key=severity, body=message)

print(" [x] Sent %r:%r" % (severity, message))

connection.close()

```

接收端

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r:%r" % (method.routing_key, body))

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

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

severities = ['error', 'warning']

for severity in severities:

channel.queue_bind(

exchange='direct_logs', queue=queue_name, routing_key=severity)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

http://www.dtcms.com/wzjs/21671.html

相关文章:

  • 区镇村政府网站群的建设推广的方式有哪些
  • 怎样学习做网站的编程google关键词搜索量
  • wordpress 搜索内容企业网站seo
  • 烟台网站建设哪家好安卓优化
  • 做视频用的网站广州网络推广平台
  • 东莞网站建设价格价格口碑营销方案
  • 政府网站优秀案例西安网站seo公司
  • 巧家县住房和城乡建设局网站高端网站建设公司排行
  • 长春专业做网站的公司排名百度爱采购排名
  • 海口市龙华区核酸检测seo描述是什么
  • 网站建设. 龙兵科技外贸怎么找客户资源
  • 美容 北京 公司 网站建设seo技术顾问
  • 软件开发模型图青岛百度快速优化排名
  • 网站初期seo怎么做福州短视频seo推荐
  • 泊头网站建设网络优化报告
  • 购物网站建设与实现软文写作的基本要求
  • 如何做论坛网站 知乎网站seo外链
  • 自己怎么做网站建设百度账号购买1元40个
  • 怎么做网站记者如何创建一个app平台
  • 湖北营销型网站建设合肥seo整站优化
  • 杭州住房城乡建设网站查询关键词歌曲歌词
  • asp.net网站百度指数数据分析报告
  • 网站logo怎么做透明在百度上怎么发布信息
  • vps 网站备案搭建一个app平台要多少钱
  • 做ppt模仿网站广州推广引流公司
  • 西宁市网站建设价格产品seo是什么意思
  • wordpress 导航站主题百度加盟
  • 做垃圾网站来干嘛跨境电商平台推广
  • 网站建设费用计入什么二级科目seo线下培训班
  • 南城免费做网站微商推广哪家好