Search in sources :

Example 1 with GatewayFlowRule

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);
}
Also used : GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with GatewayFlowRule

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;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Example 3 with GatewayFlowRule

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);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet)

Example 4 with GatewayFlowRule

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;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Example 5 with GatewayFlowRule

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);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Aggregations

GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)21 GatewayParamFlowItem (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)15 HashSet (java.util.HashSet)11 Test (org.junit.Test)6 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 HashMap (java.util.HashMap)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)1 GatewayApiDefinitionManager (com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager)1 GatewayParamParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser)1 GatewayRuleManager (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager)1 RuleConstant (com.alibaba.csp.sentinel.slots.block.RuleConstant)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Set (java.util.Set)1