JAVA全系列 教程
3762个小节阅读:7090.3k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
缓存命中率低
代码实现:
xxxxxxxxxx
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by wcc.
--- DateTime: 2021/9/7 16:05
---
-- 根据商品id 做流量得定向分发
-- 获取请求商品id
local uri_args = ngx.req.get_uri_args()
-- 获取商品id
local productId = uri_args["productId"]
if not productId then
ngx.log(ngx.ERR,"参数不正确。。。")
return
end
--- 设置分发地址 -- 使用redis技术动态获取
local hosts = {"192.168.66.100","192.168.66.101","192.168.66.102"}
-- 获取id 得hash值 取模做负载均衡 获取到一个地址
local hash,err = ngx.crc32_long(productId)
if not hash then
ngx.log(ngx.ERR,"HASH failed ")
return
end
-- 取模 获取hosts 个数
local index = (hash % table.getn(hosts)) +1
-- http://192.168.66.100
backend = "http://"..hosts[index]
-- 引入http 包
local http = require("resty.http")
local httpc = http:new()
-- 发送请求
local resp,err = httpc:request_uri(backend,{method="GET",keepalive_timeout=60})
if not resp then
ngx.say(ngx.ERR,err)
return
end
ngx.status = res.status
ngx.say(resp.body)
httpc:close()