设为首页 收藏本站
查看: 845|回复: 0

[经验分享] Python学习之RabbitMQ队列(一)

[复制链接]

尚未签到

发表于 2018-8-13 13:31:54 | 显示全部楼层 |阅读模式
  RabbitMQ是流行的开源消息队列系统,用erlang语言开发。

  •   安装(Windows):
  Windows:
  首先需要安装 Erlang环境
  官网:
  http://www.erlang.org/
  Windows版下载地址:http://www.erlang.org/download/otp_win64_17.3.exe
  Linux版:     使用yum安装
  安装模块 pip install pika
  Linux(centos7):
  安装都是需要Erlang环境的。
wget  

  
rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
  

  
rpm --import http://packages.erlang-solutions.com/rpm/erlang_solutions.asc
  

  
yum install erlang
  erlang 安装完成后,开始安装rabbitmq。
  从官网下载(http://www.rabbitmq.com)rpm包;
rpm -ivh rabbitmq-server-3.4.1-1.noarch.rpm  #安装  

  
service rabbitmq-server start  #启动
  

  
chkconfig rabbitmq-server on   #开机自启动
  

  
#编辑配置文件
  
cp /usr/share/doc/rabbitmq-server-3.4.1/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
  
vim /etc/rabbitmq/rabbitmq.config
  

  
   %% The default "guest" user is only permitted to access the server
  
   %% via a loopback interface (e.g. localhost).
  
   %% {loopback_users, [<<"guest">>]},
  
   %%
  
   %% Uncomment the following line if you want to allow access to the
  
   %% guest user from anywhere on the network.
  

  
    {loopback_users, []}  #将改行取消注释,即允许远程用户访问。
  开启web界面管理工具
rabbitmq-plugins enable rabbitmq_management  #开启web界面管理工具  

  
systemctl restart  rabbitmq-server.service #重启服务
DSC0000.png

  界面如图所示,默认端口为15672.
  默认用户名和密码均为 guest
  默认网页是不允许访问的,需要增加一个用户修改一下权限,代码如下:
  添加用户:rabbitmqctl add_user tanx tanx
  添加权限:rabbitmqctl set_permissions -p "/" tanx ".*" ".*" ".*"
  修改用户角色rabbitmqctl set_user_tags tanx administrator
  然后就可以远程访问了,然后可直接配置用户权限等信息。
  set_permissions [-p vhost] {user} {conf} {write} {read}
  vhost
  The name of the virtual host to which to grant the user access, defaulting to /.
  user
  The name of the user to grant access to the specified virtual host.
  conf
  A regular expression matching resource names for which the user is granted configure permissions.
  write
  A regular expression matching resource names for which the user is granted write permissions.
  read
  A regular expression matching resource names for which the user is granted read permissions.
  rabbitmq常用命令
  add_user        <UserName> <Password>
  delete_user     <UserName>
  change_password <UserName> <NewPassword>
  list_users
  add_vhost    <VHostPath>
  delete_vhost <VHostPath>
  list_vhostsset_permissions   [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>
  clear_permissions [-p <VHostPath>] <UserName>
  list_permissions  [-p <VHostPath>]
  list_user_permissions <UserName>
  list_queues    [-p <VHostPath>] [<QueueInfoItem> ...]
  list_exchanges [-p <VHostPath>] [<ExchangeInfoItem> ...]
  list_bindings  [-p <VHostPath>]
  list_connections [<ConnectionInfoItem> ...]
  2.python代码
  先来一段简单的代码实现
  producter端
#!/usr/bin/env python  
import pika
  

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

  
#声明queue
  
channel.queue_declare(queue='hello')
  

  
#n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
  
channel.basic_publish(exchange='',
  
                      routing_key='hello',
  
                      body='Hello World!')
  
print(" [x] Sent 'Hello World!'")
  
connection.close()
  customer端
#_*_coding:utf-8_*_  
__author__ = 'Alex Li'
  
import pika
  

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

  

  
#You may ask why we declare the queue again  we have already declared it in our previous code.
  
# We could avoid that if we were sure that the queue already exists. For example if send.py program
  
#was run before. But we're not yet sure which program to run first. In such cases it's a good
  
# practice to repeat declaring the queue in both programs.
  
channel.queue_declare(queue='hello')
  

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

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

  
print('
  • Waiting for messages. To exit press CTRL+C')
      
    channel.start_consuming()

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-551247-1-1.html 上篇帖子: 【立贴】2017年8月开始自学Python 下篇帖子: Python2和Python3的一些语法区别
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表