use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project learn-simple by muggle0.
the class SentileConfig method initRules.
@PostConstruct
private void initRules() throws Exception {
FlowRule rule1 = new FlowRule();
rule1.setResource("test.hello");
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 每秒调用最大次数为 1 次
rule1.setCount(1);
List<FlowRule> rules = new ArrayList<>();
// DegradeRuleManager.loadRules(List< DegradeRule > rules); // 修改降级规则
rules.add(rule1);
// 将控制规则载入到 Sentinel
// AuthorityRuleManager
FlowRuleManager.loadRules(rules);
/* ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));*/
// FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
DegradeRule rule = new DegradeRule();
List<DegradeRule> rules1 = new ArrayList<>();
rule.setResource("test.hello");
rule.setCount(0.01);
// 秒级RT
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setTimeWindow(10);
rules1.add(rule);
DegradeRuleManager.loadRules(rules1);
ParamFlowRule paramFlowRule = new ParamFlowRule("resourceName").setParamIdx(0).setCount(5);
// 单独设置限流 QPS,设置param 参数限流规则
ParamFlowItem item = new ParamFlowItem().setObject("param").setClassType(int.class.getName()).setCount(10);
paramFlowRule.setParamFlowItemList(Collections.singletonList(item));
ParamFlowRuleManager.loadRules(Collections.singletonList(paramFlowRule));
SystemRule systemRule = new SystemRule();
systemRule.setHighestCpuUsage(0.8);
systemRule.setAvgRt(10);
systemRule.setQps(10);
systemRule.setMaxThread(10);
systemRule.setHighestSystemLoad(2.5);
SystemRuleManager.loadRules(Collections.singletonList(systemRule));
AuthorityRule authorityRule = new AuthorityRule();
authorityRule.setStrategy(RuleConstant.AUTHORITY_BLACK);
authorityRule.setLimitApp("127.0.0.1");
authorityRule.setResource("test.hello");
AuthorityRuleManager.loadRules(Collections.singletonList(authorityRule));
// ContextUtil.enter("xxx", "origin");
}
use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.
the class ParamFlowQpsDemo method initParamFlowRules.
private static void initParamFlowRules() {
// 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).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));
}
use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.
the class GatewayFlowSlot method checkGatewayParamFlow.
private void checkGatewayParamFlow(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
if (args == null) {
return;
}
List<ParamFlowRule> rules = GatewayRuleManager.getConvertedParamRules(resourceWrapper.getName());
if (rules == null || rules.isEmpty()) {
return;
}
for (ParamFlowRule rule : rules) {
// Initialize the parameter metrics.
ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
String triggeredParam = "";
if (args.length > rule.getParamIdx()) {
Object value = args[rule.getParamIdx()];
triggeredParam = String.valueOf(value);
}
throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
}
}
}
use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.
the class JsonSerializeTest method paramFlowRuleSerializeTest.
@Test
public void paramFlowRuleSerializeTest() {
ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
ParamFlowRule rule = new ParamFlowRule();
rule.setClusterConfig(new ParamFlowClusterConfig());
rule.setResource("rs").setLimitApp("default");
paramFlowRule.setRule(rule);
Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project spring-boot-student by wyh-spring-ecosystem-student.
the class JsonSerializeTest method paramFlowRuleSerializeTest.
@Test
public void paramFlowRuleSerializeTest() {
ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
ParamFlowRule rule = new ParamFlowRule();
rule.setClusterConfig(new ParamFlowClusterConfig());
rule.setResource("rs").setLimitApp("default");
paramFlowRule.setRule(rule);
Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Aggregations