首页 > 作文

thinkphp5 + redis 实现接口高并发限流

更新时间:2023-04-08 23:13:59 阅读: 评论:0

基于redis实现接口限流,主要使用Redis的一个模块:Redis-Cell

redis-cell如何使用,请你参考上一篇文章,传送门:https://blog.csdn.net/liuxl57805678/article/details/108824411

话不多述直接上代码:

先看下我项目目录:

LeakyBucket实现类

<?phpnamespace app\common\rver;class LeakyBucket{    protected $key = null;        protected $max_burst = null;        protected $tokens = null;        protected $conds = null;        protected $apply = 1;        protected $redis_connect;        /**     * LeakyBucket construct     * @param $key string     * @param $max_burst int 初始桶数量     * @param $tokens int 速率     * @param $conds int 时间     * @param int $apply 每次漏水数量     */    public function __construct($key, $max_burst, $tokens, $conds, $apply = 1)    {        $this->initRedis();        $this->key = $key;        $this->max_burst = $max_burst;        $this->tokens = $tokens;        $this->conds =公司宣言 $conds;        $this->apply = $apply;    }        /**     * 是否放行     * @return int 0 or 1   0:放行  1:拒绝     */    public function isPass()    {        $rs = $this->redis_connect->rawCommand('CL.THROTTLE', $this->key, $this->max_burst, $this->tokens, $this->conds, $this->apply);        return $rs[0];    }        //初始化redis    public function initRedis($conf = [])    {        if (empty($conf)) {            if (!empty(config('redis.'))) {                $conf = config('redis.');            }        }                $this->redis_connect = $this->init($conf);    }        protected function init($redisCfg)    {        if (extension_loaded('redis')) {            if (empty($redisCfg)) {                return '请配置redis信息!';            }            return $this->tupServer($redisCfg);        } el {            exit('缺少redis扩展!');        }    }        public function tupServer($config)    {        $this->redis_connect = new \Redis();        if ($config['socket_type'] === 'unix') {            $success = $this->redis_connect->connect($config['socket']);        } el {            $success = $this->redis_connect->connect($config['host'], $config['port'], $config['timeout']);        }        if (!$success) {            return fal;        } el {            if (ist($config['password'])) {                $this->redis_connect->auth($config['password']);            }                        if (ist($config['db']) && !empty($config['db'])) {                $this->redis_connect->lect(intval($config['db']));            }        }                return $this->redis_connect;    }    }

中间件

<?phpnamespace app\http\middleware;u app\common\rver\LeakyBucket;u think\Request;class CheckParam{    public function handle(Request $request, \Closure $next)    {        $ip = $芳凯尔request->ip(0, 1);        $leakyBucketConfig = config('leaky_bucket.');        $rver = new LeakyBucket($ip, $leakyBucketConfig['max_burst'], $leakyBucketConfig['tokens'], $leakyBucketConfig['conds']);        if ($rver->isPass()){            $res['code'] = 1001;            $res['msg'] = '操作频繁,请稍后再试';            return json($res);        }        return $next($request);        }    }}

中间件配置 config 目录下找到:middleware.php

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 /d/file/titlepic/www.thinkphp.cn All rights rerved.// +----------------------------------------------------------------------// | Licend ( /d/file/titlepic/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------// +----------------------------------------------------------------------// | 中间件配置// +----------------------------------------------------------------------return [    // 默认中间件命名空间    'che抗战20年ck'=>app\http\middleware\CheckParam::class];

调用中间件:(后面所有的控制器集成Common即可)其他调用中间件方式,请参考官方手册。

<?phpnamespace app\index\controller;u think\Controller;class Common extends Controller{    protected $middleware = ['check'];}

相关配置文件:什么是优质课配置文件放在config目录下

leaky_bucket.php

<?php// +------------------------------------------------------------小学生300字日记----------// | LeakyBucket设置// +----------------------------------------------------------------------return [    'max_burst' => 60,  //初始化桶容量    'tokens' => 20,     //漏斗的速率    'conds' => 60,    //时间];

redis.php

<?php// +----------------------------------------------------------------------// | Redis设置// +----------------------------------------------------------------------return [    'socket_type' => 'tcp',    'host' => '127.0.0.1',    'port' => '6379',    'timeout' => 10,    'password' => '',    'db' => 0];

喜欢的朋友记得点赞收藏哦。

本文地址:https://blog.csdn.net/liuxl57805678/article/details/108849044

本文发布于:2023-04-08 23:13:55,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/4a209e490a40b415d9583693ee4479ab.html

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

本文word下载地址:thinkphp5 + redis 实现接口高并发限流.doc

本文 PDF 下载地址:thinkphp5 + redis 实现接口高并发限流.pdf

下一篇:返回列表
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图