use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class EtcdDataSourceTest method testDynamicUpdate.
@Test
public void testDynamicUpdate() throws InterruptedException {
String demo_key = "etcd_demo_key";
ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(demo_key, (value) -> JSON.parseArray(value, FlowRule.class));
FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty());
KV kvClient = Client.builder().endpoints(endPoints).build().getKVClient();
final String rule1 = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 5.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]";
kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule1.getBytes()));
Thread.sleep(1000);
FlowRule flowRule = FlowRuleManager.getRules().get(0);
Assert.assertTrue(flowRule.getResource().equals("TestResource"));
Assert.assertTrue(flowRule.getCount() == 5.0);
Assert.assertTrue(flowRule.getGrade() == 1);
final String rule2 = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 6.0,\n" + " \"grade\": 3,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]";
kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule2.getBytes()));
Thread.sleep(1000);
flowRule = FlowRuleManager.getRules().get(0);
Assert.assertTrue(flowRule.getResource().equals("TestResource"));
Assert.assertTrue(flowRule.getCount() == 6.0);
Assert.assertTrue(flowRule.getGrade() == 3);
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class DemoFlowRuleApplication method initFlowQpsRule.
private static void initFlowQpsRule() {
List<FlowRule> rules = new ArrayList<FlowRule>();
FlowRule rule1 = new FlowRule();
rule1.setResource(RESOURCE_KEY);
// set limit qps to 5
rule1.setCount(5);
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule1.setLimitApp("default");
rules.add(rule1);
FlowRuleManager.loadRules(rules);
System.out.println("Flow rule loaded: " + rules);
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule 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.FlowRule in project spring-cloud-alibaba by alibaba.
the class SentinelConverterTests method testConverterEmptyContent.
@Test
public void testConverterEmptyContent() {
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter.convert("");
assertThat(flowRules.size()).isEqualTo(0);
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project spring-cloud-alibaba by alibaba.
the class SentinelConverterTests method testJsonConverter.
@Test
public void testJsonConverter() {
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter.convert(readFileContent("classpath: flowrule.json"));
assertThat(flowRules.size()).isEqualTo(1);
assertThat(flowRules.get(0).getResource()).isEqualTo("resource");
assertThat(flowRules.get(0).getLimitApp()).isEqualTo("default");
assertThat(String.valueOf(flowRules.get(0).getCount())).isEqualTo("1.0");
assertThat(flowRules.get(0).getControlBehavior()).isEqualTo(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
assertThat(flowRules.get(0).getStrategy()).isEqualTo(RuleConstant.STRATEGY_DIRECT);
assertThat(flowRules.get(0).getGrade()).isEqualTo(RuleConstant.FLOW_GRADE_QPS);
}
Aggregations