在实际的开发中,因为缺乏对下单并发场景的认识,后端逻辑没有对重复请求进行过滤,就会出现重复订单(严重的业务漏洞)、产生经济损失和退单的售后成本。所以我们十分有必要进行对重复订单进行处理。

常见的方法有以下几种:

【锁-机制】
在数据库中建立锁表

CREATE TABLE `TradeLock` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL COMMENT '锁类型',
`lockId` int(11) NOT NULL DEFAULT '0' COMMENT '业务ID',
`status` int(11) NOT NULL DEFAULT '0' COMMENT '锁状态',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='Trade锁机制';

每次request进来则往表里面插入数据:

成功--获得锁--继续操作
失败-->返回错误

在应对高并发场景,频繁请求数据不是明智的方法,这里我们就想到用redis做队列控制。
原理:入队---扣库存--执行(在原队列有逻辑没处理完成即返回库存不足!)

【计数器】

每次请求+1 利用cache缓存、设定失效时间30s(具有根据业务场景)。假如里面有请求则返回不要重复下单。以下是利用tp5里面的redis的cache驱动做计数器。

use thinkCache;

#缓存计数器 解决并发重复订单问题
$request =Cache('request_'.$user_id);
$request =$request+1;
Cache('request_'.$user_id,$request,30);
if($request>1){
    $response['code'] =0;
    $response['msg'] ='请勿重复操作';
    $response['data']='';
    return json($response);
}

引用
【防重复请求处理方案】
https://www.jianshu.com/p/a8655bdd73cd?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.特定用户下通过crontab -e命令编辑crontab任务,比如www
命令应该为:

crontab -e -u www

进入定时任务编辑界面:
701.png

英文键盘, 按【i】进入编辑模式
750.png

写入定时任务规则,比如我这里是给定时任务插件执行脚本

          • /usr/bin/php /home/wwwroot/www.kinmor.com/public/index.php /addons/crontab/autotask/index > /dev/null 2>&1 &

按【ESC】退出编辑模式
907.png
英文键盘,按【:wq】 保存退出

2.常用命令

启动服务

/bin/systemctl restart crond.service 

重新载入配置

/bin/systemctl reload  crond.service  

查看crontab服务状态

/bin/systemctl status  crond.service 

3.crontab相关命令

语法:crontab -u <用户名称> 或 crontab -u <用户名称>

参数:
-e  编辑该用户的计时器设置。
-l  列出该用户的计时器设置。
-r  删除该用户的计时器设置。
-u<用户名称>  指定要设定计时器的用户名称。

命令  svn up

Conflict discovered in 'test.txt'.
Select: (p) postpone, (df) diff-full, (e) edit,
        (mc) mine-conflict, (tc) theirs-conflict,
        (s) show all options:
svn detects that theres a conflict here and require you to take some kind of action.

If you type 's' here you will get a list of the commands and meaning
如果你输入s选项,则会列出所有svn解决冲突的选项,如下所示:

(e) edit - change merged file in an editor #直接进入编辑
(df) diff-full - show all changes made to merged file #显示更改至目标文件的所有变化
(r) resolved - accept merged version of file

(dc) display-conflict - show all conflicts (ignoring merged version) #显示所有冲突
(mc) mine-conflict - accept my version for all conflicts (same) #冲突以本地为准
(tc) theirs-conflict - accept their version for all conflicts (same) #冲突以服务器为准

(mf) mine-full - accept my version of entire file (even non-conflicts)#完全以本地为准
(tf) theirs-full - accept their version of entire file (same) #完全以服务器为准

(p) postpone - mark the conflict to be resolved later #标记冲突,稍后解决
(l) launch - launch external tool to resolve conflict
(s) show all - show this list #显示所有

今天登陆linux服务器 发现var/log下的日志很大 其中包括btmp、message都很大!猜测是被人密码字典猜测后台root了。为规避风险对危险IP进行屏蔽

利用命令进行筛选IP

lastb | awk '{ print $3}' | sort | uniq -c | sort -n

999.png

扩展

将筛选结果导出来 放在home目录下以ips命名

  lastb | awk '{ print $3}' | sort | uniq -c | sort -n > /home/ips.txt

2.删除日志

rm -rf /var/log/btmp

touch /var/log/btmp 

版本:thinkphp5.1

thinkphplibrarythinklogdriverFile.php 148行

问题:最近项目发布到linux系统centos上,Nginx运行的用户是www 而日志 write则变成了root用户 这样www无法读取日志就造成程序错误。linux有严格的权限管理!先根据网上的攻略进行改造!记录如下:

/**
 * 日志写入
 * @access protected
 * @param  array     $message 日志信息
 * @param  string    $destination 日志文件
 * @param  bool      $apart 是否独立文件写入
 * @param  bool      $append 是否追加请求信息
 * @return bool
 */
protected function write($message, $destination, $apart = false, $append = false)
{
    // 检测日志文件大小,超过配置大小则备份日志文件重新生成
    $this->checkLogSize($destination);

    // 日志信息封装
    $info['timestamp'] = date($this->config['time_format']);

    foreach ($message as $type => $msg) {
        $info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
    }

    if (PHP_SAPI == 'cli') {
        $message = $this->parseCliLog($info);
    } else {
        // 添加调试日志
        $this->getDebugLog($info, $append, $apart);

        $message = $this->parseLog($info);
    }
    //解决root产生的日志 www无法访问
    if (!is_file($destination)) {
    $first = true;
    }
    $ret =error_log($message, 3, $destination);
    try {
        if (isset($first) && is_file($destination)) {
            chmod($destination, 0777);
            unset($first);
        }
    } catch (\Exception $e){
    
    }
    return $ret;
}