用Netscaler的Variable和Assignment來實現計數控制

用Netscaler的Variable和Assignment來實現計數控制html

Variable變量和Assignment調度的手冊:
https://docs.citrix.com/en-us/netscaler/11-1/appexpert/variables.html
在負載均衡作各類複雜策略的時候每每會碰到須要先後有連續性的狀態計數,netscaler做爲一個策略封裝比較友好的ADC在提供完善策略安全性保障的同時還提供了比較開放的編程環境。在編程中很關鍵的一環就是變量的定義和使用,咱們就變量這一話題來看netscaler可以提供怎樣的功能。本文拋磚引玉爲目的,提供一個思路供你們參考,具體應用場景還須要你們發散性思考。express

定義變量
長整型: ------------------------------- 數組型:
用Netscaler的Variable和Assignment來實現計數控制用Netscaler的Variable和Assignment來實現計數控制編程

add ns variable name_variable -type ulong -init 1 -expires 3600
add ns variable my_variable -type "map(text(20),ulong,500)" -init 15 -expires 600數組

調用變量進行計算
進行計算的方式有以下:add加法、set重設成某個值、sub減法、append附加、clear清除
用Netscaler的Variable和Assignment來實現計數控制
add ns assignment name_assign_add -variable "$name_variable" -add 1
add ns assignment my_assign -variable "$my_variable[HTTP.REQ.HEADER(\"CJ_Cookie\")]" -add 1安全

計算好的變量Variable能夠在Policy裏當Action使用,或者在表達式中來作判斷依據
調用的方法以下:$variable_name.valueExists.其餘表達式(返回布爾值)或$variable_name.valueCount.其餘表達式(返回數值)cookie

$name.valueExists(key-expression). Returns true if there is a value in the map selected by the key-expression. Otherwise returns false. This function will update the expiration and LRU information if the map entry exists, but will not create a new map entry if the value does not exist.app

$name.valueCount. Returns the number of values currently held by the variable. This is the number of entries in a map. For a singleton variable, this is 0 if the variable is uninitialized or 1 otherwise.負載均衡

例如:
$name_variable.GE(10);
$my_variable.valueCount.EQ(10)
用Netscaler的Variable和Assignment來實現計數控制
add rewrite action rw_action_variable_waring2 insert_after_all "HTTP.RES.BODY(59999).SET_TEXT_MODE(IGNORECASE)" "\"this is waring\"" -pattern "隨波漢化版"
add rewrite policy rw_policy_variable_waring "$name_variable.GE(10)" rw_action_variable_waring2
實現效果:當變量大於10的時候執行插入body的動做ide

或着在action中定義插入一個cookie值,名字爲CJ_Count: 值爲變量的值
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"測試

測試案例use case:

定義變量,作爲計數用戶訪問或登陸次數

用Netscaler的Variable和Assignment來實現計數控制
add ns variable name_variable -type ulong -init 1 -expires 3600
add ns variable name_variable_dele -type ulong -init 1 -expires 3600

調用兩個變量並計算:
name_assign_add 每次增長1 ------------------ name_assign_dele 每次把變量清除

用Netscaler的Variable和Assignment來實現計數控制用Netscaler的Variable和Assignment來實現計數控制
用Netscaler的Variable和Assignment來實現計數控制
add ns assignment name_assign_add -variable "$name_variable" -add 1
add ns assignment name_assign_dele -variable "$name_variable" -clear

Rewrite策略:
rewrite策略實現訪問URL=03時,variable增長1,訪問URL=callout時,variable清除;請求header中Accetp爲text時插入COOKIE
用Netscaler的Variable和Assignment來實現計數控制
add rewrite policy rw_policy_variable "HTTP.REQ.HEADER(\"Accept\").CONTAINS(\"text\")" rw_action_variable
add rewrite policy rw_policy_variable_add "HTTP.REQ.URL.PATH.GET(1).EQ(\"03\")" name_assign_add
add rewrite policy rw_policy_variable_dele "HTTP.REQ.URL.PATH.GET(1).EQ(\"callout\")" name_assign_dele

action爲:插入cookie,名字爲CJ_Count,值爲變量的值
用Netscaler的Variable和Assignment來實現計數控制
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"

效果:
登陸主頁時,因爲accept爲text,遂插入cookie:CJ_Cookie=1
用Netscaler的Variable和Assignment來實現計數控制
用Netscaler的Variable和Assignment來實現計數控制
每次訪問URL=/03時Cookie的值+1
用Netscaler的Variable和Assignment來實現計數控制
用Netscaler的Variable和Assignment來實現計數控制
訪問URL=/callout時,把cookie的值清除
用Netscaler的Variable和Assignment來實現計數控制

用Netscaler的Variable和Assignment來實現計數控制

相關文章
相關標籤/搜索