Search in sources :

Example 1 with AuthorityRule

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule in project Sentinel by alibaba.

the class AuthorityDemo method initBlackRules.

private static void initBlackRules() {
    AuthorityRule rule = new AuthorityRule();
    rule.setResource(RESOURCE_NAME);
    rule.setStrategy(RuleConstant.AUTHORITY_BLACK);
    rule.setLimitApp("appA,appB");
    AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)

Example 2 with AuthorityRule

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule 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 3 with AuthorityRule

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule in project spring-boot-student by wyh-spring-ecosystem-student.

the class JsonSerializeTest method authorityRuleJsonSerializeTest.

@Test
public void authorityRuleJsonSerializeTest() {
    AuthorityRuleEntity emptyRule = new AuthorityRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    AuthorityRuleEntity authorityRule = new AuthorityRuleEntity();
    AuthorityRule rule = new AuthorityRule();
    rule.setStrategy(0).setLimitApp("default").setResource("rs");
    authorityRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"limitApp\":\"default\",\"resource\":\"rs\",\"strategy\":0}}".equals(JSON.toJSONString(authorityRule)));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Test(org.junit.Test)

Example 4 with AuthorityRule

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule in project Sentinel by alibaba.

the class AuthorityDemo method initWhiteRules.

private static void initWhiteRules() {
    AuthorityRule rule = new AuthorityRule();
    rule.setResource(RESOURCE_NAME);
    rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
    rule.setLimitApp("appA,appE");
    AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)

Example 5 with AuthorityRule

use of com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule in project Sentinel by alibaba.

the class JsonSerializeTest method authorityRuleJsonSerializeTest.

@Test
public void authorityRuleJsonSerializeTest() {
    AuthorityRuleEntity emptyRule = new AuthorityRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    AuthorityRuleEntity authorityRule = new AuthorityRuleEntity();
    AuthorityRule rule = new AuthorityRule();
    rule.setStrategy(0).setLimitApp("default").setResource("rs");
    authorityRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"limitApp\":\"default\",\"resource\":\"rs\",\"strategy\":0}}".equals(JSON.toJSONString(authorityRule)));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Test(org.junit.Test)

Aggregations

AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)6 AuthorityRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 PostConstruct (javax.annotation.PostConstruct)1