获取PHP7源码:www.php.net
tar -xzvf ... # 解压命令
./configure --prefix=/home/study/php # 安装至某个路径,提前安装gcc等
make # 编译
make install # 安装
源码执行文件放在:bin
目录下
php -m # 查看 PHP 安装的扩展
alias
命令=命令的绝对路径
vim /.bash_profile
alias php=/home/work/soft/php/bin/php # 添加
source /.bash_profile # 注意
source FileName
作用:在当前bash
环境下读取并执行FileName
中的命令。 用于重新执行刚修改的初始化文档,如 .bash_profile
和 .profile
等等
注:该命令通常用命令“.
”来替代
如:source /etc/profile
与 . /etc/profile
是等效的
php -i | grep php.ini # 查找PHP的配置文件
获取swoole源码:https://gitee.com/swoole/swoole.git
phpize
是用来扩展php
模块的,通过phpize
可以建立php
的外挂模块,解决没有configure
问题
/usr/local/php/bin/phpize # 在需要执行的目录执行这行代码即可
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
最后可以在PHP
的扩展目录中看见swoole.so
扩展文件
在php.ini
文件中添加:extension=swoole.so
查看是否添加成功:php -m
在swoole/examples/server
下执行php echo.php
查看是否执行端口:9501
netstat -anp|grep 9501
非常重要
)Swoole官网文档:创建TCP服务器 | 创建UDP服务器
//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
//swoole_server->set函数用于设置swoole_server运行时的各项参数
$serv->set([
'worker_num' => 6 , // worker进程数,cpu 1-4倍
'max_request' => 10000,
]);
/**
* 监听连接进入事件
* $fd 客户端连接的唯一标示
* $reactor_id 线程id
*/
$serv->on('connect', function ($serv, $fd, $reactor_id) {
echo "Client: {$reactor_id} - {$fd}-Connect.\n";
});
/**
* 监听数据接收事件
* $reactor_id = $from_id
*/
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
$serv->send($fd, "Server: {$reactor_id} - {$fd}".$data);
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
测试tcp
服务器方法:
netstat -anp | grep 9501
telnet
方式登录远程主机:telnet 127.0.0.1 9501
tcp
客户端脚本 查看当前worker
进程数:ps -aft | grep tcp_server.php
Tips:为了保证程序执行的完整性,当修改tcp
服务器脚本后最好设置平滑重启worker
进程
平滑重启worker进程
// 连接 swoole tcp 服务
$client = new swoole_client(SWOOLE_SOCK_TCP);
if(!$client->connect("127.0.0.1", 9501)) {
echo "连接失败";
exit;
}
// php cli常量
fwrite(STDOUT, "请输入消息:");
$msg = trim(fgets(STDIN));
// 发送消息给 tcp server服务器
$client->send($msg);
// 接受来自server 的数据
$result = $client->recv();
echo $result;
常用
)$http = new swoole_http_server("0.0.0.0", 8811);
//添加测试一:获取参数并打印出来
//$http->on('request', function ($request, $response) {
// $response->cookie("singwa",'xsssss', time() + 1800);
// $response->end('sss'.json_encode($request->get));
//});
/**
* https://wiki.swoole.com/wiki/page/783.html
* 配置静态文件根目录,与enable_static_handler配合使用。
* 设置document_root并设置enable_static_handler为true后,
* 底层收到Http请求会先判断document_root路径下是否存在此文件,
* 如果存在会直接发送文件内容给客户端,不再触发onRequest回调。
*/
$http->set(
[
'enable_static_handler' => true,
'document_root' => "/home/work/hdtocs/swoole_mooc/data",
]
);
$http->on('request', function($request, $response) {
//print_r($request->get);
$content = [
'date:' => date("Ymd H:i:s"),
'get:' => $request->get,
'post:' => $request->post,
'header:' => $request->header,
];
swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
// todo
}, FILE_APPEND);
$response->cookie("singwa", "xsssss", time() + 1800);
$response->end("sss". json_encode($request->get));
});
$http->start();
WebSocket
协议是基于TCP
的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex
)通信--允许服务器主动发送信息给客户端
为什么需要WebSocket
HTTP
的通信只能由客户端发起WebSocket特点
TCP
协议之上ws
wss
1. 面向过程:
procedure_ws_server.php
$server = new swoole_websocket_server("0.0.0.0", 9912);
//配置静态文件根目录,可选
$server->set(
[
'enable_static_handler' => true,
'document_root' => "/home/wwwroot/www.lingyuan88.com/public/swoole/data",
]
);
//监听websocket连接打开事件
$server->on('open', 'onOpen');
function onOpen($server, $request) {
print_r($request->fd);
}
// 监听ws消息事件
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "singwa-push-secesss");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
2. WebSocket服务优化,基础类库面向对象:
object_ws_server.php
class Ws {
CONST HOST = "0.0.0.0";
CONST PORT = 9912;
public $ws = null;
public function __construct() {
$this->ws = new swoole_websocket_server(self::HOST, self::PORT);
//配置静态文件根目录,可选
$this->ws->set(
[
'enable_static_handler' => true,
'document_root' => "/home/wwwroot/www.lingyuan88.com/public/swoole/data",
]
);
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 监听ws连接事件
* @param $ws
* @param $request
*/
public function onOpen($ws, $request) {
print_r($request->fd);
}
/**
* 监听ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}\n";
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}
/**
* close
* @param $ws
* @param $fd
*/
public function onClose($ws, $fd) {
echo "clientid:{$fd}\n";
}
}
$obj = new Ws();
ws_client.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title>
head>
<body>
<h1>singwa-swoole-ws测试h1>
<script>
var wsUrl = "ws://120.77.206.215:9912";
var websocket = new WebSocket(wsUrl);
//实例对象的onopen属性
websocket.onopen = function(evt) {
websocket.send("hello-sinwa");
console.log("conected-swoole-success");
}
// 实例化 onmessage
websocket.onmessage = function(evt) {
console.log("ws-server-return-data:" + evt.data);
}
//onclose
websocket.onclose = function(evt) {
console.log("close");
}
//onerror
websocket.onerror = function(evt, e) {
console.log("error:" + evt.data);
}
script>
body>
html>
1. 通过WebSocket
静态文件目录测试
2. 通过HTTP
服务测试
使用场景
注意:
继续往下执行
,不会等待任务执行完后再继续向下执行class Ws {
CONST HOST = "0.0.0.0";
CONST PORT = 9912;
public $ws = null;
public function __construct() {
$this->ws = new swoole_websocket_server(self::HOST, self::PORT);
$this->ws->set(
[
'worker_num' => 2,
'task_worker_num' => 2,
]
);
//注册Server的事件回调函数
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 监听ws连接事件
* @param $ws
* @param $request
*/
public function onOpen($ws, $request) {
var_dump($request->fd);
}
/**
* 监听ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}\n";
// todo 10s
$data = [
'task' => 1,
'fd' => $frame->fd,
];
//投递异步任务
//注意:程序会继续往下执行,不会等待任务执行完后再继续向下执行
$ws->task($data);
//客户端会马上收到以下信息
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}
/**
* @param $serv
* @param $taskId
* @param $workerId
* @param $data
* @return string
*/
public function onTask($serv, $taskId, $workerId, $data) {
print_r($data);
// 耗时场景 10s
sleep(10);
return "on task finish"; // 告诉worker,并返回给onFinish的$data
}
/**
* @param $serv
* @param $taskId
* @param $data
*/
public function onFinish($serv, $taskId, $data) {
echo "taskId:{$taskId}\n";
echo "finish-data-sucess:{$data}\n";
}
/**
* close
* @param $ws
* @param $fd
*/
public function onClose($ws, $fd) {
echo "clientid:{$fd}\n";
}
}
$obj = new Ws();
务必理解
) 关注的是消息通知
机制;
同步:调用发出之后不会立即返回
,但一旦返回,则返回最终结果;异步:调用发出之后,被调用方
立即返回消息
,但返回的并非最终结果。被调用者通过状态、通知机制等来通知调用者,或通过回调函数来处理结果;
关注的是调用者等待被调用者返回调用结果时的状态。
阻塞:调用结果返回之前,调用者会被挂起
,调用者只有在得到返回结果之后才能继续。非阻塞:调用者在结果返回之前,不会被挂起;
blocking IO:阻塞式IO
nonblocking IO:非阻塞IO
multiplexing IO:多路复用IO
signal driven IO:事件驱动式IO
asynchronous IO:异步IO
真正执行IO
过程的阶段是内核内存数据
拷贝到进程内存
中
异步
高精度定时器,粒度为毫秒级
//每隔2000ms触发一次
swoole_timer_tick(2000, function ($timer_id) {
echo "tick-2000ms\n";
});
//3000ms后执行此函数
swoole_timer_after(3000, function () {
echo "after 3000ms.\n";
});
Swoole官网文档:异步文件系统IO
/**
* 读取文件
* __DIR__
* 文件不存在会返回false
* 成功打开文件立即返回true
* 数据读取完毕后会回调指定的callback函数。
*/
//函数风格
$result = swoole_async_readfile(__DIR__."/1.txt", function($filename, $fileContent) {
echo "filename:".$filename.PHP_EOL; // \n \r\n
echo "content:".$fileContent.PHP_EOL;
});
//命名空间风格
$result = Swoole\Async::readfile(__DIR__."/1.txt", function($filename, $fileContent) {
echo "filename:".$filename.PHP_EOL; // \n \r\n
echo "content:".$fileContent.PHP_EOL;
});
var_dump($result);
echo "start".PHP_EOL;
$http->on('request', function($request, $response) {
$content = [
'date:' => date("Ymd H:i:s"),
'get:' => $request->get,
'post:' => $request->post,
'header:' => $request->header,
];
swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
// todo
}, FILE_APPEND);
$response->end("response:". json_encode($request->get));
});
class AsyncMySql {
/**
* @var string
*/
public $dbSource = "";
/**
* mysql的配置
* @var array
*/
public $dbConfig = [];
public function __construct() {
//new swoole_mysql;
$this->dbSource = new Swoole\Mysql;
$this->dbConfig = [
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => 'test',
'database' => 'test',
'charset' => 'utf8',
];
}
public function update() {}
public function add() {}
/**
* mysql 执行逻辑
* @param $id
* @param $username
* @return bool
*/
public function execute($id, $username) {
$this->dbSource->connect($this->dbConfig, function($db, $result) use($id, $username) {
echo "mysql-connect".PHP_EOL;
if($result === false) {
var_dump($db->connect_error);
// todo
}
$sql = "select * from cmf_user where id=1";
//$sql = "update test set `username` = '".$username."' where id=".$id;
// insert into
// query (add select update delete)
$db->query($sql, function($db, $result){
// select => result返回的是 查询的结果内容
if($result === false) {
// todo
var_dump($db->error);
}elseif($result === true) {// add update delete
// todo
var_dump($db->affected_rows);
}else {
print_r($result);
}
$db->close();
});
});
return true;
}
}
$obj = new AsyncMySql();
$flag = $obj->execute(1, 'singwa-111112');
var_dump($flag).PHP_EOL;
echo "start".PHP_EOL;
swoole使用redis的前置条件
redis
服务hiredis
库swoole
需要加入 -enable-async-redis
编译安装hiredis
使用Redis
客户端,需要安装hiredis
库,下载hiredis
源码后,执行
make -j
sudo make install
sudo ldconfig
hiredis下载地址
启用异步Redis客户端
编译swoole
时,在configure
指令中加入--enable-async-redis
[root@izwz93ee3z8wdxsujiec2oz swoole]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-async-redis
make clean
make -j
sudo make install
查看PHP
的swoole
扩展:php -m
查看hiredis
是否编译安装成功:php --ri swoole
$redisClient = new swoole_redis;// Swoole\Redis
$redisClient->connect('127.0.0.1', 6379, function(swoole_redis $redisClient, $result) {
echo "connect".PHP_EOL;
var_dump($result);
// 同步 redis (new Redis())->set('key',2);
/*$redisClient->set('singwa_1', time(), function(swoole_redis $redisClient, $result) {
var_dump($result);
});*/
/*$redisClient->get('singwa_1', function(swoole_redis $redisClient, $result) {
var_dump($result);
$redisClient->close();
});*/
$redisClient->keys('*gw*', function(swoole_redis $redisClient, $result) {
var_dump($result);
$redisClient->close();
});
});
echo "start".PHP_EOL;