云存储能用来做网站吗,苏州学做网站,邢台精品网站建设,做网站前台用什么系统几万台设备有windows有安卓还有linux系统#xff0c;每个设备三分钟就会向服务器post设备的硬件信息#xff0c;数据格式json#xff0c;后台管理界面只需要最新的数据#xff0c;不需要历史数据#xff0c;业务逻辑非常简单#xff0c;PHP代码就几行#xff0c;已经…系统几万台设备有windows有安卓还有linux系统每个设备三分钟就会向服务器post设备的硬件信息数据格式json后台管理界面只需要最新的数据不需要历史数据业务逻辑非常简单PHP代码就几行已经优化到极致了但是架不住频率太高了。服务器负载就被这个的简单逻辑给耗尽。历史原因客户端已经不可能修改只能在服务端进行优化了。 做法也很简单就是不用php-fpmnginx接收到数据直接把post数据存储到redis里面供管理后台直接使用。 ngx如何连接redis请参考我的另一篇文件NgxLuaRedis 实时IP黑名单系统https://blog.csdn.net/hangbobo/article/details/142763400在这里我只接上代码
local my_cache ngx.shared.my_cache
local redis_connectloadstring(my_cache:get(redis_connect))
local redredis_connect()
red:hincrby(store_ip:02,hardware,1)local auth_header ngx.req.get_headers()[Authorization]
if string.len(auth_header) 8 thenlocal token string.sub(auth_header,8)if string.len(token) 0 thenngx.req.read_body() local data ngx.req.get_body_data()local cjson require cjson.safelocal obj cjson.decode(data)obj.timeos.date(%Y-%m-%d %H:%M:%S, ngx.time())datacjson.encode(obj)red:hset(hardware,token,data)ngx.say({msg:succeed,status_code:200,data:[..data..]})ngx.exit(200)end
end
ngx.exit(404)
解析json代码并增加当前时间这个是非必须的只是为了验证数据是不是最新提交的。 我的设备唯id是通过header头的”Authorization“ 传递的你需要结合你你的实际代码。
代码存储为 /www/server/nginx/conf/hardware.lua nginx配置
lua_shared_dict my_cache 10m;
server
{listen 80;listen 443 ssl;http2 on;...........default_type application/json; access_by_lua_file /www/server/nginx/conf/access.lua; location /boxs/hardware {access_by_lua_file /www/server/nginx/conf/hardware.lua;}...........
}