Home Backend Development PHP Tutorial PHP asynchronous coroutine development: building a highly available online consultation system

PHP asynchronous coroutine development: building a highly available online consultation system

Dec 02, 2023 pm 02:09 PM
- php development - Asynchronous coroutines - Online consultation system

PHP asynchronous coroutine development: building a highly available online consultation system

PHP asynchronous coroutine development: building a highly available online consultation system

在当今互联网时代,客户体验已成为企业竞争的关键因素之一。随着移动互联网的普及,业务处理速度和响应时间越来越成为用户选择的重要指标。在线咨询系统是供用户与客服进行实时交流的一种应用,被广泛应用于电商、客服、在线教育等领域。但同时,高并发和大流量的极限压力也对在线咨询系统提出了更高的要求。利用PHP异步协程技术,构建高可用的在线咨询系统可以有效地解决这一难题。

PHP异步协程简介

传统的PHP异步编程模型无法充分利用现代计算机的多核和异构计算能力,无法满足高并发和大量I/O密集型操作的需求。PHP应用程序在处理请求的时候,通常需要等待I/O操作(如网络请求、数据库查询等)完成后才能进行下一步操作。这时会出现阻塞现象,导致程序无法充分利用CPU资源,从而影响应用的并发能力和性能。

PHP协程是一种轻量级的线程,可以在一个线程内部创建多个协程,通过yield函数在不同的协程之间切换执行上下文。PHP7.0.0以后版本的swoole扩展实现了对协程的支持,可以通过swoole协程提供的一系列API来构建高性能的异步编程模型。利用PHP协程技术,可以很好地解决阻塞IO的问题,提高应用程序的并发性和性能。

在线咨询系统设计

在线咨询系统的主要功能是客服与用户之间的实时交流,需要实现多个客服同时在线服务多个用户。用户与客服之间需要实时交互,需要对话的发送和接收进行快速响应处理。在线咨询系统需要实时监控客户端和服务端的连接状态,及时处理异常情况,保证系统的高可用性和稳定性。

下面我们通过一个简单的在线咨询系统来具体介绍异步协程在实际应用中的使用。

1.服务器配置

考虑到在线咨询服务的高并发需求,我们需要将服务器的Nginx配置修改为upstream模块,支持反向代理和负载均衡。我们使用4台配置相同的服务器,通过Nginx反向代理进行负载均衡和高可用性。

upstream backend {

server 192.168.1.100:80;
server 192.168.1.101:80;
server 192.168.1.102:80;
server 192.168.1.103:80;
Copy after login

}

