workerman5.0篇〡异步非阻塞协程HTTP客户端
概述
workerman/http-client 是一个异步http客户端组件。所有请求响应异步非阻塞,内置连接池,消息请求和响应符合PSR7规范。
Workerman 5.0 版本中的异步HTTP协程客户端组件是一个基于PHP协程的高性能HTTP客户端,它能够充分利用PHP的异步特性来提高HTTP请求的效率和性能。这个组件允许开发者在编写PHP代码时,以同步的方式发送异步HTTP请求,从而使得编写的代码更加简洁易懂,同时也能够处理大量的并发请求。
特点
-
异步非阻塞:所有的请求和响应都是异步进行的,不会阻塞主线程,这意味着可以同时处理多个HTTP请求和响应。
-
内置连接池:为了提高效率和性能,该组件内置了连接池,可以复用TCP连接,减少建立和关闭连接的开销。
-
符合PSR-7规范:消息请求和响应都符合PSR-7规范,这使得它能够与遵循该规范的其他PHP组件和库无缝集成。
-
支持多种协议:除了HTTP和HTTPS协议,该组件还支持WebSocket、WSS等协议,使其能够应对更多的应用场景。
安装
composer require workerman/http-client
开源技术小栈注意:协程用法需要
workerman>=5.0
、workerman/http-client>=2.0.0
并安装composer require revolt/event-loop ^1.0.0
启动webman
/var/www/webman/admin.webman.tinywan.com # php start.php start
Workerman[start.php] start in DEBUG mode
------------------------------------------------------- WORKERMAN --------------------------------------------------------
Workerman/5.0.0 PHP/8.2.10 (Jit off) Linux/5.15.167.4-microsoft-standard-WSL2
-------------------------------------------------------- WORKERS ---------------------------------------------------------
event-loop proto user worker listen count state
revolt tcp root webman http://0.0.0.0:8288 24 [OK]
revolt tcp root monitor none 1 [OK]
--------------------------------------------------------------------------------------------------------------------------
协程用法
WorkerMan 中使用
declare(strict_types=1);
use Workerman\Worker;
require_once '../vendor/autoload.php';
try {
$worker = new Worker();
$worker->onWorkerStart = function () {
$http = new Workerman\Http\Client();
$response = $http->get('https://www.tinywan.com/');
var_dump($response->getStatusCode());
echo $response->getBody() . PHP_EOL;
$response = $http->post('https://www.tinywan.com/', ['key1' => 'value1', 'key2' => 'value2']);
var_dump($response->getStatusCode());
echo $response->getBody() . PHP_EOL;
$response = $http->request('https://www.tinywan.com/', [
'method' => 'GET',
'version' => '1.1',
'headers' => ['Connection' => 'keep-alive'],
'data' => ['key1' => 'value1', 'key2' => 'value2'],
]);
echo $response->getBody() . PHP_EOL;
};
Worker::runAll();
} catch (Throwable $throwable) {
var_dump($throwable->getMessage());
}
http-client 协程异步并发
<?php
/**
* @desc 伪代码
* @author Tinywan(ShaoBo Wan)
*/
declare(strict_types=1);
use Workerman\Worker;
use \Workerman\Connection\TcpConnection;
use \Workerman\Protocols\Http\Request;
require_once '../vendor/autoload.php';
// 创建一个Worker监听8217端口,使用http协议通讯
$httpWorker = new Worker("http://0.0.0.0:8217");
// 启动8个进程对外提供服务
$httpWorker->count = 8;
// 接收到浏览器发送的数据时回复给浏览器
$httpWorker->onMessage = function (TcpConnection $connection, Request $request) {
$http = new \Workerman\Http\Client();
$count = 50;
$result = [];
while ($count--) {
$startTime = microtime(true);
echo '开始时间:' . $startTime . PHP_EOL;
$response = $http->get('https://api.tinywan.com/systems/website');
$endTime = microtime(true);
echo '结束时间:' . $endTime . PHP_EOL;
$result[] = sprintf('第%d个 | 耗时%s秒 | 状态码%d', $count, $endTime - $startTime, $response->getStatusCode());
}
$connection->send(json_encode($result));
};
Worker::runAll();
webman 中使用
如果你需要在webman中使用异步http请求并将结果返回给前端,参考以下用法
<?php
declare(strict_types=1);
namespace app\controller;
use support\Request;
use support\Response;
use Throwable;
use Workerman\Protocols\Http\Chunk;
class CoroutineController
{
/**
* @param Request $request
* @return Response
* @throws Throwable
*/
public function index(Request $request): Response
{
$connection = $request->connection;
$http = new \Workerman\Http\Client();
$http->get('https://api.tinywan.com/website', function ($response) use ($connection) {
$connection->send(new Chunk($response->getBody()->getContents()));
$connection->send(new Chunk('')); // 发送空的的chunk代表response结束
});
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
"Access-Control-Allow-Origin" => "*"
]);
}
}
以上用法是先给客户端返回一个带chunked
的http
头,然后将数据以chunk
的方式发送给客户端。