hyperf2.1中使用guzzlehttp/guzzle包模拟并发请求流程步骤
Guzzle这个包基本上只要是phper都知道,我就不多说了,直接进入主题
环境
centos7
hyperf2.1 (有兴趣的 可以升级2.2了)
路由:
注解
以前的文章有教程的
这里我用我的网站作为并发测试的目的url
https://www.zongscan.com/indextest?v=1
因为是测试,所以直接retrun get参数,方便看效果
代码:
if(input('?v')) { return input('v');}
hyperf中测试控制器代码:
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\View\RenderInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class TestController
{
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function index(RenderInterface $render,ResponseInterface $response,RequestInterface $request)
{
//return $response->json('测试专用控制器');
return $response->json([
"errno"=>0,
"data" => '/upload/image/'
]);
}
/**
* 模拟并发
*/
public function imitateconcurrency()
{
// 模拟并发请求代码
// $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
$options = ['base_uri' => 'https://www.zongscan.com/indextest'];
// $client 为协程化的 GuzzleHttp\Client 对象
$client = $this->clientFactory->create($options);
//$resp = $client->request('GET', $url,[]);
$promises = [
'a' => $client->getAsync('?v=1'),
'b' => $client->getAsync('?v=2'),
'c' => $client->getAsync('?v=3'),
'd' => $client->getAsync('?v=4')
];
//异步运行
$results = \GuzzleHttp\Promise\unwrap($promises);
echo $results['a']->getBody()->getContents();
echo "\n";
echo $results['b']->getBody()->getContents();
echo "\n";
echo $results['c']->getBody()->getContents();
echo "\n";
echo $results['d']->getBody()->getContents();
echo "\n";
var_dump($results);
//return $results;
}
}
直接跑一下,截张效果图
异步返回的数据也贴一下
array(4) {
["a"]=>
object(GuzzleHttp\Psr7\Response)#144991 (6) {
["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
string(2) "OK"
["statusCode":"GuzzleHttp\Psr7\Response":private]=>
int(200)
["headers":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["server"]=>
array(1) {
[0]=>
string(5) "nginx"
}
["date"]=>
array(1) {
[0]=>
string(29) "Tue, 24 Aug 2021 03:10:08 GMT"
}
["content-type"]=>
array(1) {
[0]=>
string(24) "text/html; charset=utf-8"
}
["transfer-encoding"]=>
array(1) {
[0]=>
string(7) "chunked"
}
["connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["x-powered-by"]=>
array(1) {
[0]=>
string(10) "PHP/7.3.11"
}
["content-encoding"]=>
array(1) {
[0]=>
string(4) "gzip"
}
}
["headerNames":"GuzzleHttp\Psr7\Response":private]=>
array(7) {
["server"]=>
string(6) "server"
["date"]=>
string(4) "date"
["content-type"]=>
string(12) "content-type"
["transfer-encoding"]=>
string(17) "transfer-encoding"
["connection"]=>
string(10) "connection"
["x-powered-by"]=>
string(12) "x-powered-by"
["content-encoding"]=>
string(16) "content-encoding"
}
["protocol":"GuzzleHttp\Psr7\Response":private]=>
string(3) "1.1"
["stream":"GuzzleHttp\Psr7\Response":private]=>
object(GuzzleHttp\Psr7\Stream)#144992 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(2043) of type (stream)
["size":"GuzzleHttp\Psr7\Stream":private]=>
NULL
["seekable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["readable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["writable":"GuzzleHttp\Psr7\Stream":private]=>
bool(true)
["uri":"GuzzleHttp\Psr7\Stream":private]=>
string(10) "php://temp"
["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
array(0) {
}
}
}
太长了,就直贴索引a的,其他的都一模一样
完
相关文章