Openresty 限流

490次阅读
没有评论

共计 2742 个字符,预计需要花费 7 分钟才能阅读完成。

Openresty 限流

最近博主在看高可用系统的三板斧相关知识点:熔断,限流,降级。此处就描述下博主是如何用openresty做限流的。虽然项目中用的是阿里的sentinel。。。。

基于IP限制单位时间窗口的请求数

此处限制每个客户端ip,每分钟只能调用30次。(这30次在一分钟的窗口内随便分布,可能是第一秒就刷完30次,或是每两秒1次)

1.现在http块中定义一块内存空间用于存储限流信息

# http块内,server块外!
lua_shared_dict my_limit_count_store 100m;

# 此处另外定义一个负载均衡块
upstream greatingapp {
    server 192.168.44.145:8081;
    server 192.168.44.146:8081;
}

2.配置基于ip的接口限制

将客户端ip作为key,从而实现基于客户ip的接口限流

local key = ngx.var.binary_remote_addr

最终location如下

    location /greating/ {
        content_by_lua_block{
            ngx.header['Content-Type'] = 'application/json; charset=utf-8'
            local limit_count = require "resty.limit.count"
            local lim, err = limit_count.new("my_limit_count_store", 30, 60)
            json = require 'cjson'

            if not lim then
                ngx.log(ngx.ERR, "failed to instantiate a resty.limit.count object: ", err)
                return ngx.exit(500)
            end
            local key = ngx.var.binary_remote_addr
            local delay, err = lim:incoming(key, true)
            ngx.log(ngx.ERR,"delay: ",delay," err: ",err)
            if not delay then
                if err == "rejected" then
                    local obj = {
                        error_msg = "访问太频繁了..",
                        status = err
                    }
                    res_json_data = json.encode(obj)
                    ngx.say(res_json_data)
                    -- return ngx.exit(503)
                end
                ngx.log(ngx.ERR, "被限流啦...... ", err)
                return ngx.exit(500)
            end
            ngx.exec("@greatingapp")
        }
    }
    location @greatingapp {
        rewrite /greating/(.*)$ /$1 break;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://greatingapp;
    }

curl模拟测试下响应

[root@nginx-cluster conf.d]# for i in `seq 35`;do curl http://openresty.xadocker.cn/greating/ && echo  ;done
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
Greeting for springboot, Hi xadocker.....
{"error_msg":"Request so fast....","status":"rejected"}

{"error_msg":"Request so fast....","status":"rejected"}

{"error_msg":"Request so fast....","status":"rejected"}

{"error_msg":"Request so fast....","status":"rejected"}

{"error_msg":"Request so fast....","status":"rejected"}

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2021-05-15发表,共计2742字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)