Search in sources :

Example 6 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class ZookeeperDataSourceTest method testZooKeeperDataSourceSameZkClient.

/**
 * Test whether different dataSources can share the same zkClient when the connection parameters are the same.
 * @throws Exception
 */
@Test
public void testZooKeeperDataSourceSameZkClient() throws Exception {
    TestingServer server = new TestingServer(21813);
    server.start();
    final String remoteAddress = server.getConnectString();
    final String flowPath = "/sentinel-zk-ds-demo/flow-HK";
    final String degradePath = "/sentinel-zk-ds-demo/degrade-HK";
    ZookeeperDataSource<List<FlowRule>> flowRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, flowPath, new Converter<String, List<FlowRule>>() {

        @Override
        public List<FlowRule> convert(String source) {
            return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            });
        }
    });
    ZookeeperDataSource<List<DegradeRule>> degradeRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, degradePath, new Converter<String, List<DegradeRule>>() {

        @Override
        public List<DegradeRule> convert(String source) {
            return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
            });
        }
    });
    Assert.assertTrue(flowRuleZkDataSource.getZkClient() == degradeRuleZkDataSource.getZkClient());
    final String groupId = "sentinel-zk-ds-demo";
    final String flowDataId = "flow-HK";
    final String degradeDataId = "degrade-HK";
    final String scheme = "digest";
    final String auth = "root:123456";
    AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes());
    List<AuthInfo> authInfoList = Collections.singletonList(authInfo);
    ZookeeperDataSource<List<FlowRule>> flowRuleZkAutoDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, authInfoList, groupId, flowDataId, new Converter<String, List<FlowRule>>() {

        @Override
        public List<FlowRule> convert(String source) {
            return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            });
        }
    });
    ZookeeperDataSource<List<DegradeRule>> degradeRuleZkAutoDataSource = new ZookeeperDataSource<List<DegradeRule>>(remoteAddress, authInfoList, groupId, degradeDataId, new Converter<String, List<DegradeRule>>() {

        @Override
        public List<DegradeRule> convert(String source) {
            return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
            });
        }
    });
    Assert.assertTrue(flowRuleZkAutoDataSource.getZkClient() == degradeRuleZkAutoDataSource.getZkClient());
    server.stop();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) AuthInfo(org.apache.curator.framework.AuthInfo) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) List(java.util.List) TypeReference(com.alibaba.fastjson.TypeReference) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Test(org.junit.Test)

Example 7 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule 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 8 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project spring-cloud-alibaba by alibaba.

the class SentinelConfigBuilder method build.

@Override
public SentinelCircuitBreakerConfiguration build() {
    Assert.hasText(resourceName, "resourceName cannot be empty");
    List<DegradeRule> rules = Optional.ofNullable(this.rules).orElse(new ArrayList<>());
    EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT);
    return new SentinelCircuitBreakerConfiguration().setResourceName(this.resourceName).setEntryType(entryType).setRules(rules);
}
Also used : EntryType(com.alibaba.csp.sentinel.EntryType) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 9 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project retrofit-spring-boot-starter by LianjiaTech.

the class SentinelRetrofitDegrade method loadDegradeRules.

@Override
public void loadDegradeRules(Class<?> retrofitInterface) {
    Method[] methods = retrofitInterface.getMethods();
    List<DegradeRule> rules = new ArrayList<>();
    for (Method method : methods) {
        if (isDefaultOrStatic(method)) {
            continue;
        }
        // 获取熔断配置
        SentinelDegrade sentinelDegrade = AnnotationExtendUtils.findAnnotationIncludeClass(method, SentinelDegrade.class);
        if (sentinelDegrade == null) {
            continue;
        }
        DegradeRule degradeRule = new DegradeRule().setCount(sentinelDegrade.count()).setTimeWindow(sentinelDegrade.timeWindow()).setGrade(sentinelDegrade.grade());
        degradeRule.setResource(parseResourceName(method));
        rules.add(degradeRule);
    }
    DegradeRuleManager.loadRules(rules);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 10 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project JHodgepodge by werwolfGu.

the class ExceptionCountDegradeDemo method initDegradeRule.

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set limit exception count to 4
    rule.setCount(4);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
    /**
     * When degrading by {@link RuleConstant#DEGRADE_GRADE_EXCEPTION_COUNT}, time window
     * less than 60 seconds will not work as expected. Because the exception count is
     * summed by minute, when a short time window elapsed, the degradation condition
     * may still be satisfied.
     */
    rule.setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
Also used : ArrayList(java.util.ArrayList) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Aggregations

DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)34 ArrayList (java.util.ArrayList)12 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)9 Test (org.junit.Test)4 List (java.util.List)3 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)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 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 SentinelDegradeRule (com.github.howinfun.sentinel.pojo.SentinelDegradeRule)2 SentinelFlowRule (com.github.howinfun.sentinel.pojo.SentinelFlowRule)2 HashSet (java.util.HashSet)2 RuleData (org.apache.shenyu.common.dto.RuleData)2 SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)2 EntryType (com.alibaba.csp.sentinel.EntryType)1 DegradeRuleManager (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager)1 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)1