centos7.5安装rabbitMQ及python3运行简单示例

rabbitMQ环境准备:

第一步:设置存储库
curl -1sLf
'https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/setup.rpm.sh'
| sudo -E bash

image.png

第二步:yum install -y erlang-23.3.4 (centos8下载erlang24版本)
第三步:yum install -y rabbitmq-server
chkconfig rabbitmq-server 查看是否以守护进程启动
systemctl enable rabbitmq-server.service 设置默认

/sbin/service rabbitmq-server start
/sbin/service rabbitmq-server status
/sbin/service rabbitmq-server stop

image.png

参考资料:
https://www.rabbitmq.com/install-rpm.html#with-rpm

python3环境准备:

第一步:
依赖升级及安装:
yum -y install openssl openssl-devel
cd /usr/local/
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar -zxvf Python-3.6.8.tgz
cd Python-3.6.8
mkdir Python
./configure --prefix=/usr/local/Python
./configure --prefix=/usr/local/Python --with-openssl
make && make install
还需要按这个逻辑改几个bash,忘记改哪几个了,根据提示来吧

mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3/bin/python3 /usr/bin/python

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
cd /usr/local/Python/bin
./pip3.6 install pika

client.py
import pika

credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters('localhost',int(5672),'/',credentials)

connection = pika.BlockingConnection(parameters)
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()

receive.py
import pika

credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters('localhost',
                                   5672,
                                   '/',
                                   credentials)

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

channel.queue_declare(queue='hello')

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

channel.basic_consume('hello', callback, False)

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

image.png

image.png

你可能感兴趣的:(centos7.5安装rabbitMQ及python3运行简单示例)