当前位置: 首页 > 后端技术 > PHP

Swoolev4.8.0发布,新增SwooleDashboard面板

时间:2023-03-29 17:22:35 PHP

距离上一个版本v4.7.1发布已经将近两个月了,v4.8.0版本终于发布了。此版本包含新功能、错误修复和向后不兼容的更改。不兼容的变化在基本模式下,onStart回调总是会在第一个工作进程(workerid为0)启动时回调,然后再执行onWorkerStart。协程API始终可以在onStart函数中使用。当Worker-0出现致命错误重启时,onStart会再次被回调。在之前的版本中,只有一个worker进程时,onStart会在Worker-0中回调。当有多个worker进程时在Manager进程中执行。admin_server这个版本重要的功能是增加了admin_server选项,用于提供API服务,可以在SwooleDashboard面板查看当前的服务信息,比如PHP加载的扩展,文件,类,函数,常量,以及Swoole相关的进程,协程,连接信息等。//创建一个Server对象,监听127.0.0.1:9501端口$server=newSwoole\Server('127.0.0.1',9501);$server->set(['admin_server'=>'0.0.0.0:9502',//启用admin_server服务'worker_num'=>2,'task_worker_num'=>3]);//监听连接传入事件$server->on('Connect',function($server,$fd){echo"Client:Connect.\n";});//监听数据接收事件$server->on('Receive',function($server,$fd,$reactor_id,$data){$server->send($fd,"Server:{$data}");});//监听连接关闭事件$server->on('Close',function($server,$fd){echo"Client:Close.\n";});//启动服务器$server->start();更新Swoolev4.8.0后可前往https://dashboard.swoole.com/体验。登录时配置本地admin_server地址或云地址,如:http://127.0.0.1:9502/,登录后右上角还可以配置其他地址。注意:部分功能受限,需要安装ext-swoole_plus。另外还增加了一些新的API:Table::stats、Coroutine::join等。下面我们详细看一下:Coroutine::join并发执行多个协程。Swoole\Coroutine::join(array$cid_array,float$timeout=-1):bool$timeout为总超时时间,超时后立即返回。但是正在运行的协程会不停的继续执行useSwoole\Coroutine\go;usefunctionSwoole\Coroutine\run;run(function(){$status=Coroutine::join([go(function()use(&$result){$result['baidu']=strlen(file_get_contents('https://www.baidu.com/'));}),go(function()use(&$result){$result['zhihu']=strlen(file_get_contents('https://www.zhihu.com/'));})],1);var_dump($result,$status);});addCommand/command提供了SwooleDashboard的API基于addCommand,代码位于库中。除了库中提供的命令外,swoole扩展中也有一些。当然也可以自定义:Swoole\Server->addCommand(string$name,int$accepted_process_types,callable$callback)$server->addCommand('test_getpid',SWOOLE_SERVER_COMMAND_MASTER|SWOOLE_SERVER_COMMAND_EVENT_WORKER,function($server){returnjson_encode(['pid'=>posix_getpid()]);});command方法用于调用服务端定义的接口:Swoole\Server->command(string$name,int$process_id,int$process_type,$data,bool$json_decode=true)$server->command('test_getpid',0,SWOOLE_SERVER_COMMAND_MASTER,['type'=>'master']);onBeforeShutdown添加onBeforeShutdown事件回调,其中可以使用coroutineAPI。安全提示onStart回调中可以使用异步和协程API,但需要注意的是这可能会与dispatch_func和package_length_func冲突,请不要同时使用。Coroutine::getStackUsage()获取当前PHP堆栈的内存使用情况。Swoole\Coroutine::getStackUsage([$cid]):intTable::stats用于获取Swoole\Table的状态。使用Swoole\Table;$table=newTable(1024);$table->column('string',Table::TYPE_STRING,256);$table->create();$table->set('swoole',['string'=>'www.swoole.com']);var_dump($table->stats());//array(8){//["num"]=>//int(1)//["conflict_count"]=>//int(0)//["conflict_max_level"]=>//int(0)//["insert_count"]=>//int(1)//["update_count"]=>//int(0)//["delete_count"]=>//int(0)//["available_slice_num"]=>//int(204)//["total_slice_num"]=>//int(204)//}更新日志下面是完整的更新日志:向下不兼容的变化在base模式下,onStart回调总是会在第一个worker进程(workerid为0)启动时回调,在onWorkerStart执行之前(#4389)(@matyhtf)新增API新增Coroutine::getStackUsage()方法(#4398)(@matyhtf)(@twose)新增Coroutine\Redis部分API(#4390)(@chrysanthemum)新增Table::stats()方法(#4405)(@matyhtf)添加Coroutine::join()方法(#4406)(@matyhtf)新功能支持服务器命令(#4389)(@matyhtf)支持Server::onBeforeShutdown事件回调(#4415)(@matyhtf)增强设置Websocketpack失败时的错误代码(swoole/swoole-src@d27c5a5)(@matyhtf)添加了Timer::exec_count字段(#4402)(@matyhtf)hookmkdir支持使用open_basedirini配置(#4407)(@NathanFreeman)添加了库vendor_init.php脚本(swoole/library@6c40b02)(@matyhtf)SWOOLE_HOOK_CURL支持CURLOPT_UNIX_SOCKET_PATH(swoole/library#121)(@sy-records)Client支持设置ssl_ciphers配置项(#4432)(@amuluowin)为Server::stats()添加了一些新信息(#4410)(#4412)(@matyhtf)修复上传文件时不必要的文件名URL解码(swoole/swoole-src@a73780e)(@matyhtf)修复HTTP2max_frame_size问题(#4394)(@twose)修复curl_multi_selectbug#4393(#4418)(@matyhtf)修复缺少协程选项(#4425)(@sy-records)修复发送缓冲区满时无法关闭连接的问题(swoole/swoole-src@2198378)(@matyhtf)