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();
}
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");
}
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);
}
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);
}
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);
}
Aggregations