您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
hyperf命令行及携程隐式分组与显式分组
发布时间:2022-10-28 22:37:35编辑:雪饮阅读()
命令行依赖
/www/server/php/74/bin/php -c /www/server/php/74/etc/php.ini /usr/bin/composer require hyperf/command
创建命令行控制器
/www/server/php/74/bin/php -c /www/server/php/74/etc/php.ini bin/hyperf.php gen:command FooCommand
执行命令行
/www/server/php/74/bin/php -c /www/server/php/74/etc/php.ini bin/hyperf.php demo:command
携程示例1:携程+并发请求+防止对方宕机
<?php
declare(strict_types=1);
namespace App\Command;
use Hyperf\Utils\Exception\ParallelExecutionException;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Parallel;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Hyperf\Guzzle\ClientFactory;
/**
* @Command
*/
#[Command]
class FooCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('demo:command');
}
public function configure()
{
parent::configure();
$this->setDescription('Hyperf Demo Command');
}
public function handle()
{
$total_request_size=100;
$group_size=5;
$parallel = new Parallel($group_size);
$url="https://ysdk.qq.com/auth/qq_check_token";
$ClientFactory=new ClientFactory($this->container);
for ($i = 0; $i < $total_request_size; $i++) {
$parallel->add(function () use($i,$ClientFactory,$url){
$client=$ClientFactory->create();
$options["query"]["timestamp"]=time();
$options["query"]["appid"]="xy220807";
$options["query"]["sig"]="sig";
$options["query"]["openid"]="xy220807";
$options["query"]["openkey"]="openkey";
$options["query"]["userip"]="192.168.31.80";
$client=$client->request("get",$url,$options);
$this->line('当前response:'.var_export($client->getBody()->getContents(),true), 'info');
return Coroutine::id();
});
}
try{
$results = $parallel->wait();
} catch(ParallelExecutionException $e){
// $e->getResults() 获取协程中的返回值。
// $e->getThrowables() 获取协程中出现的异常。
$this->line('发生了异常:'.$e->getMessage(), 'info');
}
$this->line('全部任务结束', 'info');
}
}
携程实例2 并发请求+携程客户端+自定义并发分组
<?php
declare(strict_types=1);
namespace App\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Guzzle\ClientFactory;
use Psr\Container\ContainerInterface;
/**
* @Command
*/
#[Command]
class Foo2Command extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('demo:command2');
}
public function configure()
{
parent::configure();
$this->setDescription('Hyperf Demo Command');
}
public function handle()
{
$group_size=100;
$request_list=[];
//生成所有请求任务
$url="https://ysdk.qq.com/auth/qq_check_token";
//此处连接数过大会触发Connection timed out错误(连接池实现则不会,连接工厂Hyperf\Guzzle\ClientFactory实现则会),
for($i=0;$i<1000;$i++){
$request["id"]=$i+1;
$request_list[$i]=$request;
}
//获取分组数
$group_number=ceil(count($request_list)/$group_size);
//请求分组
$groups=[];
foreach($request_list as $key=>$val){
for($i=0;$i<$group_number;$i++){
//本组key
$key_start=$i*$group_size;
$key_end=$key_start+$group_size-1;
if($key>=$key_start && $key<=$key_end){
$groups[$i][]=$val;
}
}
}
$ClientFactory=new ClientFactory($this->container);
//携程分组列表
foreach($groups as $key=>$group){
$this->line('分组:'.($key+1)."任务开始", 'info');
//创建当前携程分组
$wg = new \Hyperf\Utils\WaitGroup();
$wg->add(count($group));
foreach($group as $gk=>$gv){
co(function () use ($wg,$ClientFactory,$gv,$key,$url) {
$url=$url."?id=".$gv["id"];
//某个请求的业务逻辑
$client=$ClientFactory->create();
$options["query"]["timestamp"]=time();
$options["query"]["appid"]="xy220807";
$options["query"]["sig"]="sig";
$options["query"]["openid"]="xy220807";
$options["query"]["openkey"]="openkey";
$options["query"]["userip"]="192.168.31.80";
$client=$client->request("get",$url,$options);
$this->line('=>=>=>=>分组:'.($key+1).",请求:".$gv["id"]."完成,response:".$client->getBody()->getContents(), 'info');
$wg->done();
});
}
$res=$wg->wait();
$this->line('分组:'.($key+1)."完成,res:".var_export($res,true), 'info');
$this->line("", 'info');
$this->line("", 'info');
$this->line("", 'info');
}
}
}
携程实例3 并发请求+携程客户端+自定义分组+客户端连接池
<?php
declare(strict_types=1);
namespace App\Command;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Guzzle\ClientFactory;
use Hyperf\Guzzle\PoolHandler;
use Hyperf\Guzzle\RetryMiddleware;
use Psr\Container\ContainerInterface;
/**
* @Command
*/
#[Command]
class Foo3Command extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('demo:command3');
}
public function configure()
{
parent::configure();
$this->setDescription('携程GuzzleHttp\Client连接池实现,解决连接工厂Hyperf\Guzzle\ClientFactory实现时携程数过多时出现的Connection timed out错误');
}
public function handle()
{
$group_size=100;
$request_list=[];
//生成所有请求任务
$url="https://ysdk.qq.com/auth/qq_check_token";
for($i=0;$i<10000;$i++){
$request["id"]=$i+1;
$request_list[$i]=$request;
}
//获取分组数
$group_number=ceil(count($request_list)/$group_size);
//请求分组
$groups=[];
foreach($request_list as $key=>$val){
for($i=0;$i<$group_number;$i++){
//本组key
$key_start=$i*$group_size;
$key_end=$key_start+$group_size-1;
if($key>=$key_start && $key<=$key_end){
$groups[$i][]=$val;
}
}
}
$handler = make(PoolHandler::class, [
'option' => [
'max_connections' => 50,
],
]);
$retry = make(RetryMiddleware::class, [
'retries' => 1,
'delay' => 10,
]);
$stack = HandlerStack::create($handler);
$stack->push($retry->getMiddleware(), 'retry');
//携程分组列表
foreach($groups as $key=>$group){
$this->line('分组:'.($key+1)."任务开始", 'info');
//创建当前携程分组
$wg = new \Hyperf\Utils\WaitGroup();
$wg->add(count($group));
foreach($group as $gk=>$gv){
co(function () use ($wg,$gv,$key,$url,$stack) {
$url=$url."?id=".$gv["id"];
//某个请求的业务逻辑
//$client=$ClientFactory->create();
$client = make(Client::class, [
'config' => [
'handler' => $stack,
],
]);
$options["query"]["timestamp"]=time();
$options["query"]["appid"]="xy220807";
$options["query"]["sig"]="sig";
$options["query"]["openid"]="xy220807";
$options["query"]["openkey"]="openkey";
$options["query"]["userip"]="192.168.31.80";
$client=$client->request("get",$url,$options);
$this->line('=>=>=>=>分组:'.($key+1).",请求:".$gv["id"]."完成,response:".$client->getBody()->getContents(), 'info');
$wg->done();
});
}
$res=$wg->wait();
$this->line('分组:'.($key+1)."完成,res:".var_export($res,true), 'info');
$this->line("", 'info');
$this->line("", 'info');
$this->line("", 'info');
}
}
}
关键字词:hyperf,命令行,携程,分组