Getting Started with EasySwoole Basics
使用 Composer 安装
composer require easyswoole/easyswoole=3.x php vendor/bin/easyswoole install
启动框架
php easyswoole start
nginx转发
server { root /data/wwwroot/; server_name local.easyswoole.com; location / { proxy_http_version 1.1; proxy_set_header Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; if (!-e $request_filename) { proxy_pass http://127.0.0.1:9501; } if (!-f $request_filename) { proxy_pass http://127.0.0.1:9501; } } }
proxy_set_header X-Real-IP $remote_addr; 获取真实IP地址
运行你的hellword
project 项目部署目录 ---------------------------------- ├─App 应用目录 │ └─HttpController 应用的控制器目录 │ └─Index.php 默认控制器文件 ----------------------------------
Index.php
<?php namespace App\HttpController; use EasySwoole\Http\AbstractInterface\Controller; class Index extends Controller { function index() { // TODO: Implement index() method. $this->response()->write('hello world'); } }
编辑根目录下的 composer.json 文件,注册应用的命名空间
{ "autoload": { "psr-4": { "App\\": "App/" } }, "require": { "easyswoole/easyswoole": "3.x-dev" } }
意思就是设置自动加载
最后执行composer dumpautoload 命令更新命名空间,可以开始编写业务逻辑
# 更新命名空间映射 composer dumpautoload # 启动框架 php easyswoole start 目录结构 project 项目部署目录 ├─App 应用目录(可以有多个) │ ├─HttpController 控制器目录 │ │ └─Index.php 默认控制器 │ └─Model 模型文件目录 ├─Log 日志文件目录 ├─Temp 临时文件目录 ├─vendor 第三方类库目录 ├─composer.json Composer架构 ├─composer.lock Composer锁定 ├─EasySwooleEvent.php 框架全局事件 ├─easyswoole 框架管理脚本 ├─easyswoole.install 框架安装锁定文件 ├─dev.php 开发配置文件 ├─produce.php 生产配置文件
生命周期,也就是流程
配置文件说明
<?php /** * Created by PhpStorm. * User: yf * Date: 2019-01-01 * Time: 20:06 */ return [ 'SERVER_NAME' => "EasySwoole",//服务名 'MAIN_SERVER' => [ 'LISTEN_ADDRESS' => '0.0.0.0',//监听地址 'PORT' => 9501,//监听端口 'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER 'SOCK_TYPE' => SWOOLE_TCP,//该配置项当为SERVER_TYPE值为TYPE_SERVER时有效 'RUN_MODEL' => SWOOLE_PROCESS,// 默认Server的运行模式 'SETTING' => [// Swoole Server的运行配置( 完整配置可见[Swoole文档](https://wiki.swoole.com/wiki/page/274.html) ) 'worker_num' => 8,//运行的 worker进程数量 'max_request' => 5000,// worker 完成该数量的请求后将退出,防止内存溢出 'task_worker_num' => 8,//运行的 task_worker 进程数量 'task_max_request' => 1000,// task_worker 完成该数量的请求后将退出,防止内存溢出 'reload_async' => true,//设置异步重启开关。设置为true时,将启用异步安全重启特性,Worker进程会等待异步事件完成后再退出。 'task_enable_coroutine' => true//开启后自动在onTask回调中创建协程 ] ], 'TEMP_DIR' => null,//临时文件存放的目录 'LOG_DIR' => null,//日志文件存放的目录 'CONSOLE' => [//console控制台组件配置 'ENABLE' => true,//是否开启 'LISTEN_ADDRESS' => '127.0.0.1',//监听地址 'PORT' => 9500,//监听端口 'USER' => 'root',//验权用户名 'PASSWORD' => '123456'//验权用户名 ], 'FAST_CACHE' => [//fastCache组件 'PROCESS_NUM' => 0,//进程数,大于0才开启 'BACKLOG' => 256,//数据队列缓冲区大小 ], 'DISPLAY_ERROR' => true,//是否开启错误显示 ];
配置操作类
EasySwoole\Config 类
toArray 方法获取全部配置,load 方法重载全部配置
如果设置了修改,需要更新配置的意思
<?php $instance = \EasySwoole\EasySwoole\Config::getInstance(); // 获取配置 按层级用点号分隔 $instance->getConf('MAIN_SERVER.SETTING.task_worker_num'); // 设置配置 按层级用点号分隔 $instance->setConf('DATABASE.host', 'localhost'); // 获取全部配置 $conf = $instance->getConf(); // 用一个数组覆盖当前配置项 $conf['DATABASE'] = [ 'host' => '127.0.0.1', 'port' => 13306 ]; $instance->load($conf);
添加用户配置项
'MYSQL' => [ 'host' => '192.168.75.1', 'port' => '3306', 'user' => 'root', 'timeout' => '5', 'charset' => 'utf8mb4', 'password' => 'root', 'database' => 'cry', 'POOL_MAX_NUM' => '20', 'POOL_TIME_OUT' => '0.1', ], /*################ REDIS CONFIG ##################*/ 'REDIS' => [ 'host' => '127.0.0.1', 'port' => '6379', 'auth' => '', 'POOL_MAX_NUM' => '20', 'POOL_MIN_NUM' => '5', 'POOL_TIME_OUT' => '0.1', ],
生产与开发配置分离
默认为开发模式,加载 dev.php
生成
php easyswoole start produce
DI注入配置
也就是依赖注入
<?php Di::getInstance()->set(SysConst::ERROR_HANDLER,function (){});//配置错误处理回调 Di::getInstance()->set(SysConst::SHUTDOWN_FUNCTION,function (){});//配置脚本结束回调 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_NAMESPACE,'App\\HttpController\\');//配置控制器命名空间 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH,5);//配置http控制器最大解析层级 Di::getInstance()->set(SysConst::HTTP_EXCEPTION_HANDLER,function (){});//配置http控制器异常回调 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_POOL_MAX_NUM,15);//http控制器对象池最大数量
动态配置
每次开始了,是上一次的进程,比如你打开了旧版,现在更新了新版,但是旧版还是开着,没有重启动,也就是一直旧版,现在有个动态配置,表示可以平滑的修改
<?php Config::getInstance()->setDynamicConf('test_config_value', 0);//配置一个动态配置项 $test_config_value_1 = Config::getInstance()->getDynamicConf('test_config_value');//获取一个配置 Config::getInstance()->delDynamicConf('test_config_value');//删除一个配置
服务管理脚本
php easyswoole install 安装easySwoole start 启动easySwoole stop 停止easySwoole(守护模式下使用) reload 重启easySwoole(守护模式下使用) help 查看命令的帮助信息 easyswoole help -start
守护模式启动
php easyswoole start d
线上
php easyswoole start produce
停止
php easyswoole stop
重启服务
php easyswoole reload 只重启task进程 php easyswoole reload all 重启task + worker进程
文件热加载
由于 swoole 常驻内存的特性,修改文件后需要重启worker进程才能将被修改的文件重新载入内存中
解决:Process的方式实现文件变动自动进行服务重载
新建文件 App/Process/HotReload.php 并添加如下内容,也可以放在其他位置,请对应命名空间
<?php /** * Created by PhpStorm. * User: evalor * Date: 2018-11-26 * Time: 23:18 */ namespace App\Process; use EasySwoole\Component\Process\AbstractProcess; use EasySwoole\EasySwoole\ServerManager; use EasySwoole\Utility\File; use Swoole\Process; use Swoole\Table; use Swoole\Timer; /** * 暴力热重载 * Class HotReload * @package App\Process */ class HotReload extends AbstractProcess { /** @var \swoole_table $table */ protected $table; protected $isReady = false; protected $monitorDir; // 需要监控的目录 protected $monitorExt; // 需要监控的后缀 /** * 启动定时器进行循环扫描 */ public function run($arg) { // 此处指定需要监视的目录 建议只监视App目录下的文件变更 $this->monitorDir = !empty($arg['monitorDir']) ? $arg['monitorDir'] : EASYSWOOLE_ROOT . '/App'; // 指定需要监控的扩展名 不属于指定类型的的文件 无视变更 不重启 $this->monitorExt = !empty($arg['monitorExt']) && is_array($arg['monitorExt']) ? $arg['monitorExt'] : ['php']; if (extension_loaded('inotify') && empty($arg['disableInotify'])) { // 扩展可用 优先使用扩展进行处理 $this->registerInotifyEvent(); echo "server hot reload start : use inotify\n"; } else { // 扩展不可用时 进行暴力扫描 $this->table = new Table(512); $this->table->column('mtime', Table::TYPE_INT, 4); $this->table->create(); $this->runComparison(); Timer::tick(1000, function () { $this->runComparison(); }); echo "server hot reload start : use timer tick comparison\n"; } } /** * 扫描文件变更 */ private function runComparison() { $startTime = microtime(true); $doReload = false; $dirIterator = new \RecursiveDirectoryIterator($this->monitorDir); $iterator = new \RecursiveIteratorIterator($dirIterator); $inodeList = array(); // 迭代目录全部文件进行检查 foreach ($iterator as $file) { /** @var \SplFileInfo $file */ $ext = $file->getExtension(); if (!in_array($ext, $this->monitorExt)) { continue; // 只检查指定类型 } else { // 由于修改文件名称 并不需要重新载入 可以基于inode进行监控 $inode = $file->getInode(); $mtime = $file->getMTime(); array_push($inodeList, $inode); if (!$this->table->exist($inode)) { // 新建文件或修改文件 变更了inode $this->table->set($inode, ['mtime' => $mtime]); $doReload = true; } else { // 修改文件 但未发生inode变更 $oldTime = $this->table->get($inode)['mtime']; if ($oldTime != $mtime) { $this->table->set($inode, ['mtime' => $mtime]); $doReload = true; } } } } foreach ($this->table as $inode => $value) { // 迭代table寻找需要删除的inode if (!in_array(intval($inode), $inodeList)) { $this->table->del($inode); $doReload = true; } } if ($doReload) { $count = $this->table->count(); $time = date('Y-m-d H:i:s'); $usage = round(microtime(true) - $startTime, 3); if (!$this->isReady == false) { // 监测到需要进行热重启 echo "severReload at {$time} use : {$usage} s total: {$count} files\n"; ServerManager::getInstance()->getSwooleServer()->reload(); } else { // 首次扫描不需要进行重启操作 echo "hot reload ready at {$time} use : {$usage} s total: {$count} files\n"; $this->isReady = true; } } } /** * 注册Inotify监听事件 */ private function registerInotifyEvent() { // 因为进程独立 且当前是自定义进程 全局变量只有该进程使用 // 在确定不会造成污染的情况下 也可以合理使用全局变量 global $lastReloadTime; global $inotifyResource; $lastReloadTime = 0; $files = File::scanDirectory(EASYSWOOLE_ROOT . '/App'); $files = array_merge($files['files'], $files['dirs']); $inotifyResource = inotify_init(); // 为当前所有的目录和文件添加事件监听 foreach ($files as $item) { inotify_add_watch($inotifyResource, $item, IN_CREATE | IN_DELETE | IN_MODIFY); } // 加入事件循环 swoole_event_add($inotifyResource, function () { global $lastReloadTime; global $inotifyResource; $events = inotify_read($inotifyResource); if ($lastReloadTime < time() && !empty($events)) { // 限制1s内不能进行重复reload $lastReloadTime = time(); ServerManager::getInstance()->getSwooleServer()->reload(); } }); } public function onShutDown() { // TODO: Implement onShutDown() method. } public function onReceive(string $str) { // TODO: Implement onReceive() method. } }
添加好后在全局的 EasySwooleEvent.php 中,注册该自定义进程
public static function mainServerCreate(EventRegister $register) { $swooleServer = ServerManager::getInstance()->getSwooleServer(); $swooleServer->addProcess((new HotReload('HotReload', ['disableInotify' => false]))->getProcess()); }
The above is the detailed content of Getting Started with EasySwoole Basics. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
