本文共 2814 字,大约阅读时间需要 9 分钟。
用Netscaler的Variable和Assignment来实现计数控制
Variable变量和Assignment调度的手册:
在负载均衡做各种复杂策略的时候往往会碰到需要前后有连续性的状态计数,netscaler作为一个策略封装比较友好的ADC在提供完善策略安全性保障的同时还提供了比较开放的编程环境。在编程中很关键的一环就是变量的定义和使用,我们就变量这一话题来看netscaler能够提供怎样的功能。本文抛砖引玉为目的,提供一个思路供大家参考,具体应用场景还需要大家发散性思考。定义变量
长整型: ------------------------------- 数组型: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清除add ns assignment name_assign_add -variable "$name_variable" -add 1add ns assignment my_assign -variable "$my_variable[HTTP.REQ.HEADER(\"CJ_Cookie\")]" -add 1计算好的变量Variable可以在Policy里当Action使用,或者在表达式中来做判断依据
调用的方法如下:$variable_name.valueExists.其他表达式(返回布尔值)或$variable_name.valueCount.其他表达式(返回数值)$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.
$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)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的动作或着在action中定义插入一个cookie值,名字为CJ_Count: 值为变量的值
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"测试案例use case:
定义变量,做为计数用户访问或登录次数
add ns variable name_variable -type ulong -init 1 -expires 3600add ns variable name_variable_dele -type ulong -init 1 -expires 3600调用两个变量并计算:
name_assign_add 每次增加1 ------------------ name_assign_dele 每次把变量清除 add ns assignment name_assign_add -variable "$name_variable" -add 1add ns assignment name_assign_dele -variable "$name_variable" -clearRewrite策略:
rewrite策略实现访问URL=03时,variable增加1,访问URL=callout时,variable清除;请求header中Accetp为text时插入COOKIEadd rewrite policy rw_policy_variable "HTTP.REQ.HEADER(\"Accept\").CONTAINS(\"text\")" rw_action_variableadd rewrite policy rw_policy_variable_add "HTTP.REQ.URL.PATH.GET(1).EQ(\"03\")" name_assign_addadd rewrite policy rw_policy_variable_dele "HTTP.REQ.URL.PATH.GET(1).EQ(\"callout\")" name_assign_deleaction为:插入cookie,名字为CJ_Count,值为变量的值
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"效果:
登录主页时,由于accept为text,遂插入cookie:CJ_Cookie=1每次访问URL=/03时Cookie的值+1访问URL=/callout时,把cookie的值清除转载于:https://blog.51cto.com/caojin/2061350