RabbitMQ的RPM包安装和Python读写操作

下载地址

bash 复制代码
## erlang 下载地址
https://packagecloud.io/rabbitmq/erlang?page=6

## rabbitmq 下载地址
https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.8.29-1.el7.noarch.rpm?distro_version_id=140

Rabbitmq的RPM包安装

bash 复制代码
## 下载
wget --content-disposition "https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-21.3.8.18-1.el7.x86_64.rpm/download.rpm?distro_version_id=140"

##
wget --content-disposition "https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.8.29-1.el7.noarch.rpm/download.rpm?distro_version_id=140"

###
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc

默认账号密码:guest/guest,rabbitmq 从 3.3.0 开始禁止使用 guest/guest 权限通过除 localhost 外的访问。

创建后创建用户相关命令

bash 复制代码
## 创建用户,第二个knight 为密码
rabbitmqctl add_user knight knight

##设置用户分配操作权限
rabbitmqctl set_user_tags knight administrator

##
rabbitmqctl set_permissions -p / knight ".*" ".*" ".*"

##
systemctl status rabbitmq-server

## 开启 web 访问
rabbitmq-plugins enable rabbitmq_management

其他常用命令

bash 复制代码
##
https://blog.csdn.net/knight_zhou/article/details/132599796

Python读写

bash 复制代码
### 依赖库
pip install pika

参考文章

bash 复制代码
##
https://support.huaweicloud.com/intl/zh-cn/devg-rabbitmq/rabbitmq-devg-003.html

生产者

bash 复制代码
import pika
import random

random_int = random.randint(1,100)
#Connection information
conf = {
    'host': 'yy.cn',
    'port': 5672,
    'queue_name': 'queue-test',
    'username': 'admin',
    'password': 'admin'
}

credentials = pika.PlainCredentials(conf['username'], conf['password'])
parameters = pika.ConnectionParameters(conf['host'],
                                       conf['port'],
                                       '/',
                                       credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(conf['queue_name'])

# data = bytes(str(random_int), encoding="utf-8")
data = bytes("cc", encoding="utf-8")

channel.basic_publish(exchange='',
                      routing_key=conf['queue_name'],
                      body=data)

print("produce...")

connection.close()

消费者

python 复制代码
import pika

#Connection information
conf = {
    'host': 'yy.cn',
    'port': 5672,
    'queue_name': 'queue-test',
    'username': 'admin',
    'password': 'admin'
}

credentials = pika.PlainCredentials(conf['username'], conf['password'])
parameters = pika.ConnectionParameters(conf['host'],
                                       conf['port'],
                                       '/',
                                       credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(conf['queue_name'])


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body.decode('utf-8'))


channel.basic_consume(queue=conf['queue_name'], on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
相关推荐
Francek Chen13 分钟前
【大数据技术基础 | 实验十二】Hive实验:Hive分区
大数据·数据仓库·hive·hadoop·分布式
Rverdoser6 小时前
RabbitMQ的基本概念和入门
开发语言·后端·ruby
陌小呆^O^6 小时前
Cmakelist.txt之Liunx-rabbitmq
分布式·rabbitmq
斯普信专业组8 小时前
深度解析FastDFS:构建高效分布式文件存储的实战指南(上)
分布式·fastdfs
jikuaidi6yuan10 小时前
鸿蒙系统(HarmonyOS)分布式任务调度
分布式·华为·harmonyos
BestandW1shEs10 小时前
彻底理解消息队列的作用及如何选择
java·kafka·rabbitmq·rocketmq
天冬忘忧10 小时前
Kafka 生产者全面解析:从基础原理到高级实践
大数据·分布式·kafka
天冬忘忧11 小时前
Kafka 数据倾斜:原因、影响与解决方案
分布式·kafka
隔着天花板看星星11 小时前
Kafka-Consumer理论知识
大数据·分布式·中间件·kafka