ThinkPHP8集成RabbitMQ的完整案例实现

一、安装依赖:需通过Composer安装php-amqplib

二、配置RabbitMQ

三、生产者

1. 发送一个邮件,将任务发送到RabbitMQ队列中。

    2. 运行结果展示

    四、启动消费者:命令行执行php think rabbitmq:consumer

    1. 在command文件夹下创建consumer.php文件

    2. 配置指令

    3. 执行结果展示

    五、补充:宝塔安装rabbitmq


    一、安装依赖:需通过Composer安装php-amqplib库‌

    composer require php-amqplib/php-amqplib

    二、配置RabbitMQ

    在服务器开放RabbitMQ端口5672

    return[
    'default'=>'rabbitmq',
    'connections'=>[
    'rabbitmq'=>[
    'driver'=>'rabbitmq',
    'host'=>'127.0.0.1', // RabbitMQ服务器地址
    'port'=>5672, // RabbitMQ端口
    'user'=>'guest', // 用户名
    'password'=>'guest', // 密码
    'vhost'=>'/', // 虚拟主机
    'queue'=>'email_queue', // 队列名称
    'exchange'=>'email_exchange', // 交换机名称
    'routing_key'=>'email_queue', // 路由键
    'durable'=> true, // 是否持久化队列和消息
    ]
    ]
    ];

    三、生产者

    1. 发送一个邮件,将任务发送到RabbitMQ队列中。

    app/controller/SendEMail.php

    namespace app\controller;
    use app\common\SendEmailJob;
    use think\facade\Config;
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    class SendEmail 
    {
        public functionsendemail(){
    $config= config('queue.connections.rabbitmq');
            // dd($config);
    $connection= new AMQPStreamConnection(
    $config['host'], $config['port'],
    $config['user'], $config['password'], $config['vhost']
    );
     
    $channel=$connection->channel();
    $channel->exchange_declare($config['exchange'], 'direct', false, true, false);
    $channel->queue_declare($config['queue'], false, true, false, false);
    $channel->queue_bind($config['queue'], $config['exchange'], $config['routing_key']);
     
    $data=[
    'to'=>'11user@example.com',
    'subject'=>'ThinkPHP8 RabbitMQ测试',
    'content'=>'这是一封通过消息队列发送的邮件'
    ];
     
    $msg= new AMQPMessage(json_encode($data), ['delivery_mode'=>2]);
    $channel->basic_publish($msg, $config['exchange'], $config['routing_key']);
     
    $channel->close();
    $connection->close();
    return'邮件任务已发送到队列';
    }
     
     
    }

    2. 运行结果展示

    ThinkPHP8集成RabbitMQ的完整案例实现

    四、启动消费者:命令行执行php think rabbitmq:consumer

    1. 在command文件夹下创建consumer.php文件

    接收任务,从RabbitMQ队列中获取任务执行。

    app/command/consumer.php

    namespace app\command;
    use think\console\Command;
    use think\console\Input;
    use think\console\Output;
    use PhpAmqpLib\Connection\AMQPStreamConnection;
     
    class Consumer extends Command {
        protected functionconfigure(){
    $this->setName('rabbitmq:consumer')->setDescription('RabbitMQ消费者');
    }
     
        protected function execute(Input $input, Output $output){
    $config= config('queue.connections.rabbitmq');
    $connection= new AMQPStreamConnection(
    $config['host'], $config['port'],
    $config['user'], $config['password'], $config['vhost']
    );
     
    $channel=$connection->channel();
    $channel->queue_declare($config['queue'], false, true, false, false);
     
    $callback= function($msg) use ($output){
    $data= json_decode($msg->body, true);
    $output->writeln("收到邮件任务: {$data['to']}");
                // 实际发送邮件逻辑
    $msg->ack();
    };
     
    $channel->basic_qos(null, 1, null);
    $channel->basic_consume($config['queue'], '', false, false, false, false, $callback);
     
    while($channel->is_consuming()){
    $channel->wait();
    }
     
    $channel->close();
    $connection->close();
    }
    }

    2. 配置指令

    config/console.php

    'commands'=>[
    'rabbitmq:consumer'=>'app\command\Consumer',
    ],

    执行命令:

    php think rabbitmq:consumer

    3. 执行结果展示

    ThinkPHP8集成RabbitMQ的完整案例实现

    五、补充:宝塔安装rabbitmq

    在宝塔软件里面安装rabbitmq 3.12.4

    ThinkPHP8集成RabbitMQ的完整案例实现

    登录可直观展示

    ThinkPHP8集成RabbitMQ的完整案例实现

    转载作品,原作者:程序员阿凡提,文章来源:https://blog.csdn.net/wangshifan116/article/details/153262509

    (0)
    打赏 微信赞赏 微信赞赏 支付宝赞赏 支付宝赞赏
    上一篇 2025-09-23 16:02
    下一篇 2025-10-16 10:08

    相关推荐

    • ThinkPHP8集成RabbitMQ的完整案例实现

      一、安装依赖:需通过Composer安装php-amqplib库‌ 二、配置RabbitMQ 在服务器开放RabbitMQ端口5672 三、生产者 1、发送一个邮件,将任务发送到R…

      2025-09-23 Php
      3710
    • 请不要使用阿里云composer镜像升级webman最新版本

      近期很多开发者升级workerman和webman-framework后会出现错误 PHP Fatal error:  Declaration of Webman\Http\Req…

      2024-12-07
      1.3K0
    • CentOS7 下 RabbitMQ 安装与配置

      CentOS7 下 RabbitMQ 安装与配置,RabbitMQ 服务器在安装之前需要安装 erlang,最新版本的 RabbitMQ 3.8.0 需要 Erlang 21.3 以上的版本支持。

      2021-11-25
      1.6K0
    • 为ThinkPHP8快速开启跨域请求

      在此前,要在TP8中启用跨域请求,是要折腾一下的。时至今日,TP官方不知道什么时候,新增了一个官方组件:topthink/think-cors。通过这个组件,1分钟就能快速搞定跨域…

      2025-10-16 Php
      2330
    • docker搭建RabbitMQ集群

      docker搭建RabbitMQ集群

      2023-03-18 Linux
      1.5K0
    • 使用PHP处理RabbitMQ消息队列的应用

      使用PHP处理RabbitMQ消息队列的应用,消息队列(Message Queue)是一种应用间的通信方式,消息发送后可以立即返回,由消息系统来确保消息的可靠传递。消息发布者只管把消息发布到 MQ 中而不用管谁来取,消息使用者只管从 MQ 中取消息而不管是谁发布的。这样发布者和使用者都不用知道对方的存在。

      2024-09-05
      1.6K0

    发表回复

    登录后才能评论
    扫码了解
    扫码了解
    反馈建议
    分享本页
    返回顶部