The first is to install rabbitmq friends. I believe this will be linux and windows people should be able to install it!
Specific installation methods Baidu. But when I install linux, had wanted to install the source code, but it seems to install the file in question, I do not know if he did not C language compiler package. Now without him, I finally installed RPM package.
The two installation package, I have to share it, we all go to download it.
erlang19.0.4-1 https://ftp.dgpm.co/s/zzc7sZbm8mxqe93
rabbitmq3.7.5 https://ftp.dgpm.co/s/NKx9HZmKfSKMys8
When installing rabbitmq, there is a small error, only need to install the support package can be resolved. It is this socat
yum install socat get a key.
After the installation is complete. The effect is such
There are some commonly used commands.
carried out:
/bin/systemctl stoprabbitmq-server.service #关闭
/bin/systemctl start rabbitmq-server.service #启动
/bin/systemctl status rabbitmq-server.service #状态
1. User Management
User management including increasing users, delete users, view the list of users, change user passwords.
The appropriate command
(1) add a user
rabbitmqctl add_user Username Password
(1.1) give the user authority to order
rabbitmqctl set_permissions -p "/" User'.*' '.*' '.*'
(1.2) gives the user command role
rabbitmqctl set_user_tags User administrator
(2) delete a user
rabbitmqctl delete_user Username
(3) modify the user's password
rabbitmqctl change_password Username Newpassword
(4) 查看当前用户列表
rabbitmqctl list_users
开启管理UI:
rabbitmq-plugins enable rabbitmq_management
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload
最后的的是防火墙开放端口。大家都懂得。
有一个用的比较多的命令,查看队列的。就是rabbitmqctl list_queues
查看队列和消息数量。
--------------------------------好了,rabbitMQ的安装就介绍了这里了吧------------------------------------------
现在开始说在python里面生产者和消费者的代码,刚开始消费者一直有一个报错。我都迷糊,查了好多资料,都解决不了,最后才知道,是rabbitmq的参数位置变了,所以我的参数位置也要调整。
就是这个报错,我都快懵了。
python生产者代码:
#_*_coding:utf-8_*_ #author:david.z import pika import random credentials=pika.PlainCredentials('david','123') #这里做了一个用户身份认证识别 connection=pika.BlockingConnection( pika.ConnectionParameters('IP地址',5672,'/',credentials) #这里是连rabbitmq ) channel=connection.channel() #声明一个管道 #声明queue channel.queue_declare(queue='PMC2',durable=True) #加True是为管道持久化 #n RabbitMQ a message can never be sent directly to queue,it always # list=['我爱北京天安门','天安门上太阳升','鱼戏莲叶东,',' 鱼戏莲叶西,','鱼戏莲叶南,',' 鱼戏莲叶北。'] # for i in list: # # print (i) channel.basic_publish(exchange='', routing_key='PMC2', body="我爱北京天安门", properties=pika.BasicProperties( delivery_mode=2, #模式为2是为了消息持久化 )) print ("[x] Sent 'Hello World!'") connection.close()
python消费者代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/2/22 15:57 # @Author :david # @File : rabbitMQ_rescv_1.py # @Software: PyCharm import pika credentials = pika.PlainCredentials('david', '123') connection = pika.BlockingConnection(pika.ConnectionParameters('IP地址',5672,'/',credentials)) channel = connection.channel() channel.queue_declare(queue='PMC2',durable=True) def callback(ch, method, properties, body): print(" [x] Received %r" % body.decode()) channel.basic_qos(prefetch_count=1) #加上这句可以实现负载平均化 channel.basic_consume('PMC2', #消费消息 callback, True) print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
自此简单的生产者消费代码就出来了。
然后就是广播,用的exchange.这里一定要提醒一下,广播原理类似于收音机,他是会发广播。但是如果生产者如果下线,然后在上线,是不会收到,上线之前的广播的,因为广播是不会一直存的。过时就没了。