27 lines
903 B
Python
27 lines
903 B
Python
|
import pika
|
|||
|
import time
|
|||
|
import random
|
|||
|
|
|||
|
def generate_order_event(channel):
|
|||
|
order_id = random.randint(1, 1000)
|
|||
|
event = f"Поступил новый заказ #{order_id}"
|
|||
|
message = f"Событие: {event}"
|
|||
|
channel.basic_publish(exchange='events', routing_key='', body=message)
|
|||
|
print(f" [x] Sent: {message}")
|
|||
|
|
|||
|
def main():
|
|||
|
# Устанавливаем соединение с сервером RabbitMQ
|
|||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
|||
|
channel = connection.channel()
|
|||
|
|
|||
|
# Объявляем exchange с типом 'fanout'
|
|||
|
channel.exchange_declare(exchange='events', exchange_type='fanout')
|
|||
|
|
|||
|
# В бесконечном цикле генерируем и отправляем события в RabbitMQ
|
|||
|
while True:
|
|||
|
generate_order_event(channel)
|
|||
|
time.sleep(1)
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
main()
|