当前位置:K88软件开发文章中心编程语言PHPSwoole → 文章内容

Swoole HttpServer实例

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:54:56

由 路飞 创建, 最后一次修改 2016-12-12 Swoole HttpServer介绍swoole-1.7.7增加了内置Http服务器的支持,通过几行代码即可写出一个异步非阻塞多进程的Http服务器。常见使用场景:因为swoole是在cli命令下执行的,在传统通过nginx+fastcgi模式下很多root的shell无法执行,而使用这个swoole服务器就很好的控制rsync,git,svn等。$http = new swoole_http_server("127.0.0.1", 9501);$http->on('request', function ($request, $response) { $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");});$http->start();swoole_http_server对Http协议的支持并不完整,建议仅作为应用服务器。并且在前端增加Nginx作为代理简单实例:$serv = new Swoole\Http\Server("127.0.0.1", 9502);$serv->on('Request', function($request, $response) { var_dump($request->get); var_dump($request->post); var_dump($request->cookie); var_dump($request->files); var_dump($request->header); var_dump($request->server); $response->cookie("User", "Swoole"); $response->header("X-Server", "Swoole"); $response->end("<h1>Hello Swoole!</h1>");});$serv->start();通过使用apache bench工具进行压力测试,在Inter Core-I5 4核 + 8G内存的普通PC机器上,swoole_http_server可以达到近11万QPS。远远超过php-fpm,golang自带http服务器,node.js自带http服务器。性能几乎接近与Nginx的静态文件处理。ab -c 200 -n 200000 -k http://127.0.0.1:9501使用Http2协议需要依赖nghttp2库,下载nghttp2后编译安装使用Http2协议必须开启openssl需要高版本openssl必须支持TLS1.2、ALPN、NPN./configure --enable-openssl --enable-http2设置http服务器的open_http2_protocol为true$serv->set([ 'ssl_cert_file' => $ssl_dir . '/ssl.crt', 'ssl_key_file' => $ssl_dir . '/ssl.key', 'open_http2_protocol' => true,]);nginx+swoole配置server { root /data/wwwroot/; server_name local.swoole.com; location / { if (!-e $request_filename) { proxy_pass http://127.0.0.1:9501; proxy_http_version 1.1; proxy_set_header Connection "keep-alive"; } }}异步Http/WebSocket客户端Swoole-1.8.0版本增加了对异步Http/WebSocket客户端的支持。底层是用纯C编写,拥有超高的性能。异步HTTP客户端目前仍在实验阶段,请谨慎使用启用Http客户端1.8.6版本之前,需要在编译swoole时增加--enable-async-httpclient来开启此功能。swoole_http_client不依赖任何第三方库支持Http-Chunk、Keep-Alive特性,暂不支持form-data格式Http协议版本为HTTP/1.1gzip压缩格式支持需要依赖zlib库构造方法function swoole_http_client->__construct(string $ip, int port, bool $ssl = false);$ip 目标服务器的IP地址,可使用swoole_async_dns_lookup查询域名对应的IP地址$port 目标服务器的端口,一般http为80,https为443$ssl 是否启用SSL/TLS隧道加密,如果目标服务器是https必须设置$ssl参数为true

Swoole HttpServer实例