use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project Sentinel by alibaba.
the class GatewayParamParserTest method testParseParametersNoParamItem.
@Test
public void testParseParametersNoParamItem() {
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
GatewayParamParser<Object> parser = new GatewayParamParser<>(itemParser);
// Create a fake request.
Object request = new Object();
// Prepare gateway rules.
Set<GatewayFlowRule> rules = new HashSet<>();
String routeId1 = "my_test_route_A";
rules.add(new GatewayFlowRule(routeId1).setCount(5).setIntervalSec(1));
rules.add(new GatewayFlowRule(routeId1).setCount(10).setControlBehavior(2).setMaxQueueingTimeoutMs(1000));
GatewayRuleManager.loadRules(rules);
Object[] params = parser.parseParameterFor(routeId1, request, routeIdPredicate);
assertThat(params.length).isEqualTo(1);
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project Sentinel by alibaba.
the class GatewayFlowRuleEntity method toGatewayFlowRule.
public GatewayFlowRule toGatewayFlowRule() {
GatewayFlowRule rule = new GatewayFlowRule();
rule.setResource(resource);
rule.setResourceMode(resourceMode);
rule.setGrade(grade);
rule.setCount(count);
rule.setIntervalSec(calIntervalSec(interval, intervalUnit));
rule.setControlBehavior(controlBehavior);
if (burst != null) {
rule.setBurst(burst);
}
if (maxQueueingTimeoutMs != null) {
rule.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
}
if (paramItem != null) {
GatewayParamFlowItem ruleItem = new GatewayParamFlowItem();
rule.setParamItem(ruleItem);
ruleItem.setParseStrategy(paramItem.getParseStrategy());
ruleItem.setFieldName(paramItem.getFieldName());
ruleItem.setPattern(paramItem.getPattern());
if (paramItem.getMatchStrategy() != null) {
ruleItem.setMatchStrategy(paramItem.getMatchStrategy());
}
}
return rule;
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project matecloud by matevip.
the class SentinelConfig method initGatewayRules.
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("aliyun_route").setCount(10).setIntervalSec(1));
rules.add(new GatewayFlowRule("aliyun_route").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
rules.add(new GatewayFlowRule("httpbin_route").setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));
rules.add(new GatewayFlowRule("httpbin_route").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));
rules.add(new GatewayFlowRule("httpbin_route").setCount(2).setIntervalSec(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("type").setPattern("warn").setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)));
rules.add(new GatewayFlowRule("some_customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));
GatewayRuleManager.loadRules(rules);
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project RuoYi-Cloud-Plus by JavaLionLi.
the class GatewayFlowRuleEntity method toGatewayFlowRule.
public GatewayFlowRule toGatewayFlowRule() {
GatewayFlowRule rule = new GatewayFlowRule();
rule.setResource(resource);
rule.setResourceMode(resourceMode);
rule.setGrade(grade);
rule.setCount(count);
rule.setIntervalSec(calIntervalSec(interval, intervalUnit));
rule.setControlBehavior(controlBehavior);
if (burst != null) {
rule.setBurst(burst);
}
if (maxQueueingTimeoutMs != null) {
rule.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
}
if (paramItem != null) {
GatewayParamFlowItem ruleItem = new GatewayParamFlowItem();
rule.setParamItem(ruleItem);
ruleItem.setParseStrategy(paramItem.getParseStrategy());
ruleItem.setFieldName(paramItem.getFieldName());
ruleItem.setPattern(paramItem.getPattern());
if (paramItem.getMatchStrategy() != null) {
ruleItem.setMatchStrategy(paramItem.getMatchStrategy());
}
}
return rule;
}
use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project novel-cloud by 201206030.
the class GatewayConfiguration method initGatewayRules.
/**
* 自定义网关限流规则(反爬虫机制)
* 1.对所有api接口通过IP进行限流,每个IP,2秒钟内请求数量大于10,即视为爬虫
* 2.对小说内容接口访问进行限流,每个IP,1秒钟请求数量大于1,则视为爬虫
*/
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(10).setIntervalSec(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
rules.add(new GatewayFlowRule("book_content_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
GatewayRuleManager.loadRules(rules);
}
Aggregations