Java Code Examples for com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#getRules()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#getRules() . 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: EtcdDataSourceDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        String rule_key = "sentinel_demo_rule_key";
        String yourUserName = "root";
        String yourPassWord = "12345";
        String endPoints = "http://127.0.0.1:2379";
        SentinelConfig.setConfig(EtcdConfig.END_POINTS, endPoints);
        SentinelConfig.setConfig(EtcdConfig.USER, yourUserName);
        SentinelConfig.setConfig(EtcdConfig.PASSWORD, yourPassWord);
        SentinelConfig.setConfig(EtcdConfig.CHARSET, "utf-8");
        SentinelConfig.setConfig(EtcdConfig.AUTH_ENABLE, "true");

        ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(rule_key, (rule) -> JSON.parseArray(rule, FlowRule.class));
        FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty());
        List<FlowRule> rules = FlowRuleManager.getRules();
        System.out.println(rules);
    }
 
Example 2
Source File: StandaloneRedisDataSourceTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testPubMsgAndReceiveSuccess() {
    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(1, rules.size());
    int maxQueueingTimeMs = new Random().nextInt();
    StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
    String flowRules =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisPubSubCommands<String, String> subCommands = connection.sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRules);
    subCommands.publish(channel, flowRules);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 3
Source File: SentinelModeRedisDataSourceTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectToSentinelAndPubMsgSuccess() {
    int maxQueueingTimeMs = new Random().nextInt();
    String flowRulesJson =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisCommands<String, String> subCommands = client.connect().sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRulesJson);
    subCommands.publish(channel, flowRulesJson);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 4
Source File: StandaloneRedisDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testPubMsgAndReceiveSuccess() {
    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(1, rules.size());
    int maxQueueingTimeMs = new Random().nextInt();
    StatefulRedisPubSubConnection<String, String> connection = client.connectPubSub();
    String flowRules =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisPubSubCommands<String, String> subCommands = connection.sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRules);
    subCommands.publish(channel, flowRules);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 5
Source File: SentinelModeRedisDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectToSentinelAndPubMsgSuccess() {
    int maxQueueingTimeMs = new Random().nextInt();
    String flowRulesJson =
        "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, "
            + "\"refResource\":null, "
            +
            "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":" + maxQueueingTimeMs
            + ", \"controller\":null}]";
    RedisCommands<String, String> subCommands = client.connect().sync();
    subCommands.multi();
    subCommands.set(ruleKey, flowRulesJson);
    subCommands.publish(channel, flowRulesJson);
    subCommands.exec();

    await().timeout(2, TimeUnit.SECONDS)
        .until(new Callable<List<FlowRule>>() {
            @Override
            public List<FlowRule> call() throws Exception {
                return FlowRuleManager.getRules();
            }
        }, Matchers.hasSize(1));

    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(rules.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
    String value = subCommands.get(ruleKey);
    List<FlowRule> flowRulesValuesInRedis = buildFlowConfigParser().convert(value);
    Assert.assertEquals(flowRulesValuesInRedis.size(), 1);
    Assert.assertEquals(flowRulesValuesInRedis.get(0).getMaxQueueingTimeMs(), maxQueueingTimeMs);
}
 
Example 6
Source File: SpringCouldDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@GetMapping("/get")
@ResponseBody
public List<FlowRule> get() {
    SpringCloudConfigDataSource dataSource = new SpringCloudConfigDataSource("flow_rule", converter);
    FlowRuleManager.register2Property(dataSource.getProperty());
    return FlowRuleManager.getRules();
}
 
Example 7
Source File: SpringCouldDataSourceTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * WebHook refresh config
 */
@GetMapping("/refresh")
@ResponseBody
public List<FlowRule> refresh() {
    locator.refresh();
    return FlowRuleManager.getRules();
}
 
Example 8
Source File: ConsulDataSourceTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testConsulDataSourceWhenInit() {
    List<FlowRule> rules = FlowRuleManager.getRules();
    Assert.assertEquals(this.rules, rules);
}
 
Example 9
Source File: RulesController.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@GetMapping("/flow")
public List<FlowRule> apiFlow() {
	return FlowRuleManager.getRules();
}