概述
我们都知道http是一种协议,允许web服务器和浏览器通过互联网进行发送和接受数据。
想对http进行详细的了解,可以找下其他文章,这篇文章不多做介绍。
我们在网上能看到的界面,图片,动画,音频,视频等,都有依赖这个协议的。
在做web系统的时候,都使用过iis,apache,nginx吧,我们利用swoole也可以简单的实现一个web服务器。
主要使用了http的两个大对象:request请求对象,response响应对象。
请求,包括get,post,cookie,header等。
响应,包括状态,响应体,扩展,发送文件等。
不多说,先分享两个程序:
一,实现一个基础的demo:“你好,swoole。”
二,实现一个简单的路由控制
本地版本:
php 7.2.6
旋风4.3.1
代码
一,demo:“你好,swoole。”
示例效果:
备注:ip地址是我的虚拟机。
示例代码:
<?phpclass server{ private $serv; public function __construct() { $this->serv = new swoole_http_server(0.0.0.0, 9502); $this->serv->set([ 'worker_num' => 2, //开启2个worker进程 'max_request' => 4, //每个worker进程 max_request设置为4次 'daemonize' => false, //守护进程(true/false) ]); $this->serv->on('start', [$this, 'onstart']); $this->serv->on('workerstart', [$this, 'onworkstart']); $this->serv->on('managerstart', [$this, 'onmanagerstart']); $this->serv->on(request, [$this, 'onrequest']); $this->serv->start(); } public function onstart($serv) { echo #### onstart ####.php_eol; echo swoole .swoole_version . 服务已启动.php_eol; echo master_pid: {$serv->master_pid}.php_eol; echo manager_pid: {$serv->manager_pid}.php_eol; echo ########.php_eol.php_eol; } public function onmanagerstart($serv) { echo #### onmanagerstart ####.php_eol.php_eol; } public function onworkstart($serv, $worker_id) { echo #### onworkstart ####.php_eol.php_eol; } public function onrequest($request, $response) { $response->header(content-type, text/html; charset=utf-8); $html = <h1>你好 swoole.</h1>; $response->end($html); }}$server = new server();
二,路由控制
示例效果:
目录结构:
├─ swoole_http -- 代码根目录│ ├─ server.php│ ├─ controller│ ├── index.php│ ├── login.php
示例代码:
server.php
<?phpclass server{ private $serv; public function __construct() { $this->serv = new swoole_http_server(0.0.0.0, 9501); $this->serv->set([ 'worker_num' => 2, //开启2个worker进程 'max_request' => 4, //每个worker进程 max_request设置为4次 'document_root' => '', 'enable_static_handler' => true, 'daemonize' => false, //守护进程(true/false) ]); $this->serv->on('start', [$this, 'onstart']); $this->serv->on('workerstart', [$this, 'onworkstart']); $this->serv->on('managerstart', [$this, 'onmanagerstart']); $this->serv->on(request, [$this, 'onrequest']); $this->serv->start(); } public function onstart($serv) { echo #### onstart ####.php_eol; swoole_set_process_name('swoole_process_server_master'); echo swoole .swoole_version . 服务已启动.php_eol; echo master_pid: {$serv->master_pid}.php_eol; echo manager_pid: {$serv->manager_pid}.php_eol; echo ########.php_eol.php_eol; } public function onmanagerstart($serv) { echo #### onmanagerstart ####.php_eol.php_eol; swoole_set_process_name('swoole_process_server_manager'); } public function onworkstart($serv, $worker_id) { echo #### onworkstart ####.php_eol.php_eol; swoole_set_process_name('swoole_process_server_worker'); spl_autoload_register(function ($classname) { $classpath = __dir__ . /controller/ . $classname . .php; if (is_file($classpath)) { require {$classpath}; return; } }); } public function onrequest($request, $response) { $response->header(server, swooleserver); $response->header(content-type, text/html; charset=utf-8); $server = $request->server; $path_info = $server['path_info']; $request_uri = $server['request_uri']; if ($path_info == '/favicon.ico' || $request_uri == '/favicon.ico') { return $response->end(); } $controller = 'index'; $method = 'home'; if ($path_info != '/') { $path_info = explode('/',$path_info); if (!is_array($path_info)) { $response->status(404); $response->end('url不存在'); } if ($path_info[1] == 'favicon.ico') { return; } $count_path_info = count($path_info); if ($count_path_info > 4) { $response->status(404); $response->end('url不存在'); } $controller = (isset($path_info[1]) && !empty($path_info[1])) ? $path_info[1] : $controller; $method = (isset($path_info[2]) && !empty($path_info[2])) ? $path_info[2] : $method; } $result = class 不存在; if (class_exists($controller)) { $class = new $controller(); $result = method 不存在; if (method_exists($controller, $method)) { $result = $class->$method($request); } } $response->end($result); }}$server = new server();
index.php
<?phpclass index{ public function home($request) { $get = isset($request->get) ? $request->get : []; //@todo 业务代码 $result = <h1>你好,swoole。</h1>; $result.= get参数:.json_encode($get); return $result; }}
login.php
<?phpclass login{ public function index($request) { $post = isset($request->post) ? $request->post : []; //@todo 业务代码 return <h1>登录成功。</h1>; }}
小结一,swoole可以替代nginx吗?
暂时不能,通过swoole越来越强大,以后说不准。
官方建议swoole与nginx结合使用。
http \ server对http协议的支持并不完整,建议仅作为应用服务器。并且在前端增加nginx作为代理。
根据自己的nginx配置文件,可以自行调整。
例如:可以添加一个配置文件
enable-swoole-php.conf
location ~ [^/]\.php(/|$){ proxy_http_version 1.1; proxy_set_header connection keep-alive; proxy_set_header x-real-ip $remote_addr; proxy_pass http://127.0.0.1:9501;}
我们都习惯于将虚拟域名的配置文件放在vhost文件夹中。
例如,虚拟域名的配置文件为:local.swoole.com.conf,可以选择加载enable-php.conf,也可以选择加载enable-swoole-php.conf。
配置文件供参考:
server { listen 80; #listen [::]:80; server_name local.swoole.com ; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/project/swoole; #include rewrite/none.conf; #error_page 404 /404.html; #include enable-php.conf; include enable-swoole-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /.well-known { allow all; } location ~ /\. { deny all; } access_log /home/wwwlogs/local.swoole.com.log; }
当前,我们直接编辑server段的代码也是可以的。
二,修改了controller文件夹中的业务代码,每次都是重启服务才生效吗?
不是,每次重启服务可能会影响到正常用户使用的,正常处理的请求会被强制关闭。
在本地运行路由控制的代码时,试试这个命令:
ps aux | grep swoole_process_server_master | awk '{print $2}' | xargs kill -usr1
给master进程发送一个usr1的信号,当swoole server接收到该信号后,就会让所有worker在处理完当前的请求后,进行重启。
如果查看所有的进程,试试这个命令:
ps -ef | grep 'swoole_process_server'| grep -v 'grep'
扩展可以试着上传文件,做一个小的ftp服务器。
可以学习swoole开源框架:easyswoole,swoft,one。
可以将swoole整合到当前正在使用的php框架中。
更多相关精品文章敬请关注swoole框架栏目!
以上就是介绍 swoole http的应用的详细内容。