相关链接

多级缓存策略思维导图:https://kdocs.cn/l/sfV9xmfovVKv
nginx基于openresty验证jwt:https://segmentfault.com/a/1190000015677681
OpenResty不完全指南:https://zhuanlan.zhihu.com/p/42082302
基于OpenResty实现 JWT验证2:https://www.jianshu.com/p/66d5163b9e99

思维导图

image-20210124224639231

OpenResty验证Jwt思维图

整体架构

保证业务连贯性用uuid

uuid生成

为每个请求生成唯一的uuid码可以将网关层上的请求和应用层的请求关联起来,对排查问题,接口统计都非常有用.

创建文件/usr/local/openresty/nginx/jwt-lua/resty/uuid.lua

local M  = {}
local charset = {}  do -- [0-9a-zA-Z]
    for c = 48, 57  do table.insert(charset, string.char(c)) end
    for c = 65, 90  do table.insert(charset, string.char(c)) end
    for c = 97, 122 do table.insert(charset, string.char(c)) end
end
function M.uuid(length)
        local res = ""
        for i = 1, length do
                res = res .. charset[math.random(1, #charset)]
        end
        return res
end
return M
修改配置文件nginx.conf
worker_processes  1;
error_log logs/error.log info;
events {
    worker_connections 1024;
}
http {
    upstream tomcat{
     server localhost:80;
    }
    lua_package_path "/usr/local/openresty/nginx/jwt-lua/?.lua;;";
    server {
        listen 8080;
        set $uid '';
        set $uuid '';
        location / {
            access_by_lua '
            local jwt = require("resty.nginx-jwt")
            jwt.auth()
            local u = require("resty.uuid")
            ngx.var.uuid = u.uuid(64)
        ';
            default_type application/json;
            proxy_set_header uid $uid;
            proxy_set_header uuid $uuid;
            proxy_pass http://tomcat;
        }
    }
}