server {

listen 80;
server_name chat.example.com;
location / {
    proxy_pass http://backend;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Copy after login

}

2.客户端页面

我们通过Vue.js框架构建一个简单的客户端聊天室页面,提供客户端和服务端之间的实时通讯功能。

<script><br>export default {<br> data () {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>return { ws: null, messages: [], msg: '' }</pre><div class="contentsignin">Copy after login</div></div><p>},<br> mounted () {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>this.initWebSocket()</pre><div class="contentsignin">Copy after login</div></div><p>},<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>initWebSocket () { if (this.ws) { this.ws.close() } const url = 'ws://' + window.location.host + '/ws' this.ws = new WebSocket(url) this.ws.onopen = () =&gt; { this.addMessage('已连接到聊天服务器') } this.ws.onmessage = (event) =&gt; { this.addMessage(event.data) } this.ws.onclose = () =&gt; { this.addMessage('聊天服务器已关闭') this.ws = null } this.ws.onerror = (event) =&gt; { this.addMessage('聊天服务器连接错误') } }, sendMessage () { if (!this.msg) { return } if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { this.addMessage('无法连接到聊天服务器') return } this.ws.send(this.msg) this.addMessage('我: ' + this.msg) this.msg = '' }, addMessage (message) { this.messages.push(message) this.$nextTick(() =&gt; { const scrollHeight = this.$el.scrollHeight this.$el.scrollTop = scrollHeight }) }</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>

3.服务端程序

在服务端实现中,我们使用swoole协程库提供的WebSocketServer类来实现WebSocket通信功能。WebSocketServer基于swoole的异步协程模型,可以支持高并发无阻塞的通信。在onMessage回调函数中处理客户端发送的消息,并将消息发送给所有已连接的客户端。onClose回调函数用于处理客户端离线事件。

$server = new SwooleWebSocketServer('0.0.0.0', 80);

$server->on('open', function (SwooleWebSocketServer $server, SwooleHttpRequest $request) {

echo "connection open: {$request->fd}
Copy after login

";
});

$server->on('message', function (SwooleWebSocketServer $server, SwooleWebSocketFrame $frame) {

echo "received message: {$frame->data}
Copy after login

";

foreach ($server->connections as $fd) {
    if ($server->isEstablished($fd)) {
        $server->push($fd, $frame->data);
    }
}
Copy after login

});

$server->on('close', function (SwooleWebSocketServer $server, $fd) {

echo "connection close: {$fd}
Copy after login

";
});

$server->start();

通过构建以上的客户端和服务端程序,我们实现了一个简单的在线咨询系统。但实际上,如果直接使用这个程序,系统的可用性和性能并不能满足我们的需求。接下来,针对在线咨询系统的高并发和大流量的极限压力,我们将利用PHP异步协程技术对系统进行进一步的优化。

4.异步协程优化

微信公众号后台是一个使用在线咨询系统的示例。在微信公众号后台中,一个客服可能需要服务数千个用户,系统需要处理大量的连接和消息发送操作。如果使用传统的同步编程方式,将会对服务器性能和响应时间造成严重的影响。

我们可以使用swoole协程库提供的一些API来实现异步操作,提高应用程序的性能和并发能力。下面是一个针对连接初始化的异步协程客户端初始化函数的实现:

function connectAsync($host, $port)
{

$client = new SwooleCoroutineClient(SWOOLE_SOCK_TCP);
$client->set([
    'open_length_check' => true,
    'package_max_length' => 1024 * 1024,
    'package_length_type' => 'N',
    'package_body_offset' => 4,
]);
$result = $client->connect($host, $port);
if (!$result) {
    throw new Exception('Failed to connect');
}
return $client;
Copy after login

}

使用协程客户端初始化函数后,我们可以在连接建立后异步发送和接收消息。下面实现一个异步协程客户端函数,用于向服务端发送消息:

function sendMessageAsync($client, $message)
{

$data = pack('N', strlen($message)) . $message;
$result = $client->send($data);
if (!$result) {
    throw new Exception('Failed to send message');
}
return true;
Copy after login

}

消息发送函数实现了在异步协程客户端上的消息发送操作,返回true代表发送成功。这里需要注意的是,协程客户端的send()方法并不是真正意义上的异步操作,它实际上是将消息写入缓冲区,调用yield语句后将会立即返回流程执行到协程栈中的其他协程。真正的异步操作是在协程内部的swoole_event_add()函数中实现。

下面是一个异步协程的消息接收函数的实现:

function recvMessageAsync($client, $timeout = 0.5)
{

$result = $client->recv(1024 * 1024, $timeout);
if ($result === '') {
    throw new Exception('Connection closed');
}
if (!$result) {
    throw new Exception('Failed to receive message');
}
$length = unpack('N', substr($result, 0, 4))[1];
$message = trim(substr($result, 4, $length));
return $message;
Copy after login

}

在消息接收函数中,我们调用recv()方法来接收响应并返回消息体。如果响应超时或连接异常,则抛出异常表示接收失败。

对于高并发的在线咨询系统,我们也可以使用swoole协程库提供的协程HTTP客户端实现异步Http请求。下面是一个异步Http请求函数的实现:

function requestAsync($url, $timeout = 0.5)
{

$cli = new SwooleHttpClient('127.0.0.1', 80);
$cli->set([
    'timeout' => $timeout,
]);
$cli->setHeaders([
    'Host' => 'localhost',
    'User-Agent' => 'swoole-http-client',
    'Accept-Encoding' => 'gzip',
]);
$cli->post($url, [
    'name' => 'swoole',
]);
return $cli;
Copy after login

}

使用协程HTTP客户端,我们可以在程序中发出大量的异步Http请求并处理响应,优化在线咨询系统的性能和响应时间。

需要注意的是,在协程中如果调用了不支持协程的阻塞函数,会导致协程堵塞,影响程序性能和响应效率。因此在使用协程时需要注意选择支持协程的方式,并避免使用不支持协程的函数。

总结

利用PHP异步协程技术,结合Nginx反向代理和负载均衡,我们可以实现高缩力度在线咨询系统。异步协程编程模型可以提高应用程序的并发性和性能,并且具有极低的系统开销和响应时间。在实际应用中,我们需要适当地使用异步协程优化程序性能,提高系统的可用性和稳定性。

The above is the detailed content of PHP asynchronous coroutine development: building a highly available online consultation system. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Mall PHP membership level skills control Mall PHP membership level skills control Jun 30, 2023 am 10:41 AM

Tips for implementing membership level system in PHP Developer City With the continuous development of e-commerce, more and more companies are beginning to establish their own online malls. In the mall, the membership level system is an important function, which can motivate users to consume and improve customer stickiness. This article will introduce the implementation skills of the membership level system in PHP Developer City. 1. Membership level division strategy Before starting to develop the membership level system, we need to determine the membership level division strategy. Common strategies for classifying membership levels include consumption amount, points, and bets.

How to develop a simple blog system using PHP How to develop a simple blog system using PHP Sep 22, 2023 am 10:01 AM

How to use PHP to develop a simple blog system With the popularity of the Internet, blogs have become an important platform for people to share their ideas and experiences. If you have a certain understanding of PHP programming and want to develop a simple blog system, this article will introduce it to you in detail. Install PHP and MySQL First, make sure PHP and MySQL are installed on your machine. You can download PHP and MySQL respectively from the official website and install them according to the installation instructions. Create a databaseCreate a database in MySQL

How to use PHP to develop Exchange mailbox functions How to use PHP to develop Exchange mailbox functions Sep 11, 2023 am 11:12 AM

How to use PHP to develop Exchange mailbox functions Introduction: With the development of the Internet, email has become an indispensable part of people's lives and work. As a commonly used enterprise email service, Exchange mailbox has powerful functions and reliable performance, and is widely favored by enterprise users. This article will introduce how to use PHP to develop Exchange mailbox functions to help readers get started quickly and conduct customized development. Part 1: Set up a PHP development environment First, we need to set up a PHP development environment

Alibaba Cloud OCR and PHP development: a practical tutorial example Alibaba Cloud OCR and PHP development: a practical tutorial example Jul 19, 2023 pm 04:29 PM

Alibaba Cloud OCR and PHP Development: A Practical Tutorial Example Introduction With the development of artificial intelligence and big data technology, OCR (Optical Character Recognition, optical character recognition) technology is increasingly used in various fields. Alibaba Cloud OCR is an excellent OCR solution provided by Alibaba Cloud, which can realize the recognition, extraction and conversion of text in images. This article will introduce how to use Alibaba Cloud OCR and PHP for development, and give a practical tutorial example. Preparatory work

PHP development of WeChat public account: how to create interactive Q&A PHP development of WeChat public account: how to create interactive Q&A Oct 27, 2023 pm 12:19 PM

PHP development of WeChat public accounts: How to create interactive Q&A, specific code examples are needed. With the popularity of WeChat public accounts, more and more people are paying attention to how to implement interactive Q&A functions in public accounts. This article will introduce how to use PHP to develop WeChat public accounts and provide specific code examples to help readers quickly implement interactive Q&A functions. 1. Set up a development environment. Before starting development, we need to set up a PHP development environment. First, you need to install a PHP runtime environment, such as XAMPP or WAMP. then you

How to implement the product promotion recommendation function in PHP Developer City How to implement the product promotion recommendation function in PHP Developer City Jun 30, 2023 am 08:44 AM

Implementation method of product promotion recommendation function in PHP Developer City Introduction: In the field of e-commerce, promotional activities are a very important strategy, which can help merchants increase sales and attract more customers. When developing a shopping mall website, an effective product promotion recommendation function can help merchants quickly promote promotions and display them to users. This article will discuss how to use PHP to implement the product promotion recommendation function in the mall. 1. Understand the needs Before starting to implement the product promotion recommendation function, we need to clarify

PHP development example: making an online audio and video community PHP development example: making an online audio and video community Oct 27, 2023 pm 02:36 PM

In today's digital era, the Internet has become an indispensable part of people's lives. In this era of information explosion, people are eager to obtain a variety of entertainment and social experiences through the Internet. The online audio and video community emerged as the times require, providing a platform for users to browse, upload, share and comment on various audio and video contents. This article takes this as a background and uses PHP development as an example to introduce how to create an online audio and video community. First of all, in order to ensure the security and reliability of the website, we need to choose a suitable development environment and tools. PHP is

PHP asynchronous coroutine development: building a highly available online consultation system PHP asynchronous coroutine development: building a highly available online consultation system Dec 02, 2023 pm 02:09 PM

PHP asynchronous coroutine development: building a highly available online consultation system In today's Internet era, customer experience has become one of the key factors for enterprise competition. With the popularity of mobile Internet, business processing speed and response time have increasingly become important indicators for user selection. The online consultation system is an application for real-time communication between users and customer service. It is widely used in e-commerce, customer service, online education and other fields. But at the same time, the extreme pressure of high concurrency and large traffic also puts higher requirements on the online consultation system. Use PHP asynchronous coroutine technology to build high-quality

See all articles