Search in sources :

Example 36 with FlowRule

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);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) KV(io.etcd.jetcd.KV) Test(org.junit.Test)

Example 37 with FlowRule

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);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 38 with FlowRule

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");
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) ParamFlowItem(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ArrayList(java.util.ArrayList) SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) PostConstruct(javax.annotation.PostConstruct)

Example 39 with FlowRule

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);
}
Also used : JsonConverter(com.alibaba.cloud.sentinel.datasource.converter.JsonConverter) List(java.util.List) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Example 40 with FlowRule

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);
}
Also used : JsonConverter(com.alibaba.cloud.sentinel.datasource.converter.JsonConverter) List(java.util.List) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Aggregations

FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)113 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)22 List (java.util.List)17 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)11 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Test (org.junit.jupiter.api.Test)6 ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)5 TypeReference (com.alibaba.fastjson.TypeReference)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 Before (org.junit.Before)5 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)4 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)3 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)3 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 IOException (java.io.IOException)3