[RabbitMQ] ThinkPHP6如何在Centos7.6安装和使用RabbitMQ

384 阅读1分钟

一.在 CentOS 7.6 上安装 Erlang 23 和 RabbitMQ

以下是在 CentOS 7.6 上安装 Erlang 23 和 RabbitMQ 的步骤:

    1. 更新系统: sudo yum update
    1. 添加 Erlang Solutions 仓库密钥:
sudo rpm --import https://packages.erlang-solutions.com/rpm/erlang_solutions.asc
    1. 添加 Erlang Solutions 仓库:
sudo tee /etc/yum.repos.d/erlang_solutions.repo <<EOF
[erlang-solutions]
name=CentOS $releasever - $basearch - Erlang Solutions
baseurl=https://packages.erlang-solutions.com/rpm/centos/$releasever/$basearch
gpgcheck=1
gpgkey=https://packages.erlang-solutions.com/rpm/erlang_solutions.asc
enabled=1
EOF
./configure 
make sudo 
make install
# 查看版本
erl 
    1. 安装 RabbitMQ 所需的附加依赖项: sudo yum install socat logrotate
  • 如果404, 则 yum --disablerepo=erlang-solutions install socat 用这个

    1. 下载 RabbitMQ 软件包:
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.19/rabbitmq-server-3.8.19-1.el7.noarch.rpm
    1. 安装 RabbitMQ: sudo rpm -Uvh rabbitmq-server-3.8.19-1.el7.noarch.rpm

如果安装失败 CSDN

sudo rpm -Uvh --nodeps rabbitmq-server-3.8.19-1.el7.noarch.rpm

    1. 启动并启用 RabbitMQ 服务:
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
    1. 检查 RabbitMQ 服务的状态: sudo systemctl status rabbitmq-server

如果服务正在运行,您应该在输出中看到 "active (running)"。

    1. 设置 RabbitMQ 管理控制台(可选):
sudo rabbitmq-plugins enable rabbitmq_management
sudo systemctl restart rabbitmq-server

完成上述步骤后,您将在 CentOS 7.6 上成功安装 Erlang 23 和 RabbitMQ。

二.远程访问 RabbitMQ 管理控制台

要创建新用户并授予管理访问权限,请按照以下步骤操作:

  1. 使用 SSH 或终端登录到 RabbitMQ 服务器。

  2. 运行以下命令创建一个新用户:sudo rabbitmqctl add_user username password

  3. 授予新用户管理员权限: sudo rabbitmqctl set_user_tags username administrator

  4. 授予新用户管理访问权限: sudo rabbitmqctl set_permissions -p / username ".*" ".*" ".*"

  5. 重新启动 RabbitMQ 服务以应用更改: sudo systemctl restart rabbitmq-server

打开可视化界面: http://ip:15672

三.安装php-amqp扩展

CSDN教程: 原文

安装rabbitMQ-c
    1. 下载 (下载不了的话用浏览器下载后上传)
wget https://github.com/alanxz/rabbitmq-c/archive/v0.10.0.zip

unzip v0.10.0.zip && cd rabbitmq-c-0.10.0

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c 

cmake --build . --target install

#软链接
ln -s /usr/local/rabbitmq-c/lib64 /usr/local/rabbitmq-c/lib
安装amqp扩展
  • 1.下载扩展,解压
wget http://pecl.php.net/get/amqp-1.9.4.tgz
tar zxvf amqp-1.9.4.tgz && cd amqp-1.9.4
  • 2.重点: 使用phpize生成安装文件
/www/server/php/74/bin/phpize
  • 3.编译安装php扩展
./configure --with-php-config=/www/server/php/74/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c

make && make install

ls /www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/amqp.so
  • 4.修改php.ini文件,添加扩展(950行左右)
extension=/www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/amqp.so
  • 5.重启php后查看phpinfo()

image.png

四.配置rabbitMQ服务

  • 1.在config目录下创建 rabbitmq.php
<?php
return [
    'default' => [
        'host' => 'localhost',
        'port' => 5672,
        'user' => 'admin',
        'password' => '123456',
        'vhost' => '/',
    ],
    'queues' => [
        'hello' => [
            'exchange' => '', // 交换机名称,不填表示使用默认的
            'queue' => 'hello', //队列名
            'routing_key' => 'hello', // 路由键,一般和队列名相同
        ],
        'queueA' => [
            'exchange' => '',
            'queue' => 'queueA',
            'routing_key' => 'queueA',
        ],
        'queueB' => [
            'exchange' => '',
            'queue' => 'queueB',
            'routing_key' => 'queueB',
        ],
    ],
];
  • 2.创建服务app\common\service\RabbitMQService
<?php

namespace app\common\service;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use think\facade\Config;

class RabbitMQService
{
    private $connection;
    private $channel;

    public function __construct()
    {
        $config = Config::get('rabbitmq.default');
        $this->connection = new AMQPStreamConnection(
            $config['host'],
            $config['port'],
            $config['user'],
            $config['password'],
            $config['vhost']
        );
        $this->channel = $this->connection->channel();
    }

    public function __destruct()
    {
        $this->channel->close();
        $this->connection->close();
    }

    public function sendMessage($message, $queue = 'hello')
    {
        $config = Config::get('rabbitmq.queues.' . $queue);

        $this->channel->queue_declare(
            $config['queue'],
            false,
            false,
            false,
            false
        );

        $msg = new AMQPMessage($message);
        $this->channel->basic_publish($msg, $config['exchange'], $config['routing_key']);

        return true;
    }

    public function receiveMessage($callback, $queue = 'hello')
    {
        $config = Config::get('rabbitmq.queues.' . $queue);

        $this->channel->queue_declare(
            $config['queue'],
            false,
            false,
            false,
            false
        );

        $this->channel->basic_consume(
            $config['queue'],
            '',
            false,
            true,
            false,
            false,
            $callback
        );

        while ($this->channel->is_consuming()) {
            $this->channel->wait();
        }
    }
}

五.在TP6中使用

  • 1.首先创建测试脚本 php think make:command Hello hello
  • 2.在config/console.log下添加 'hello' => 'app\command\Hello',
  • 3.编写测试程序app/command/Hello.php
  • 4.测试 php think hello
<?php
declare (strict_types = 1);
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\service\RabbitMQService;

class Hello extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello')
            ->setDescription('the hello command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('开始');
        $this->mq();
    }
    
    public function mq()
    {
       $rabbitMQService = new RabbitMQService();

       // 发送消息
       $rabbitMQService->sendMessage("Hello, RabbitMQ!");

       // 接收消息
       $callback = function ($msg) {
           echo "接收到消息: " . $msg->body . "\n";
       };
       $rabbitMQService->receiveMessage($callback); // 此方法是阻塞的,只能接收一个队列的消息

       return 'RabbitMQ消息已发送和接收';
    }
}