Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and implementation method of message queue in PHP

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the principle and implementation of message queuing in PHP, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. What is message queuing

Message queuing (English: Message queue) is a way of communicating between processes or between different threads of the same process.

two。 Why use message queuing

Message queuing technology is a technology for exchanging information between distributed applications. Message queues can reside in memory or on disk, and queues store messages until they are read by the application. Through message queuing, applications can execute independently without knowing each other's location or waiting for the receiver to receive this message before continuing execution.

3. When do you use message queuing

First of all, you need to figure out the difference between message queuing and remote procedure calls. When many readers asked me, I found that they needed RPC (remote procedure calls), not message queues.

Message queues are implemented synchronously or asynchronously, usually we use message queues asynchronously, and remote procedure calls often use synchronous methods.

What is the difference between MQ and RPC? MQ usually passes on an irregular protocol, which is user-defined and implements store-and-forward, while RPC is usually a dedicated protocol, and the calling procedure returns the result.

4. When to use message queuing

For synchronization requirements, remote procedure call (PRC) is more suitable for you.

Asynchronous requirements, message queuing is more suitable for you.

At present, many message queuing software also support RPC function, and many RPC systems can also be called asynchronously.

Message queues are used to meet the following requirements

① store and forward

② distributed transaction

③ publish and subscribe

④ content-based routing

⑤ point-to-point connection

5. Who is responsible for handling message queues?

As a common practice, if a small project team can have one person to implement it, including message push, reception and processing. If a large team usually defines a good news protocol and then develops its own parts, for example, one team is responsible for writing the push protocol part and the other team is responsible for writing the receiving and processing part.

So why don't we frame message queues?

Framing has several benefits:

① developers do not need to learn message queuing interface ② developers do not need to care about message push and reception ③ developers push messages through a unified API ④ developers focus on implementing business logic functions

6. How to implement message queue Framework

The following is a SOA framework developed by the author, which provides three interfaces, namely SOAP,RESTful,AMQP (RabbitMQ). After understanding the idea of the framework, it is easy for you to further expand, such as adding support for XML-RPC, ZeroMQ and so on.

Https://github.com/netkiller/SOA

This article only talks about the message queuing framework.

6.1. Daemon process

The message queuing framework is a local application (command line program), and we need to implement a daemon in order for it to run in the background.

Https://github.com/netkiller/SOA/blob/master/bin/rabbitmq.php

Each instance processes a set of queues. Instantiation requires providing three parameters: $queueName = 'queue name', $exchangeName = 'exchange name', and $routeKey = 'routing'

$daemon = new\ framework\ RabbitDaemon ($queueName = 'email', $exchangeName =' email', $routeKey = 'email')

The daemon needs to be run using the root user. After running, the daemon switches to the normal user and creates a process ID file for use when the process is stopped.

Daemon core code https://github.com/netkiller/SOA/blob/master/system/rabbitdaemon.class.php

6.2. Message queuing protocol

A message protocol is an array that serializes or JSON-pushes the array to a message queuing server, using a protocol in json format.

$msg = array ('Namespace'= >' namespace', "Class" = > "Email", "Method" = > "smtp", "Param" = > array ($mail, $subject, $message, null))

Serialized protocol

{"Namespace": "single", "Class": "Email", "Method": "smtp", "Param": ["netkiller@msn.com", "Hello", "TestHelloWorld", null]}

The json format is used for generality so that the push side can use any language. If compatibility is not considered, binary serialization is recommended, for example, msgpack is more efficient.

6.3. Message queuing processing

Message queuing processing core code

Https://github.com/netkiller/SOA/blob/master/system/rabbitmq.class.php

So the message is processed in the following code

$this- > queue- > consume (function ($envelope, $queue) {$speed = microtime (true); $msg = $envelope- > getBody (); $result = $this- > loader ($msg); $queue- > ack ($envelope- > getDeliveryTag ()); / / send ACK reply / / $this- > logging- > info ('. $msg.''. $result) $this- > logging- > debug ('Protocol:'. $msg.''); $this- > logging- > debug ('Result:'. $result.''); $this- > logging- > debug ('Time:'. (microtime (true)-$speed).');})

Public function loader ($msg = null) is responsible for disassembling the protocol, then loading the corresponding class file, passing parameters, running methods, and feedback the results.

Time can output the time it takes for a program to run, which is useful for later optimization.

Prompt

Loader () can be further optimized to use multithreading to submit tasks to the thread pool every time loader is called, so that the message queue can be processed by multithreading.

6.4. test

Test code https://github.com/netkiller/SOA/blob/master/test/queue/email.php

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report