/** * Here we prepare different parameters to validate flow control by parameters. */ privatestaticfinal Integer[] PARAMS = new Integer[] {PARAM_A, PARAM_B, PARAM_C, PARAM_D};
privatestaticvoidinitParamFlowRules(){ // QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg). ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY) .setParamIdx(0) .setGrade(RuleConstant.FLOW_GRADE_QPS) //.setDurationInSec(3) //.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) //.setMaxQueueingTimeMs(600) .setCount(5);
// We can set threshold count for specific parameter value individually. // Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int) // in index 0 will be 10, rather than the global threshold (5). ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B)) .setClassType(int.class.getName()) .setCount(10); rule.setParamFlowItemList(Collections.singletonList(item)); ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); } }