swoolephp⽇志,Swoole+ThinkPHP5.0——⽇志与服务监控Swoole+ThinkPHP5.0——⽇志与服务监控
羡仙. · 2019 年 06 ⽉ 26 ⽇
⽇志
在server/websocket.php添加public function onRequest($request, $response)
{
...
$_FILES = [];
if (isset($request->files)) {
foreach ($request->files as $key => $val) {
$_FILES[$key] = $val;
}
}
// 写在设置全局变量后⾯
/*记录⽇志*/
$this->writeLog();
...
}
/*记录⽇志*/
public function writeLog()
{
$data = array_merge(['data' => date('Y-m-d H:i:s')], $_GET, $_POST, $_SERVER);
$logs = '';
foreach ($data as $key => $val) {
$logs .= $key . ':' . $val . ' ';
}
echo $logs;
}
结果展⽰
写⼊⽂件
在runtime/log⽬录下可以看到⽇志的格式是log/Ym/d.log.通过swoole按照thinkphp5.0风格来写logfile.编辑server/websocket.php的writeLog()./*记录⽇志*/
public function writeLog()
{
$data = array_merge(['data' => date('Y-m-d H:i:s')], $_GET, $_POST, $_SERVER);
$logs = '';
foreach ($data as $key => $val) {
$logs .= $key . ':' . $val . ' ';
}
/*
* swoole_async_writefile
* 异步⽂件写⽇志
* ⽇志⽂件路径 ⽇记内容 回调函数 追加的形式
* APP_Path就是我们的application⽬录
* tmd 没这个⽅法了。要想⽤要么使⽤协程⾃⼰替换。要么下载async-ext扩展github/swoole/ext-async
* */
go(function () use ($logs) {
/*判断⽬录存在不存在*/
$dir = APP_PATH . '../runtime/log/' . date('Ym') . '/';
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$this->exeWriteLog($dir . date('d') . '_success.log', $logs);
});
}
/*执⾏写⼊⽇志*/
public function exeWriteLog($filename, $content)
{
/*
* 如果⽂件不存在,则创建⽂件,相当于fopen()函数⾏为。
* 如果⽂件存在,默认将清空⽂件内的内容,可设置 flags 参数值为 FILE_APPEND。
* */
file_put_contents($filename, PHP_EOL . $content, FILE_APPEND);
}
多次请求的问题
thinkphp中请求⼀个页⾯他会⾛两次请求,tmd让⼈很头疼.之前做个视频⽹站也是因为⾛个图⽚的地址.因为没有然后报错~.他请求两次也是很费资源的!解决,修改server/websocket.php/*request 回调*/
public function onRequest($request, $response)
{
// 顶部添加,如果没有问题可以不⽤加这段~
if($request->server['request_uri'] == '/xxx'){
// 返回404 并结束。如果不⽤end() 会报500。这个请求就协程⾃⼰的吧
$response->status(404);
$response->end();
return ;
}
...swoole扩展
}
服务监控
Linux下查看端⼝
启动服务的时候都是到服务⽬录下执⾏php xxx.php. 如果我们不对服务做监控,他突然挂掉了,⽽我们并不知道.所以我们更好的管理服务以及定位到问题的所在.需要写个脚本来监控服务~# 启动服务
php server/websocket.php
# 查看进程
netstat -anp | grep 950
tcp 0 0 0.0.0.0:9503 0.0.0.0:* LISTEN 127358/php
tcp 0 0 0.0.0.0:9504 0.0.0.0:* LISTEN 127358/php
tcp 0 0 0.0.0.0:9505 0.0.0.0:* LISTEN 127358/php
# 这⾥因为我做了聊天室和图⽂直播,分别⽤了9503,9604.所以启动服务后有3个进程.
-- 简单的返回值
netstat -anp | grep 9503 | grep LISTEN | wc -l
# 服务启动时返回
1
# 服务为启动返回
编写脚本
需要时刻的去检测端⼝.可以⽤到linux的crontab去检,但是linux的crontab检测服务是每分钟,如需要每秒的去监听,应该换⽤swoole的毫秒级定时器.先写⼀个简单的脚本server/script/montitor/server.php.class Server
{
const POST = 9503;
public function port()
{
$shell = 'netstat -anp | grep ' . self::POST . '| grep LISTEN | wc -l'; $result = shell_exec($shell);
if($result != 1){
/*发送报警 短信或者邮件*/
echo date('Y-m-d H:i:s') . 'error' .PHP_EOL;
}else{
echo date('Y-m-d H:i:s') . 'success' .PHP_EOL;
}
}
}
\swoole_timer_tick(2000,function($timer_id){
(new Server())->port();
echo 'timer_start' . PHP_EOL;
});
启动脚本,关闭服务,启动服务测试.# 启动脚本监听
php server/script/montitor/server.php
# 返回
2019-06-25 13:55:02error
timer_start
2019-06-25 13:55:04success
timer_start
2019-06-25 13:55:06success
timer_start
2019-06-25 13:55:08success
timer_start
2019-06-25 13:55:10error
timer_start
2019-06-25 13:55:12error
timer_start
后台执⾏
通过php xxx.php去执⾏的,这样是不⾏的.应该让脚本在后台⽆间断的去执⾏吧.在linux中有个强⼤的后台执⾏命令nohup [程序名] & ,符号&是指后台运⾏,不加就是直接执⾏.# 查看php执⾏⽂件路径
find /usr/local/php/ -name php
# 返回php执⾏⽂件路径
/usr/local/php/bin/php
# 查看脚本所在路径
pwd
# 返回
/data/wwwroot/liveTelecast/server/script/montitor
cd /data/wwwroot/liveTelecast/server/script/montitor
# 创建⽇志⽂件
mkdir log
cd log
# 后台PHP执⾏脚本,输⼊到⽇志⽂件
nohup /usr/local/php/bin/php /data/wwwroot/liveTelecast/server/script/montitor/server.php >
/data/wwwroot/liveTelecast/server/script/montitor/ &
# 返回pid.如果想关闭,执⾏kill -9 返回的pid就好.
[1] 130473
[root@localhost montitor]# nohup: 忽略输⼊重定向错误到标准输出端
# 查看pid的进程
ps -ef | grep 130473
# 返回
root 130473 127391 0 10:24 pts/1 00:00:00 /usr/local/php/bin/php
/data/wwwroot/liveTelecast/server/script/montitor/server.php
root 130543 127391 0 10:24 pts/1 00:00:00 grep --color=auto 130473
# 查看⽇志⽂件
# 返回
2019-06-25 14:25:18success
timer_start
2019-06-25 14:25:20success
timer_start
2019-06-25 14:25:22error
timer_start

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。