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('APP_PATH', __DIR__ . '/application/'); define('BIND_MODULE','rabbitmq/Client'); // 加載框架引導文件 require __DIR__ . '/thinkphp/start.php';
Generator
private function queueEvent($message) { // error_log("\n******" . date("His") . "********\n" . print_r($message, 1) . "\n*************\n", 3, 'messag_event.log'); dump($message); //設(shè)置你的連接 $conn_args = array('host' => 'ip', 'port' => '5672', 'login' => 'ymq', 'password' => '123456', 'vhost'=>'/'); $content = $message; //創(chuàng)建連接和channel $conn = new \AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker!\n"); } $channel = new \AMQPChannel($conn); //創(chuàng)建交換機 $e_name = 'MQTT_device_event'; //交換機名 $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('host' => 'ip', 'port' => '5672', 'login' => 'ymq', 'password' => '123456', 'vhost' => '/'); $e_name = 'MQTT_device_event'; //交換機名 $q_name = 'q_event'; //隊列名 $k_route = 'key_event'; //路由key //創(chuàng)建連接和channel $conn = new \AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker!\n"); } $channel = new \AMQPChannel($conn); //創(chuàng)建交換機 $ex = new \AMQPExchange($channel); $ex->setName($e_name); $ex->setType(AMQP_EX_TYPE_DIRECT); //direct類型 $ex->setFlags(AMQP_DURABLE); //持久化 $ex->declareExchange(); //創(chuàng)建隊列 $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, 'key_33'); //阻塞模式接收消息 echo "Message:\n"; while(True){ $q->consume(function($envelope, $queue) { $msg = $envelope->getBody(); //處理數(shù)據(jù) echo $msg . PHP_EOL; //處理消息 $queue->ack($envelope->getDeliveryTag()); //手動發(fā)送ACK應答 }); //$q->consume('processMessage', 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
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

How to ensure that messages are not lost. The rabbitmq message delivery path producer->switch->queue->consumer is generally divided into three stages. 1. The producer ensures the reliability of message delivery. 2.MQ internal messages are not lost. 3. Consumer consumption is successful. What is message delivery reliability? Simply put, it means that messages are 100% sent to the message queue. We can turn on confirmCallback. After the producer delivers the message, mq will give the producer an ack. Based on the ack, the producer can confirm whether the message is sent to mq. Turn on confirmCallback and modify the configuration file #NONE: disable the release confirmation mode, which is the default value , CORRELATED:

How to use RabbitMQ to implement distributed message processing in PHP Introduction: In large-scale application development, distributed systems have become a common requirement. Distributed message processing is a pattern that improves the efficiency and reliability of the system by distributing tasks to multiple processing nodes. RabbitMQ is an open source, reliable message queuing system that uses the AMQP protocol to implement message delivery and processing. In this article we will cover how to use RabbitMQ in PHP for distribution

As modern applications increase in complexity, messaging has become a powerful tool. In this area, RabbitMQ has become a very popular message broker that can be used to deliver messages between different applications. In this article, we will explore how to use RabbitMQ in Go language. This guide will cover the following: Introduction to RabbitMQ RabbitMQ Installation RabbitMQ Basic Concepts Getting Started with RabbitMQ in Go RabbitMQ and Go

With the advent of the Internet era, message queue systems have become more and more important. It enables asynchronous operations between different applications, reduces coupling, and improves scalability, thereby improving the performance and user experience of the entire system. In the message queuing system, RabbitMQ is a powerful open source message queuing software. It supports a variety of message protocols and is widely used in financial transactions, e-commerce, online games and other fields. In practical applications, it is often necessary to integrate RabbitMQ with other systems. This article will introduce how to use sw

Now more and more companies are beginning to adopt the microservice architecture model, and in this architecture, message queues have become an important communication method, among which RabbitMQ is widely used. In the Go language, go-zero is a framework that has emerged in recent years. It provides many practical tools and methods to allow developers to use message queues more easily. Below we will introduce go-zero based on practical applications. And the usage and application practice of RabbitMQ. 1.RabbitMQ OverviewRabbit

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

Introduction to the solution for real-time data synchronization between Golang and RabbitMQ: In today's era, with the popularity of the Internet and the explosive growth of data volume, real-time data synchronization has become more and more important. In order to solve the problems of asynchronous data transmission and data synchronization, many companies have begun to use message queues to achieve real-time synchronization of data. This article will introduce a real-time data synchronization solution based on Golang and RabbitMQ, and provide specific code examples. 1. What is RabbitMQ? Rabbi
