Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

RabbitMQ uses Python tests to send and receive messages

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Set rabbitMQ mirroring mode

1.1. General mode

Each node of the cluster only has the same metadata, that is, the structure of the queue

The message entity exists only on one of the nodes rabbit01 (or rabbit02)

When the message enters the Queue of the rabbit01 node and the consumer is consumed from the rabbit02 node, RabbitMQ temporarily transmits the message between rabbit01 and rabbit02, taking out the message entity in An and sending it to consumer through B

1.2. Mirror mode

Make the required queue into a mirror queue, which exists with multiple nodes, and the message entity will actively synchronize between the mirror nodes, which belongs to the HA scheme of RabbitMQ.

The side effect is also obvious. in addition to reducing system performance, if there are too many mirror queues and a large number of messages enter, the network bandwidth within the cluster will be greatly consumed by this synchronous communication.

1.1. Create a mirror mode

Start rabbit

Rabbitmq-server start-detached

Rabbitmqctl set_policy ha-all "^"'{"ha-mode": "all"}'

Rabbitmqctl set_policy ha-all "^"'{"ha-mode": "all", "ha-sync-mode": "automatic"}'

1.2, View

Rabbitmqctl list_policies

Rabbitmqctl set_policy [- p] [--priority] [--apply-to] # clear

Rabbitmqctl clear_policy [- p] # View

Rabbitmqctl list_policies [- p]

Clear Mirror Mod

Rabbitmqctl clear_policy-p / ha-all

Cluster node status:

{running_nodes, [rabbit@zk_kakfa3,rabbit@zk_kakfa2,rabbit@zk_kakfa1]}

Write python test script to send and receive messages:

1.3. Installation dependency:

Python client

Pip install pika

1.4.The RabbitMQ controls adding queues

1.5.The python test send script

More mian.py

Import pika

Import random,time

Credentials = pika.PlainCredentials ('testmq',' 1qaz2wsx')

# remote IP can be connected here, please remember to open the remote port

Parameters = pika.ConnectionParameters ('192.168.12.223)

Connection = pika.BlockingConnection (parameters)

Channel = connection.channel ()

# channel.queue_declare (queue='hello')

Number=1

While True:

# for i in ['test3']: # number = random.randint (1PM1000) body=' hello world {}: '.format (number) channel.basic_publish (exchange=' {}' .format ('test3'), routing_key='hello', body=body) print ("push message: [x] Sent%"% body) time.sleep (1) number+=1

Connection.close ()

1.6. python test receiving script

#! / usr/bin/env python

Coding: UTF-8--

Import pika

Import random,time

Def callback (ch, method, props, body):

# time.sleep (2)

Print ('recive message:',body)

Ch.basic_ack (delivery_tag=method.delivery_tag)

Credentials = pika.PlainCredentials ('testmq',' 1qaz2wsx')

# remote IP can be connected here, please remember to open the remote port

Parameters = pika.ConnectionParameters ('192.168.12.223)

Connection = pika.BlockingConnection (parameters)

Channel = connection.channel ()

Channel.basic_consume ('test3',callback, auto_ack=False)

# channel.basic_consume ('test2',callback, auto_ack=True)

Channel.start_consuming ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report