前言
业务场景:监控当前业务当出现并发情况时技术人员可以可以及时处理
使用技术栈: laravel+redis
半小时触发一次报警信息实现思路
1、xshell脚本
具体参数就不详细解释了,想要详细了解可以自行百度
curl -H "Content-Type:application/json" -H "connection:Keep-Alive" -s -X Post "预警接口"
2、laravel 任务调度
任务调度 | 综合话题 |《Laravel 5.6 中文文档 5.6》| Laravel China 社区 (learnku.com)
定时任务
现在Kernel 文件书写定时任务,定时任务名称需要上下一致
预警逻辑
在Commands 中书写该逻辑
当预警人数占当前时段总人数35% 时触发钉钉预警 该逻辑 根据业务自行书写
钉钉预警实现思路
1、获取token和secret
保存后会获得 webhookurl 与 secret
2、书写逻辑
<?php
namespace App\Extend\Qy\Ding\Qy;use Illuminate\Support\Facades\Log;/*** 钉钉发送消息*/
class DingListen
{//token
// public $listenToken = "d3dfd656c95f7d319090bb9a51423cd0a948e7b26ce71a6042c4313469e654f4"; //线上机器人tokenpublic $listenToken = "f1112c52be54005e3f2c58f5bf610555f0fc655fb1cafb005e6242f66ca918a3"; //测试机器人token//secret
// public $listenSecret = "SEC8c7d7a0cf2ecd150790004648be4a05ee39ab11cfecca4b96ceca23c8ab56260"; //线上机器人secretpublic $listenSecret = "SEC800bc05ded1461939ab7b46d0efa2a97cdc8c379ec29e89600a0753eef486527"; // 测试机器人secret/*** 请求地址* @var string $url* */public $url = 'https://oapi.dingtalk.com/robot/send';/*** 请求地址* @var string $timestamp 13位毫秒级* */public $timestamp;/*** 加签* @var string $sign* */public $sign;/*** webhookurl* @var string $webhookurl* */public $webhookurl;public function __construct(){//时间戳$this->timestamp = time() * 1000;//加签$str = $this->timestamp."\n". $this->listenSecret;$sign = hash_hmac("sha256",$str ,$this->listenSecret,true);$sign = base64_encode($sign);$this->sign = $sign ;//返回链接$webhookurl = $this->url.'?access_token='.$this->listenToken. '×tamp='.$this->timestamp.'&sign='.urlencode($this->sign);$this->webhookurl = $webhookurl;}//发送text文本信息public function text($content = '', $mobiles = [], $userIds = [], $isAtAll = true){$sendContent = ["msgtype" => "text","text" => ["content" => $content],"at" => ["atMobiles" => $mobiles,"atUserIds" => $userIds, //不是钉钉管理员无法获取userIds"isAtAll" => $isAtAll]];$res = $this->_curl_post_json($this->webhookurl, $sendContent);return $res;}//发送json数据public function _curl_post_json($url, $data = array()){$curl = curl_init($url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, 320));curl_setopt($curl, CURLOPT_HEADER, 0);curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$res = curl_exec($curl);curl_close($curl);return $res;}
}
也可以将上述配置文件封装到Cofig 配置文件 自行调用 实例只发送了text 文本消息 具体案例信息查看官方文档进行配置 本文章仅限提供思路