- 准备工作:
zlib-devel-1.2.7-18.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
nginx-1.9.15.tar.gz
10.131.178.239(rabbitmq-node1)
10.131.178.240(rabbitmq-node2)
10.131.178.247(nginx机器)
10.131.178.101(验证机器) - 在178.247机器上安装nginx:
rpm -ivh zlib-devel-1.2.7-18.el7.x86_64.rpm
rpm -ivh pcre-devel-8.32-17.el7.x86_64.rpm
tar -xzvf nginx-1.9.15.tar.gz
./configure --prefix=/usr/local/nginx --with-stream --with-pcre
make
make install
2.配置nginx.conf:
vim /usr/local/nginx/conf/nignx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
stream {
upstream rabbitmq{
server 10.131.178.239:5672 max_fails=2 fail_timeout=3s weight=2;
server 10.131.178.240:5672 max_fails=2 fail_timeout=3s weight=2;
}
server {
listen 15678;
#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_connect_timeout 30s;
proxy_timeout 30s;
proxy_pass rabbitmq;
}
}
- 启动nginx:
/usr/local/nginx/sbin/nginx
4.模拟场景测试,需要在178.101机器上安装pika模块,如果内网安装失败,请直接在外网安装好,把site-packages中pika对应文件直接复制到对应的Python内网site-packages目录中(Linux和Win通用,至少现在是这样):
生产者Producer.py
# coding: utf-8
import json
import pika
import datetime
#生成消息入口处
def get_message():
for i in range(10): #生成10条消息
message=json.dumps({'id': "10000%s" % i, "amount": 100 * i,"name":"tony","createtime":str(datetime.datetime.now())})
producter(message)
#消息生产者
def producter(message):#消息生产者
# 获取与rabbitmq 服务的连接,虚拟队列需要指定参数 virtual_host,如果是默认的可以不填(默认为/),也可以自己创建一个
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.131.178.247', port=15678,credentials=pika.PlainCredentials('admin', 'xxxxxx')))
#创建一个 AMQP 信道(Channel),建造一个大邮箱,隶属于这家邮局的邮箱
channel = connection.channel()
# 声明消息队列tester,消息将在这个队列传递,如不存在,则创建
channel.queue_declare(queue='tester')
# 向队列插入数值 routing_key的队列名为tester,body 就是放入的消息内容,exchange指定消息在哪个队列传递,这里是空的exchange但仍然能够发送消息到队列中,因为我们使用的是我们定义的空字符串“”exchange(默认的exchange)
channel.basic_publish(exchange='', routing_key='tester', body=message)
# 关闭连接
connection.close()
if __name__=="__main__":
get_message()#程序执行入口
消费者Consumer.py
# coding:utf-8
import pika
#接收消息,并写入文件,这也算是持久化了
def write_file(message):
with open("msg.txt","a+") as f:
f.write(message)
def consumer():#消息消费者
# 获取与rabbitmq 服务的连接
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.131.178.247', port=15678,credentials=pika.PlainCredentials('admin', 'xxxxxx')))
# 创建一个 AMQP 信道(Channel)
channel = connection.channel()
# 声明消息队列tester,durable=False 表示不持久化
channel.queue_declare(queue='tester', durable=False)
# 定义一个回调函数来处理消息队列中的消息,这里是将消息写入文件,你也可以入库。
def callback(ch, method, properties, body):
ch.basic_ack(delivery_tag=method.delivery_tag) # 告诉生成者,消息处理完成
write_file(body.decode())
#告诉rabbitmq在tester列表里面收消息,收到就调用callback函数
channel.basic_consume('tester', callback)
# 开始接收信息,并进入阻塞状态,队列里有信息才会调用callback进行处理
channel.start_consuming()
if __name__=="__main__":
consumer()
写的不够详细,踩了很多很多很多很多坑s,有问题可以留言,看到会回复
while true;do python producer.py;done;sleep 5;
代码参考文章:
https://blog.csdn.net/weixin_41754309/article/details/107070249
https://blog.csdn.net/weixin_41754309/article/details/107070171