分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

用Netscaler的Variable和Assignment来实现计数控制

发布时间:2023-09-06 01:36责任编辑:赖小花关键词:暂无标签
用Netscaler的Variable和Assignment来实现计数控制

Variable变量和Assignment调度的手册:
https://docs.citrix.com/en-us/netscaler/11-1/appexpert/variables.html
在负载均衡做各种复杂策略的时候往往会碰到需要前后有连续性的状态计数,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 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.其他表达式(返回数值)

$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 3600
add 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 1
add ns assignment name_assign_dele -variable "$name_variable" -clear

Rewrite策略:
rewrite策略实现访问URL=03时,variable增加1,访问URL=callout时,variable清除;请求header中Accetp为text时插入COOKIE

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,值为变量的值

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的值清除

用Netscaler的Variable和Assignment来实现计数控制

原文地址:http://blog.51cto.com/caojin/2061350

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved