ububtu install rabiitmq and use it

1、安装所需环境(无权限就使用sudo)

      apt-get install erlang-nox     # 安装erlang
      erl    # 查看relang语言版本,成功执行则说明relang安装成功
2、添加公钥

     wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -

3、更新下工具

     apt-get update

4、install rabbitmq

     apt-get install rabbitmq-server  #安装成功自动启动

5、检查状态

     systemctl status rabbitmq-server   #Active: active (running) 说明处于运行状态

6、start stop restart

     service rabbitmq-server start    # 启动
     service rabbitmq-server stop     # 停止
     service rabbitmq-server restart  # 重启 
7、启用 web端可视化操作界面,我们还需要配置Management Plugin插件

     rabbitmq-plugins enable rabbitmq_management   # 启用插件
     service rabbitmq-server restart    # 重启

8、查看用户一般都有一个guest

     rabbitmqctl list_users

9、添加用户管理

     rabbitmqctl add_user admin yourpassword   # 增加普通用户
     rabbitmqctl set_user_tags admin administrator    # 给普通用户分配管理员角色 

    你可以在你的浏览器上输入:http://服务器Ip:15672/ 来访问你的rabbitmq监控页面。使用刚刚添加的新用户登录。

10、以下是python程序访问

     

import pika  # 引入mq包
import time

class RabbitMqSendMessage:
    def __init__(self, user, password, host, port,queue=None,durable=False,data=None):
        self.user = user
        self.password = password
        self.host = host
        self.port = port
        self.queue = queue
        self.credentials = pika.PlainCredentials(username=user,password=password)
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                        host=self.host,port=self.port,credentials=self.credentials))
        # create a broker
        self.channel = self.connection.channel()
        # create a queue named SR
        self.res = self.channel.queue_declare(queue=self.queue,durable=durable)
        self.data = data

    def publish(self,data):
        message = data if not self.data else self.data
        self.channel.basic_publish(exchange='',routing_key=self.queue,body=message)


    def consuming(self,callback=None,auto_ack=False,):
        '''
            basic_consume(self,
                          queue,
                          auto_ack=False,
                          exclusive=False,
                          consumer_tag=None,
                          arguments=None):
        '''
        self.channel.basic_consume(self.queue,callback,auto_ack=auto_ack)
        self.channel.start_consuming()

    def callback(self,ch, method, properties, body):
        #rewrite this method to satisfied your need if you have to
        print(body)

    def close(self):
        self.connection.close()

if __name__ == '__main__':
    rmsm = RabbitMqSendMessage(user='admin',password='xxx',
                               host='148.23.34.34',port='5672',
                               queue='message',durable=True)

    for i in range(10):

        rmsm.publish("I am coming%s".format(str(i)))
        time.sleep(5)
    rmsm.close()
    # rmsm.consuming(rmsm.callback,True)
    # rmsm.close()

https://blog.csdn.net/chasejava/article/details/105912398  若出现问题accessdeny 可以查看此内容

你可能感兴趣的:(rabbitmq,python,linux)