A Rate-Limiting HTTP Proxy(3)Redis MySQL Logging

A Rate-Limiting HTTP Proxy(3)Redis MySQL Logging

Redis
Start the Redis Service on my Local
>redis-server conf/redis.conf

Connect to the Redis Server
>redis-cli

We use this package to access Redis lua-resty-redis
https://github.com/openresty/lua-resty-redis

Installation - I am using OpenResty bundle, so I can directly use that with
local redis = require “resty.redis”

Redis Commands
https://redis.io/commands

A helper module redis.lua
local redis = require "resty.redis"

local config = {
host = "127.0.0.1",
port = 6379,
-- pass = "1234" -- redis password
}

local _M = {}

function _M.new(self)
local red = redis:new()
red:set_timeout(1000) -- 1 second
local res = red:connect(config['host'], config['port'])
if not res then
return nil
end
if config['pass'] ~= nil then
res = red:auth(config['pass'])
if not res then
return nil
end
end
red.close = close
return red
end

function close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if self.subscribed then
return nil, "subscribed state"
end
return sock:setkeepalive(10000, 50)
end

Load the OpenResty System Module
lua_package_path "/opt/luaweb/lua/?.lua;;";

Use Redis in hello.lua
local req = require "req"
local cjson = require "cjson"
local redis = require "redis"

local args = req.getArgs()

-- convert string to JSON object
local json_str = '{"name": "Carl.Luo", "age": 35}'
local json = cjson.decode(json_str)
local key = args['key']

if key == nil or key == "" then
key = "foo"
end

local red = redis:new()
local value = red:get(key)
red:close()
ngx.say("key = " .. key .. "Value = " .. value .. "<br />")

MySQL
module lua-resty-mysql
https://github.com/openresty/lua-resty-mysql

Installation
local mysql = require "resty.mysql"

I have a local MySQL database
>mysql -h 127.0.0.1 -u root

res is a array, each item in the array is a table. That is the result from the MySQL driver.
The package we abstract some methods. mysql.lua
local mysql = require "resty.mysql"

local config = {
host = "127.0.0.1",
port = 3306,
database = "mysql",
user = "root",
password = ""
}

local _M = {}

function _M.new(self)
local db, err = mysql:new()
if not db then
return nil
end
db:set_timeout(1000) -- 1 second
local ok, err, errno, sqlstate = db:connect(config)

if not ok then
return nil
end
db.close = close
return db
end

function close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if self.subscribed then
return nil, "subscribed state"
end
return sock:setkeepalive(10000, 50) -- 10 seconds, 50 connections
end

return _M

Here is How we use that in welcome.lua
local mysql = require "mysql"
local cjson = require "cjson"
local req = require "req"

local args = req.getArgs()
local name = args['name']

if name == nil or name == "" then
name = "root"
end

name = ngx.quote_sql_str(name) -- pre handle SQL
local db = mysql:new()
local sql = "select User, Host from user where User = " .. name

ngx.say(sql)
ngx.say("<br />")

local res, err, errno, sqlstate = db:query(sql)
db:close()
if not res then
ngx.say(err)
return {}
end

ngx.say(cjson.encode(res))
ngx.say("<br />")

Logging
The method ngx.log(logging level, content)
Logging level — ngx.STDERR/EMERG/ALERT/CRIT/ERR/WARN/NOTICE/INFO/DEBUG
ngx.log(ngx.ALERT, 'print to error.log')
ngx.log(ngx.STDERR, 'print to error.log')
ngx.log(ngx.EMERG, 'print to error.log')
ngx.log(ngx.ALERT, 'print to error.log')
ngx.log(ngx.CRIT, 'print to error.log')
ngx.log(ngx.ERR, 'print to error.log')
ngx.log(ngx.WARN, 'print to error.log')
ngx.log(ngx.NOTICE, 'print to error.log')
ngx.log(ngx.INFO, 'print to error.log')
ngx.log(ngx.DEBUG, 'print to error.log’)

Control the global LEVEL in nginx.conf
error_log logs/error.log notice;


References:
https://github.com/362228416/openresty-web-dev/tree/master/demo4
https://github.com/362228416/openresty-web-dev/tree/master/demo5
https://github.com/362228416/openresty-web-dev/tree/master/demo6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值