com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager Java Examples

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParamFlowQpsDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private static void initParamFlowRules() {
    // QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).
    ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY)
        .setParamIdx(0)
        .setGrade(RuleConstant.FLOW_GRADE_QPS)
        //.setDurationInSec(3)
        //.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
        //.setMaxQueueingTimeMs(600)
        .setCount(5);

    // We can set threshold count for specific parameter value individually.
    // Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)
    // in index 0 will be 10, rather than the global threshold (5).
    ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B))
        .setClassType(int.class.getName())
        .setCount(10);
    rule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #2
Source File: ModifyParamFlowRulesCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode rule data error", e);
        return CommandResponse.ofFailure(e, "decode rule data error");
    }

    RecordLog.info(String.format("[API Server] Receiving rule change (type:parameter flow rule): %s", data));

    String result = SUCCESS_MSG;
    List<ParamFlowRule> flowRules = JSONArray.parseArray(data, ParamFlowRule.class);
    ParamFlowRuleManager.loadRules(flowRules);
    if (!writeToDataSource(paramFlowWds, flowRules)) {
        result = WRITE_DS_FAILURE_MSG;
    }
    return CommandResponse.ofSuccess(result);
}
 
Example #3
Source File: ParamFlowQpsDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static void initParamFlowRules() {
    // QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).
    ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY)
        .setParamIdx(0)
        .setGrade(RuleConstant.FLOW_GRADE_QPS)
        //.setDurationInSec(3)
        //.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
        //.setMaxQueueingTimeMs(600)
        .setCount(5);

    // We can set threshold count for specific parameter value individually.
    // Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)
    // in index 0 will be 10, rather than the global threshold (5).
    ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B))
        .setClassType(int.class.getName())
        .setCount(10);
    rule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #4
Source File: ModifyParamFlowRulesCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode rule data error", e);
        return CommandResponse.ofFailure(e, "decode rule data error");
    }

    RecordLog.info(String.format("[API Server] Receiving rule change (type:parameter flow rule): %s", data));

    String result = SUCCESS_MSG;
    List<ParamFlowRule> flowRules = JSONArray.parseArray(data, ParamFlowRule.class);
    ParamFlowRuleManager.loadRules(flowRules);
    if (!writeToDataSource(paramFlowWds, flowRules)) {
        result = WRITE_DS_FAILURE_MSG;
    }
    return CommandResponse.ofSuccess(result);
}
 
Example #5
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initDynamicRuleProperty() {
    ReadableDataSource<String, List<FlowRule>> ruleSource = new NacosDataSource<>(remoteAddress, groupId,
        flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    FlowRuleManager.register2Property(ruleSource.getProperty());

    ReadableDataSource<String, List<ParamFlowRule>> paramRuleSource = new NacosDataSource<>(remoteAddress, groupId,
        paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
    ParamFlowRuleManager.register2Property(paramRuleSource.getProperty());
}
 
Example #6
Source File: FreqParamFlowSimulate.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化热点限流的规则
 */
private void initHotParamFlowRules() {
    // 设置热点参数的规则,qps 模式,阈值为5
    ParamFlowRule rule = new ParamFlowRule(resourceName)
            .setParamIdx(0)
            .setGrade(RuleConstant.FLOW_GRADE_QPS)
            .setCount(5);
    // 对userId=111的参数设置例外项,可以为该用户id的值单独设置阈值
    ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(111))
            .setClassType(int.class.getName())
            .setCount(10);
    rule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #7
Source File: FreqParamFlowController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
public FreqParamFlowController(){
    // 定义热点限流的规则,对第一个参数设置 qps 限流模式,阈值为5
    ParamFlowRule rule = new ParamFlowRule(resourceName)
            .setParamIdx(0)
            .setGrade(RuleConstant.FLOW_GRADE_QPS)
            .setCount(5);
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #8
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initDynamicRuleProperty() {
    ReadableDataSource<String, List<FlowRule>> ruleSource = new NacosDataSource<>(remoteAddress, groupId,
        flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
    FlowRuleManager.register2Property(ruleSource.getProperty());

    ReadableDataSource<String, List<ParamFlowRule>> paramRuleSource = new NacosDataSource<>(remoteAddress, groupId,
        paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
    ParamFlowRuleManager.register2Property(paramRuleSource.getProperty());
}
 
Example #9
Source File: AbstractDataSourceProperties.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public void postRegister(AbstractDataSource dataSource) {
	switch (this.getRuleType()) {
	case FLOW:
		FlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case DEGRADE:
		DegradeRuleManager.register2Property(dataSource.getProperty());
		break;
	case PARAM_FLOW:
		ParamFlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case SYSTEM:
		SystemRuleManager.register2Property(dataSource.getProperty());
		break;
	case AUTHORITY:
		AuthorityRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_FLOW:
		GatewayRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_API_GROUP:
		GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
		break;
	default:
		break;
	}
}
 
Example #10
Source File: SentinelEndpoint.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@ReadOperation
public Map<String, Object> invoke() {
	final Map<String, Object> result = new HashMap<>();
	if (sentinelProperties.isEnabled()) {

		result.put("appName", AppNameUtil.getAppName());
		result.put("logDir", LogBase.getLogBaseDir());
		result.put("logUsePid", LogBase.isLogNameUsePid());
		result.put("blockPage", SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY));
		result.put("metricsFileSize", SentinelConfig.singleMetricFileSize());
		result.put("metricsFileCharset", SentinelConfig.charset());
		result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());
		result.put("consoleServer", TransportConfig.getConsoleServerList());
		result.put("clientIp", TransportConfig.getHeartbeatClientIp());
		result.put("heartbeatIntervalMs", TransportConfig.getHeartbeatIntervalMs());
		result.put("clientPort", TransportConfig.getPort());
		result.put("coldFactor", sentinelProperties.getFlow().getColdFactor());
		result.put("filter", sentinelProperties.getFilter());
		result.put("datasource", sentinelProperties.getDatasource());

		final Map<String, Object> rules = new HashMap<>();
		result.put("rules", rules);
		rules.put("flowRules", FlowRuleManager.getRules());
		rules.put("degradeRules", DegradeRuleManager.getRules());
		rules.put("systemRules", SystemRuleManager.getRules());
		rules.put("authorityRule", AuthorityRuleManager.getRules());
		rules.put("paramFlowRule", ParamFlowRuleManager.getRules());
	}
	return result;
}
 
Example #11
Source File: GetParamFlowRulesCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    return CommandResponse.ofSuccess(JSON.toJSONString(ParamFlowRuleManager.getRules()));
}
 
Example #12
Source File: GetParamFlowRulesCommandHandler.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    return CommandResponse.ofSuccess(JSON.toJSONString(ParamFlowRuleManager.getRules()));
}