有针对性的对url进行一致性hash 定向负载到后端Nginx,提高Nginx缓存系统命中率。
ocal http = require("resty.http")
local httpc = http.new()
local hosts = {"192.168.1.101","192.168.1.102"}
local item_id= ngx.var.arg_id
local id_hash = ngx.crc32_long(item_id)
local index = (id_hash % 2) +1
local resp, err = httpc:request_uri("http://"..hosts[index], {
method = "GET",
path = "/sogou?query=resty.http",
headers = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
}
})
if not resp then
ngx.say("request error :", err)
return
end
ngx.say(resp.body)
httpc:close()