com.alibaba.csp.sentinel.slots.system.SystemRuleManager Java Examples

The following examples show how to use com.alibaba.csp.sentinel.slots.system.SystemRuleManager. 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: FileDataSourceDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void listenRules() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
    String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
    String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");

    // Data source for FlowRule
    FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
        flowRulePath, flowRuleListParser);
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

    // Data source for DegradeRule
    FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
        = new FileRefreshableDataSource<>(
        degradeRulePath, degradeRuleListParser);
    DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());

    // Data source for SystemRule
    FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
        = new FileRefreshableDataSource<>(
        systemRulePath, systemRuleListParser);
    SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
 
Example #2
Source File: SystemGuardDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private static void initSystemRule() {
    List<SystemRule> rules = new ArrayList<SystemRule>();
    SystemRule rule = new SystemRule();
    // max load is 3
    rule.setHighestSystemLoad(3.0);
    // max cpu usage is 60%
    rule.setHighestCpuUsage(0.6);
    // max avg rt of all request is 10 ms
    rule.setAvgRt(10);
    // max total qps is 20
    rule.setQps(20);
    // max parallel working thread is 10
    rule.setMaxThread(10);

    rules.add(rule);
    SystemRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #3
Source File: FileDataSourceDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void listenRules() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
    String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
    String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");

    // Data source for FlowRule
    FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
        flowRulePath, flowRuleListParser);
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

    // Data source for DegradeRule
    FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
        = new FileRefreshableDataSource<>(
        degradeRulePath, degradeRuleListParser);
    DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());

    // Data source for SystemRule
    FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
        = new FileRefreshableDataSource<>(
        systemRulePath, systemRuleListParser);
    SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
 
Example #4
Source File: SystemGuardDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static void initSystemRule() {
    List<SystemRule> rules = new ArrayList<SystemRule>();
    SystemRule rule = new SystemRule();
    // max load is 3
    rule.setHighestSystemLoad(3.0);
    // max cpu usage is 60%
    rule.setHighestCpuUsage(0.6);
    // max avg rt of all request is 10 ms
    rule.setAvgRt(10);
    // max total qps is 20
    rule.setQps(20);
    // max parallel working thread is 10
    rule.setMaxThread(10);

    rules.add(rule);
    SystemRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example #5
Source File: SendMetricCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * add current cpu usage and load to the metric list.
 *
 * @param list metric list, should not be null
 */
private void addCpuUsageAndLoad(List<MetricNode> list) {
    long time = TimeUtil.currentTimeMillis() / 1000 * 1000;
    double load = SystemRuleManager.getCurrentSystemAvgLoad();
    double usage = SystemRuleManager.getCurrentCpuUsage();
    if (load > 0) {
        MetricNode loadNode = toNode(load, time, Constants.SYSTEM_LOAD_RESOURCE_NAME);
        list.add(loadNode);
    }
    if (usage > 0) {
        MetricNode usageNode = toNode(usage, time, Constants.CPU_USAGE_RESOURCE_NAME);
        list.add(usageNode);
    }
}
 
Example #6
Source File: FetchActiveRuleCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String type = request.getParam("type");
    if ("flow".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(FlowRuleManager.getRules()));
    } else if ("degrade".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(DegradeRuleManager.getRules()));
    } else if ("authority".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(AuthorityRuleManager.getRules()));
    } else if ("system".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(SystemRuleManager.getRules()));
    } else {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid type"));
    }
}
 
Example #7
Source File: SystemRuleTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testSystemRule_load() {
    SystemRule systemRule = new SystemRule();

    systemRule.setAvgRt(4000L);

    SystemRuleManager.loadRules(Collections.singletonList(systemRule));
}
 
Example #8
Source File: SendMetricCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * add current cpu usage and load to the metric list.
 *
 * @param list metric list, should not be null
 */
private void addCpuUsageAndLoad(List<MetricNode> list) {
    long time = TimeUtil.currentTimeMillis() / 1000 * 1000;
    double load = SystemRuleManager.getCurrentSystemAvgLoad();
    double usage = SystemRuleManager.getCurrentCpuUsage();
    if (load > 0) {
        MetricNode loadNode = toNode(load, time, Constants.SYSTEM_LOAD_RESOURCE_NAME);
        list.add(loadNode);
    }
    if (usage > 0) {
        MetricNode usageNode = toNode(usage, time, Constants.CPU_USAGE_RESOURCE_NAME);
        list.add(usageNode);
    }
}
 
Example #9
Source File: FetchActiveRuleCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String type = request.getParam("type");
    if ("flow".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(FlowRuleManager.getRules()));
    } else if ("degrade".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(DegradeRuleManager.getRules()));
    } else if ("authority".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(AuthorityRuleManager.getRules()));
    } else if ("system".equalsIgnoreCase(type)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(SystemRuleManager.getRules()));
    } else {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid type"));
    }
}
 
Example #10
Source File: AppLifecycleBean.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
void onStart(@Observes StartupEvent ev) {
    LOGGER.info("The application is starting...");
    FlowRule rule = new FlowRule()
            .setCount(1)
            .setGrade(RuleConstant.FLOW_GRADE_QPS)
            .setResource("GET:/hello/txt")
            .setLimitApp("default")
            .as(FlowRule.class);
    FlowRuleManager.loadRules(Arrays.asList(rule));

    SystemRule systemRule = new SystemRule();
    systemRule.setLimitApp("default");
    systemRule.setAvgRt(3000);
    SystemRuleManager.loadRules(Arrays.asList(systemRule));

    DegradeRule degradeRule1 = new DegradeRule("greeting1")
            .setCount(1)
            .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
            .setTimeWindow(10)
            .setMinRequestAmount(1);

    DegradeRule degradeRule2 = new DegradeRule("greeting2")
            .setCount(1)
            .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
            .setTimeWindow(10)
            .setMinRequestAmount(1);
    DegradeRuleManager.loadRules(Arrays.asList(degradeRule1, degradeRule2));
}
 
Example #11
Source File: SystemRuleTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSystemRule_load() {
    SystemRule systemRule = new SystemRule();

    systemRule.setAvgRt(4000L);

    SystemRuleManager.loadRules(Collections.singletonList(systemRule));
}
 
Example #12
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 #13
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;
}