人妻系列无码专区av在线,国内精品久久久久久婷婷,久草视频在线播放,精品国产线拍大陆久久尤物

idmicom重置密碼(Openresty灰度發(fā)布及版本約束)

idmicom重置密碼(Openresty灰度發(fā)布及版本約束)

己癡香 2025-04-11 科技 15 次瀏覽 0個(gè)評(píng)論
目標(biāo)利用openresty配合Lua腳本實(shí)現(xiàn)基于redis配置進(jìn)行灰度發(fā)布及最小版本約束。實(shí)現(xiàn)如下功能:1、隨機(jī)灰度2、基于用戶ID灰度(用戶ID%100<Radio)3、基于指定用戶ID灰度(例如用戶ID:2、3)4、基于App版本號(hào)進(jìn)行灰度(例如內(nèi)部版本號(hào):31)5、全量灰度6、App最小版本約束代碼約束1、App請(qǐng)求頭中攜帶客戶端類型(X-App-Type)、客戶端版本號(hào)(X-App-Version)2、App請(qǐng)求頭中攜帶Token信息(X-Client-Token),用于獲取用戶ID(測(cè)試腳本中token生成規(guī)則為 userid_token),實(shí)際使用中,需要根據(jù)token機(jī)制進(jìn)行用戶ID轉(zhuǎn)換(修改getUserId方法)3、代碼中自動(dòng)忽略了版本號(hào)為空或?yàn)?的情況,如果需要判斷,需要修改分發(fā)邏輯4、通過(guò)修改send_upgrade方法,自行配置版本過(guò)低的提示5、客戶端版本號(hào)為數(shù)字6、客戶端類型為數(shù)字;代碼中100:代表ios 200:代表androidRedis 配置參考(gray.config){ "ratio": "30", "minVersion": "1", "versions": [ "10" ], "type": 1, "gray": "192.168.1.2:8080", "default": "192.168.1.2:8081", "userIds": [ "1" ]}Nginx 全局配置 增加如下代碼....http{ .... lua_code_cache on; lua_shared_dict gray_cache 10m; ....}Nginx 轉(zhuǎn)發(fā)配置server { listen 80; location / { set $target ''; default_type text/html; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_by_lua_file /etc/nginx/lua/gray.lua; proxy_pass http://$target$request_uri; }}Lua腳本local redis = require "resty.redis";local cjson = require("cjson")local function isEmpty(s) return s == nil or s == ''endlocal function stringToInt(str) if isEmpty(str) then return 0 end local number = tonumber(str) if not number then return 0 end return numberendlocal function left(str, split) local index = string.find(str, split) if not index then return nil end local result = string.sub(str, 0, index - 1) return resultendlocal function getUserId() -- 根據(jù)token計(jì)算用戶ID,需根據(jù)自己的業(yè)務(wù)就行替換 local token = ngx.req.get_headers()["X-Client-Token"] if isEmpty(token) then return 0 end local uidStr = left(token, "_") if isEmpty(uidStr) then return 0 end return stringToInt(uidStr)endlocal function getClientVersion() local version = ngx.req.get_headers()["X-App-Version"] return stringToInt(version)endlocal function getClientType() -- 客戶端類型,在這個(gè)地方 100表示ios 200表示 安卓 local version = ngx.req.get_headers()["X-App-Type"] return stringToInt(version)endlocal function close_redis(redis_cluster) if not redis_cluster then return end local pool_max_idle_time = 10000 local pool_size = 100 local ok, err = redis_cluster:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.log(ngx.ERR, "set keepalive fail ", err) endendlocal function read_gray_config(address, port, password, key, default_config) local redis_cache = redis:new(); redis_cache:set_timeout(1000); local ok, err = redis_cache:connect(address, port); if not ok then close_redis(redis_cache) ngx.log(ngx.ERR, "redis 連接錯(cuò)誤: ", err) return default_config; end if not isEmpty(password) then local ok, err = redis_cache:auth(password) if not ok then ngx.log(ngx.ERR, "redis 攜帶密碼連接錯(cuò)誤: ", err) close_redis(redis_cache) return default_config; end end local res, err = redis_cache:get(key) if not res then ngx.log(ngx.ERR, "redis讀取數(shù)據(jù)錯(cuò)誤: ", err) close_redis(redis_cache) return default_config end local json = cjson.new() if not json then ngx.log(ngx.ERR, "創(chuàng)建json錯(cuò)誤 ") close_redis(redis_cache) return default_config end if res == ngx.null then local ok, err = redis_cache:set(key, json.encode(default_config)) if not ok then ngx.log(ngx.ERR, "寫入默認(rèn)配置出錯(cuò): ", err) end ngx.log(ngx.INFO, "灰度配置為空,采用默認(rèn)配置") close_redis(redis_cache) return default_config else close_redis(redis_cache) return json.decode(res) endendlocal function load_gray(address, port, password, timeout, key, default_config) local share_cache = ngx.shared.gray_cache local cache_data = share_cache:get("config") local json = cjson.new() if not json then ngx.log(ngx.ERR, "創(chuàng)建json對(duì)象錯(cuò)誤 ") return default_config end if cache_data == nil then cache_data = read_gray_config(address, port, password, key, default_config) if cache_data == nil then ngx.log(ngx.ERR, "獲取配置信息返回null") else local ok, err = share_cache:set("config", json.encode(cache_data), timeout) if not ok then ngx.log(ngx.INFO, "刷新本地灰度配置信息失敗", err) else ngx.log(ngx.INFO, "刷新本地灰度配置信息成功") end end return cache_data else ngx.log(ngx.INFO, "采用緩存配置信息") return json.decode(cache_data) endendlocal default_cache = { type = 0, -- 灰度類型 0、關(guān)閉灰度 1、隨機(jī)灰度 2、根據(jù)用戶ID灰度 3、指定用戶ID灰度 4、指定用戶版本灰度 5、全量灰度 default = "192.168.1.2:8080", -- 正常分發(fā)地址 gray = "192.168.1.2:8081", -- 灰度分發(fā)地址 userIds = { "0" }, -- 灰度用戶ID,例如:{"2","3","4"} ratio = "0", -- 灰度分發(fā)比例 minVersion = "0", -- 客戶端最小版本號(hào) versions = { "0" } -- 灰度版本號(hào),例如:{"30","31"}}local function contains(value, list) if list == nil or isEmpty(value) then return false end for k, v in ipairs(list) do if v == value then return true; end end return false;end-- 發(fā)送版本過(guò)低消息local function send_upgrade(minVersion,clientType) local upgrade_response = '{"code":403,"data":{"version":"0","message":"您當(dāng)前的版本過(guò)低,請(qǐng)升級(jí)到最新版本!"}}' ngx.header.content_type = "application/json" ngx.say(string.format(upgrade_response,clientType,minVersion,minVersion))endlocal gray = load_gray("127.0.0.1", 6379, "", 10, "gray.config", default_cache)if gray then ngx.var.target = gray["default"] local gray_type = gray["type"] local iosMinVersion = gray["iosMinVersion"] local andoridMinVersion = gray["andoridMinVersion"] local clientType = getClientType() local request_uri = ngx.var.request_uri if (string.find(request_uri, "^/yuliao/uri/") == nil) then local clientVersion = getClientVersion() if clientType == 100 and iosMinVersion ~= nil and iosMinVersion > 0 then -- 判斷ios最小版本 local clientVersion = getClientVersion() if clientVersion > 0 and clientVersion < iosMinVersion then ngx.log(ngx.INFO,"uri:",request_uri," send ios upgrade response") send_upgrade(iosMinVersion,clientType) return; end else if clientType == 200 and andoridMinVersion ~= nil and andoridMinVersion > 0 then -- 判斷安卓最小版本 if clientVersion > 0 and clientVersion < andoridMinVersion and clientVersion ~= 1 then ngx.log(ngx.INFO,"uri:",request_uri," send android upgrade response") send_upgrade(andoridMinVersion,clientType) return; end end end end if gray_type == 1 then -- 隨機(jī)灰度 local ratio = stringToInt(gray["ratio"]) local number = math.random(100) % 100 if number < ratio then ngx.var.target = gray["gray"] ngx.log(ngx.INFO, "隨機(jī)灰度(YES):", " number:", number, " ratio:", ratio, " upstream:", ngx.var.target) else ngx.log(ngx.INFO, "隨機(jī)灰度(NO):", " number:", number, " ratio:", ratio, " upstream:", ngx.var.target) end elseif gray_type == 2 then -- 用戶ID灰度 local ratio = stringToInt(gray["ratio"]) local userId = getUserId() local number = userId % 100 if number < ratio then ngx.var.target = gray["gray"] ngx.log(ngx.INFO, "用戶ID灰度(YES):", " userId:", userId, " ratio:", ratio, " upstream:", ngx.var.target) else ngx.log(ngx.INFO, "用戶ID灰度(NO):", " userId:", userId, " ratio:", ratio, " upstream:", ngx.var.target) end elseif gray_type == 3 then -- 指定用戶ID灰度 local userId = getUserId() if userId > 0 then userId = tostring(userId) local userIds = gray["userIds"] if contains(userId, userIds) then ngx.var.target = gray["gray"] ngx.log(ngx.INFO, "指定用戶灰度(YES):", " userId:", userId, " upstream:", ngx.var.target) else ngx.log(ngx.INFO, "指定用戶灰度(NO):", " userId:", userId, " upstream:", ngx.var.target) end else ngx.log(ngx.INFO, "指定用戶灰度(NO):", " userId:", userId, " upstream:", ngx.var.target) end elseif gray_type == 4 then -- 指定用戶版本灰度 if version > 0 then local versions = gray["versions"] version = tostring(version) if contains(version, versions) then ngx.var.target = gray["gray"] ngx.log(ngx.INFO, "指定版本灰度(YES):", " version:", version, " upstream:", ngx.var.target) else ngx.log(ngx.INFO, "指定版本灰度(NO):", " version:", version, " upstream:", ngx.var.target) end else ngx.log(ngx.INFO, "指定版本灰度(NO):", " version:", version, " upstream:", ngx.var.target) end elseif gray_type == 5 then ngx.var.target = gray["gray"] ngx.log(ngx.INFO, "系統(tǒng)全量灰度(YES):", " upstream:", ngx.var.target) endelse local json = cjson.new() ngx.header.content_type = "application/json" ngx.say(cjson.encode({ code = 500, message = '無(wú)法找到轉(zhuǎn)發(fā)配置,請(qǐng)聯(lián)系管理員!' })) ngx.log(ngx.ERR, "無(wú)法找到系統(tǒng)配信息,返回500")end

轉(zhuǎn)載請(qǐng)注明來(lái)自夕逆IT,本文標(biāo)題:《idmicom重置密碼(Openresty灰度發(fā)布及版本約束)》

每一天,每一秒,你所做的決定都會(huì)改變你的人生!

發(fā)表評(píng)論

快捷回復(fù):

評(píng)論列表 (暫無(wú)評(píng)論,15人圍觀)參與討論

還沒(méi)有評(píng)論,來(lái)說(shuō)兩句吧...