前言
最近因為工作需要,要進行IVR的重構(gòu), 我們現(xiàn)在系統(tǒng)接了三家IVR服務(wù)商, N個業(yè)務(wù), 由于IVR這玩意一般只能外網(wǎng)回調(diào), 而開發(fā)環(huán)境又不允許外網(wǎng)隨便訪問,
著實煩人。 所有我們打算重構(gòu)一把, 封裝多家IVR, 對業(yè)務(wù)透明, 同時回調(diào)可以針對多家IVR服務(wù)商的不同callid直接轉(zhuǎn)發(fā)到當(dāng)時請求的同學(xué)的
開發(fā)域名去。
而不同的IVR服務(wù)商的callid參數(shù)是不同的,有的是在url里面(call_id), 有的則是直接post的json數(shù)據(jù)(callid), 所以太扯了。
直接用lua處理下, 查下redis里面這個callid當(dāng)時是哪位同學(xué)發(fā)起的請求(請求IVR的時候會寫入redis中), 直接proxy_pass到這位同學(xué)的開發(fā)域名去就ok了。
環(huán)境部署
環(huán)境直接用openresty吧, redis、json這些常用庫都已經(jīng)打包完畢, 也可以自己安裝, 就是太麻煩。
openresty
nginx配置
新建一個vhost, 配置如下
server { server_name ivr.com; access_log /home/work/log/nginx/access.ivr.log; error_log /home/work/log/nginx/error.ivr.log; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_read_timeout 30; proxy_connect_timeout 10; location /ivr/ { lua_code_cache off; resolver 8.8.8.8; set $backend ''; rewrite_by_lua_file /home/work/tengine-2.1.0/conf/lua/ivr.lua; proxy_pass http://$backend; } }
不加resolver的話可能會報錯, 無法解析,加一個8.8.8.8就可以搞定了。
lua_code_cache 是開發(fā)環(huán)境的配置, 不緩存lua代碼, 修改完lua直接生效, 不然每次要重啟nginx, 上生產(chǎn)環(huán)境要關(guān)掉, 嚴(yán)重影響性能。
不過我們這個需求主要是針對開發(fā)環(huán)境, 所以無所謂。
lua代碼
local redis = require "resty.redis"local cjson = require "cjson"local cache = redis.new()cache.connect(cache, '127.0.0.1', '6379')local args = ngx.req.get_uri_args()local uri = ngx.var.request_urilocal callid = nillocal channel = 0if string.find(uri, 'yuntongxun') then callid = args["callid"] channel = 0elseif string.find(uri, 'yunhu') then ngx.req.read_body() local body_data = ngx.req.get_body_data() local data = cjson.decode(body_data) callid = data['call_id'] channel = 1elseif string.find(uri, 'huawei') then callid = args["vSessionsId"] channel = 2else endif callid == nil then ngx.say(uri) ngx.say(cjson.encode(args)) ngx.say('callid is empty') return ''endlocal key = callid .. '_channel' .. channellocal res = cache:get(key)if res == ngx.null then ngx.say("cache get error") return ''endngx.var.backend = res
沒啥特別的, 針對多個IVR服務(wù)商, 進行解析callid, 然后拼成一個key, 去redis中查詢整個key當(dāng)時寫入的value(開發(fā)者域名),
最后設(shè)置backend整個參數(shù), 然后由nginx進行proxy_pass就完了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選