tp6使用rabbitmq

1.composer安装amqp包

composer requirer php-amqplib/php-amqplib

2.新建配置文件 config/rabbit_mq.php

return [
    'host'=>'127.0.0.1',
    'port'=>'5672',
    'user'=>'guest',
    'password'=>'guest',
    'vhost'=>'/',
    'exchange_name' => 'email_exchange',
    'queue_name' => 'email_queue',
    'route_key' => 'email_route',
    'consumer_tag' => 'consumer',

];

3. 生产者Producer.php

<?php

namespace app;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;

class Producer
{

    private $connection;
    private $channel;
    private $mq_config;

    public function __construct()
    {
        $this->mq_config = config('rabbit_mq');
        $this->connection = new AMQPStreamConnection(
            $this->mq_config['host'],
            $this->mq_config['port'],
            $this->mq_config['user'],
            $this->mq_config['password']
        );
        //创建通道
        $this->channel = $this->connection->channel();
    }

    public function send($data)
    {
        /**
         * 创建队列(Queue)
         * name: hello         // 队列名称
         * passive: false      // 如果设置true存在则返回OK,否则就报错。设置false存在返回OK,不存在则自动创建
         * durable: true       // 是否持久化,设置false是存放到内存中的,RabbitMQ重启后会丢失;设置true,则代表是一个持久化的队列,服务重启后也会存在,因为服务会把持久化的queue存放到磁盘上当服务重启的时候,会重新加载之前被持久化的queue
         * exclusive: false    // 是否排他,指定该选项为true则队列只对当前连接有效,连接断开后自动删除
         * auto_delete: false // 是否自动删除,当最后一个消费
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
首先,需要在RabbitMQ中创建一个名为“cancel_order”的队列。该队列将用于接收自动取消订单的消息。 然后,需要编写一个Python脚本来将消息发布到该队列中。以下是示例脚本: ```python import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 发布一个消息到队列中 channel.basic_publish(exchange='', routing_key='cancel_order', body='cancel order') print("Order cancellation message sent to RabbitMQ") connection.close() ``` 这个脚本将在RabbitMQ中发布一个名为“cancel order”的消息,用于取消订单。 接下来,需要在订单处理系统中添加一个消费者来监听“cancel_order”队列,并自动取消订单。以下是示例代码: ```python import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 定义一个回调函数来处理消息 def cancel_order_callback(ch, method, properties, body): print("Cancel order message received") # 在这里添加取消订单的代码 # ... # 监听“cancel_order”队列 channel.basic_consume(queue='cancel_order', on_message_callback=cancel_order_callback, auto_ack=True) # 开始消费消息 print("Waiting for cancellation messages...") channel.start_consuming() ``` 当一个名为“cancel order”的消息被发布到“cancel_order”队列时,消费者将自动调用“cancel_order_callback”函数,并执行取消订单的代码。 最后,可以使用一个定时任务来定期发布自动取消订单的消息到“cancel_order”队列中。这可以通过使用Python的“schedule”库来实现。以下是示例代码: ```python import schedule import time import pika # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建一个名为“cancel_order”的队列 channel.queue_declare(queue='cancel_order') # 发布一个名为“cancel order”的消息到队列中 def publish_cancel_order_message(): channel.basic_publish(exchange='', routing_key='cancel_order', body='cancel order') print("Order cancellation message sent to RabbitMQ") # 每小时发布一个自动取消订单的消息 schedule.every().hour.do(publish_cancel_order_message) # 开始执行定时任务 while True: schedule.run_pending() time.sleep(1) ``` 这个脚本将每小时发布一个名为“cancel order”的消息到“cancel_order”队列中。这将使消费者自动取消超时的订单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值