当前位置: 首页 > news >正文

ThinkPHP8集成RabbitMQ的完整案例实现

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 function sendemail(){$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、运行结果展示

在这里插入图片描述

四、启动消费者:命令行执行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 function configure() {$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、执行结果展示

在这里插入图片描述

五、补充:宝塔安装rabbitmq

在宝塔软件里面安装rabbitmq 3.12.4
在这里插入图片描述
登录可直观展示
在这里插入图片描述

http://www.dtcms.com/a/295830.html

相关文章:

  • C# 方法执行超时策略
  • [Python] -进阶理解5- Python 模块与包的导入机制解析
  • uniapp中mp-html使用方法
  • 特定日志输出aop实现
  • day62-可观测性建设-全链路监控zabbix+grafana
  • Redis的事务和Lua之间的区别
  • day13 flash
  • 「iOS」黑魔法——方法交换
  • 告别束缚:这款“隐形心电监测仪”让心脏健康管理更自由
  • JavaSE:开发环境的搭建(Eclipse)
  • 企业级数据分析创新实战:基于表格交互与智能分析的双引擎架构
  • 从0到1学习c++ 命名空间
  • 《 java 随想录》| 数组
  • MySQL的命令行客户端
  • 探索双链表:C语言中的链式结构魔法
  • 光谱仪杂散光性能分析
  • 大疆无人机炸机后视频损坏的完美修复案例解析
  • uni-file-picker vue3二次封装(本地上传 + v-model)
  • Mysql命令show processlist
  • Linux基础服务(autofs和Samba)
  • 论文阅读:《针对多目标优化和应用的 NSGA-II 综述》一些关于优化算法的简介
  • OpenCV —— color_matrix_numpy_mat_reshape
  • 新mac电脑软件安装指南(前端开发用)
  • 解决http的web服务中与https服务交互的问题
  • 平时遇到的错误码及场景?404?400?502?都是什么场景下什么含义,该怎么做 ?
  • AI实践:Pydantic
  • 大模型Prompt优化工程
  • pdf文件的属性值怎么修改?修改PDF内部的属性创建时间和修改时间
  • Lua(垃圾回收)
  • omofun官网网站入口,动漫在线看|官方下载