使用 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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

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

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