search
HomePHP FrameworkThinkPHPHow to use rabbitmq in thinkPHP5

How to use rabbitmq in thinkPHP5

Aug 23, 2019 am 10:42 AM
rabbitmqthinkphp5

How to use rabbitmq in thinkPHP5

thinkPHP5 How to use rabbitmq?

After installing the rabbitmq extension of tp5, add the file rabbitmq.php to the project root directory file to boot rabbitmq.

<?php
define(&#39;APP_PATH&#39;, __DIR__ . &#39;/application/&#39;);
define(&#39;BIND_MODULE&#39;,&#39;rabbitmq/Client&#39;);
// 加载框架引导文件
require __DIR__ . &#39;/thinkphp/start.php&#39;;

Generator

  private function queueEvent($message)
    {
//        error_log("\n******" . date("His") . "********\n" . print_r($message, 1) . "\n*************\n", 3, 
&#39;messag_event.log&#39;);
        dump($message);
        //设置你的连接
        $conn_args = array(&#39;host&#39; => &#39;ip&#39;, &#39;port&#39; => &#39;5672&#39;, &#39;login&#39; => &#39;ymq&#39;, &#39;password&#39; => &#39;123456&#39;,
        &#39;vhost&#39;=>&#39;/&#39;);
 
 
        $content = $message;
//创建连接和channel
        $conn = new \AMQPConnection($conn_args);
        if (!$conn->connect()) {
            die("Cannot connect to the broker!\n");
        }
        $channel = new \AMQPChannel($conn);
 
//创建交换机
        $e_name = &#39;MQTT_device_event&#39;; //交换机名
        $ex = new \AMQPExchange($channel);
        $ex->setName($e_name);
//        $ex->setType(AMQP_EX_TYPE_TOPIC); //direct类型
        $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型
        $ex->setFlags(AMQP_DURABLE); //持久化
        $ex->declareExchange();
    }

Related recommendations: "ThinkPHP Tutorial"

Run php directory run producer

Consumer

 public function index()
    {
        //连接RabbitMQ
        $conn_args = array(&#39;host&#39; => &#39;ip&#39;, &#39;port&#39; => &#39;5672&#39;, &#39;login&#39; => &#39;ymq&#39;, &#39;password&#39; => &#39;123456&#39;, &#39;vhost&#39; 
        => &#39;/&#39;);
 
        $e_name = &#39;MQTT_device_event&#39;; //交换机名
        $q_name = &#39;q_event&#39;; //队列名
        $k_route = &#39;key_event&#39;; //路由key 
//创建连接和channel
        $conn = new \AMQPConnection($conn_args);
        if (!$conn->connect()) {
            die("Cannot connect to the broker!\n");
        }
        $channel = new \AMQPChannel($conn); 
//创建交换机
        $ex = new \AMQPExchange($channel);
        $ex->setName($e_name);
        $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型
        $ex->setFlags(AMQP_DURABLE); //持久化
        $ex->declareExchange();
//创建队列
        $q = new \AMQPQueue($channel);
        $q->setName($q_name);
        $q->setFlags(AMQP_DURABLE); //持久化
        $q->declareQueue();     //最好队列object在这里declare()下,否则如果是新的queue会报错 
//绑定交换机与队列,并指定路由键,可以多个路由键
        $q->bind($e_name, $k_route);
//$q->bind($e_name, &#39;key_33&#39;);  
//阻塞模式接收消息
        echo "Message:\n";
        while(True){
            $q->consume(function($envelope, $queue) {
                $msg = $envelope->getBody();
                //处理数据
                echo $msg . PHP_EOL; //处理消息
                $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
            });
            //$q->consume(&#39;processMessage&#39;, AMQP_AUTOACK); //自动ACK应答
        }
 
        $conn->disconnect(); 
    }

Execute the command php rabbitmq

Start it

Check whether the queue is consumed

Log in to http://127.0.0.115672/# /queues Address

How to use rabbitmq in thinkPHP5How to use rabbitmq in thinkPHP5

The above is the detailed content of How to use rabbitmq in thinkPHP5. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment