Java Code Examples for com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#register2Property()
The following examples show how to use
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#register2Property() .
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 |
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: FileDataSourceInit.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Override public void init() throws Exception { // A fake path. String flowRuleDir = System.getProperty("user.home") + "/sentinel/rules"; String flowRuleFile = "flowRule.json"; String flowRulePath = flowRuleDir + "/" + flowRuleFile; ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>( flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}) ); // Register to flow rule manager. FlowRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson); // Register to writable data source registry so that rules can be updated to file // when there are rules pushed from the Sentinel Dashboard. WritableDataSourceRegistry.registerFlowDataSource(wds); }
Example 3
Source File: FileDataSourceDemo.java From Sentinel with Apache License 2.0 | 6 votes |
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: ConsulDataSourceTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Before public void init() { this.consul = ConsulStarterBuilder.consulStarter() .build() .start(); int port = consul.getHttpPort(); String host = "127.0.0.1"; client = new ConsulClient(host, port); Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser(); String flowRulesJson = "[{\"resource\":\"test\", \"limitApp\":\"default\", \"grade\":1, \"count\":\"0.0\", \"strategy\":0, " + "\"refResource\":null, " + "\"controlBehavior\":0, \"warmUpPeriodSec\":10, \"maxQueueingTimeMs\":500, \"controller\":null}]"; initConsulRuleData(flowRulesJson); rules = flowConfigParser.convert(flowRulesJson); consulDataSource = new ConsulDataSource<>(host, port, ruleKey, waitTimeoutInSecond, flowConfigParser); FlowRuleManager.register2Property(consulDataSource.getProperty()); }
Example 5
Source File: StandaloneRedisDataSourceTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Before public void buildResource() { try { // Bind to a random port. server = RedisServer.newRedisServer(); server.start(); } catch (IOException e) { e.printStackTrace(); } Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser(); client = RedisClient.create(RedisURI.create(server.getHost(), server.getBindPort())); RedisConnectionConfig config = RedisConnectionConfig.builder() .withHost(server.getHost()) .withPort(server.getBindPort()) .build(); initRedisRuleData(); ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<List<FlowRule>>(config, ruleKey, channel, flowConfigParser); FlowRuleManager.register2Property(redisDataSource.getProperty()); }
Example 6
Source File: StandaloneRedisDataSourceTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Before public void buildResource() { try { // Bind to a random port. server = RedisServer.newRedisServer(); server.start(); } catch (IOException e) { e.printStackTrace(); } Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser(); client = RedisClient.create(RedisURI.create(server.getHost(), server.getBindPort())); RedisConnectionConfig config = RedisConnectionConfig.builder() .withHost(server.getHost()) .withPort(server.getBindPort()) .build(); initRedisRuleData(); ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<List<FlowRule>>(config, ruleKey, channel, flowConfigParser); FlowRuleManager.register2Property(redisDataSource.getProperty()); }
Example 7
Source File: ZookeeperDataSourceDemo.java From Sentinel with Apache License 2.0 | 6 votes |
private static void loadRules2() { final String remoteAddress = "127.0.0.1:2181"; // 引入groupId和dataId的概念,是为了方便和Nacos进行切换 final String groupId = "Sentinel-Demo"; final String flowDataId = "SYSTEM-CODE-DEMO-FLOW"; // final String degradeDataId = "SYSTEM-CODE-DEMO-DEGRADE"; // final String systemDataId = "SYSTEM-CODE-DEMO-SYSTEM"; // 规则会持久化到zk的/groupId/flowDataId节点 // groupId和和flowDataId可以用/开头也可以不用 // 建议不用以/开头,目的是为了如果从Zookeeper切换到Nacos的话,只需要改数据源类名就可以 ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); // ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId, // source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {})); // DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty()); // // ReadableDataSource<String, List<SystemRule>> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId, // source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {})); // SystemRuleManager.register2Property(systemRuleDataSource.getProperty()); }
Example 8
Source File: SentinelModeRedisDataSourceTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Before public void initData() { Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser(); RedisConnectionConfig config = RedisConnectionConfig.builder() .withRedisSentinel(host, redisSentinelPort) .withRedisSentinel(host, redisSentinelPort) .withSentinelMasterId(redisSentinelMasterId).build(); initRedisRuleData(); ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<>(config, ruleKey, channel, flowConfigParser); FlowRuleManager.register2Property(redisDataSource.getProperty()); }
Example 9
Source File: ZookeeperDataSourceTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testZooKeeperDataSource() throws Exception { TestingServer server = new TestingServer(21812); server.start(); final String remoteAddress = server.getConnectString(); final String path = "/sentinel-zk-ds-demo/flow-HK"; ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, path, new Converter<String, List<FlowRule>>() { @Override public List<FlowRule> convert(String source) { return JSON.parseObject(source, new TypeReference<List<FlowRule>>() { }); } }); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, new ExponentialBackoffRetry(3, 1000)); zkClient.start(); Stat stat = zkClient.checkExists().forPath(path); if (stat == null) { zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); } final String resourceName = "HK"; publishThenTestFor(zkClient, path, resourceName, 10); publishThenTestFor(zkClient, path, resourceName, 15); zkClient.close(); server.stop(); }
Example 10
Source File: NacosDataSourceDemo.java From Sentinel with Apache License 2.0 | 5 votes |
private static void loadMyNamespaceRules() { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress); properties.put(PropertyKeyConst.NAMESPACE, NACOS_NAMESPACE_ID); ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties, groupId, dataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { })); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }
Example 11
Source File: ClusterFlowClientController.java From sentinel-tutorial with Apache License 2.0 | 5 votes |
/** * 注册动态规则Property * 当client与Server连接中断,退化为本地限流时需要用到的该规则 */ private void registerClusterFlowRuleProperty(){ // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务 ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, APP_NAME+FLOW_POSTFIX, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); // 为集群客户端注册动态规则源 FlowRuleManager.register2Property(ds.getProperty()); }
Example 12
Source File: SpringCouldDataSourceTest.java From Sentinel with Apache License 2.0 | 5 votes |
@GetMapping("/get") @ResponseBody public List<FlowRule> get() { SpringCloudConfigDataSource dataSource = new SpringCloudConfigDataSource("flow_rule", converter); FlowRuleManager.register2Property(dataSource.getProperty()); return FlowRuleManager.getRules(); }
Example 13
Source File: NacosDataSourceDemo.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private static void loadMyNamespaceRules() { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, remoteAddress); properties.put(PropertyKeyConst.NAMESPACE, NACOS_NAMESPACE_ID); ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(properties, groupId, dataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { })); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }
Example 14
Source File: DemoClusterInitFunc.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
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 15
Source File: NacosDataSourceDemo.java From Sentinel with Apache License 2.0 | 4 votes |
private static void loadRules() { ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { })); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }
Example 16
Source File: NacosDataSourceDemo.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
private static void loadRules() { ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { })); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }
Example 17
Source File: EtcdDataSourceTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testDynamicUpdate() throws InterruptedException { String demo_key = "etcd_demo_key"; ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(demo_key, (value) -> JSON.parseArray(value, FlowRule.class)); FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty()); KV kvClient = Client.builder() .endpoints(endPoints) .build().getKVClient(); final String rule1 = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 5.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule1.getBytes())); Thread.sleep(1000); FlowRule flowRule = FlowRuleManager.getRules().get(0); Assert.assertTrue(flowRule.getResource().equals("TestResource")); Assert.assertTrue(flowRule.getCount() == 5.0); Assert.assertTrue(flowRule.getGrade() == 1); final String rule2 = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 6.0,\n" + " \"grade\": 3,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; kvClient.put(ByteSequence.from(demo_key.getBytes()), ByteSequence.from(rule2.getBytes())); Thread.sleep(1000); flowRule = FlowRuleManager.getRules().get(0); Assert.assertTrue(flowRule.getResource().equals("TestResource")); Assert.assertTrue(flowRule.getCount() == 6.0); Assert.assertTrue(flowRule.getGrade() == 3); }
Example 18
Source File: ZookeeperDataSourceTest.java From Sentinel with Apache License 2.0 | 4 votes |
@Test public void testZooKeeperDataSourceAuthorization() throws Exception { TestingServer server = new TestingServer(21812); server.start(); final String remoteAddress = server.getConnectString(); final String groupId = "sentinel-zk-ds-demo"; final String dataId = "flow-HK"; final String path = "/" + groupId + "/" + dataId; final String scheme = "digest"; final String auth = "root:123456"; AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes()); List<AuthInfo> authInfoList = Collections.singletonList(authInfo); CuratorFramework zkClient = CuratorFrameworkFactory.builder(). connectString(remoteAddress). retryPolicy(new ExponentialBackoffRetry(3, 100)). authorization(authInfoList). build(); zkClient.start(); Stat stat = zkClient.checkExists().forPath(path); if (stat == null) { ACL acl = new ACL(ZooDefs.Perms.ALL, new Id(scheme, DigestAuthenticationProvider.generateDigest(auth))); zkClient.create().creatingParentContainersIfNeeded().withACL(Collections.singletonList(acl)).forPath(path, null); } ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, authInfoList, groupId, dataId, new Converter<String, List<FlowRule>>() { @Override public List<FlowRule> convert(String source) { return JSON.parseObject(source, new TypeReference<List<FlowRule>>() { }); } }); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); final String resourceName = "HK"; publishThenTestFor(zkClient, path, resourceName, 10); publishThenTestFor(zkClient, path, resourceName, 15); zkClient.close(); server.stop(); }
Example 19
Source File: ZookeeperDataSourceDemo.java From Sentinel with Apache License 2.0 | 3 votes |
private static void loadRules() { final String remoteAddress = "127.0.0.1:2181"; final String path = "/Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW"; ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }
Example 20
Source File: ZookeeperDataSourceDemo.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 3 votes |
private static void loadRules() { final String remoteAddress = "127.0.0.1:2181"; final String path = "/Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW"; ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }