diff --git a/sentinel-demo/sentinel-demo-apollo-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/ApolloDataSourceDemo.java b/sentinel-demo/sentinel-demo-apollo-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/ApolloDataSourceDemo.java
index 5db3d20a..54eabffe 100644
--- a/sentinel-demo/sentinel-demo-apollo-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/ApolloDataSourceDemo.java
+++ b/sentinel-demo/sentinel-demo-apollo-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/apollo/ApolloDataSourceDemo.java
@@ -1,11 +1,12 @@
package com.alibaba.csp.sentinel.demo.datasource.apollo;
-import com.alibaba.csp.sentinel.datasource.DataSource;
+import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
+
import java.util.List;
/**
@@ -37,34 +38,33 @@ import java.util.List;
* @author Jason Song
*/
public class ApolloDataSourceDemo {
- private static final String KEY = "TestResource";
- public static void main(String[] args) {
- loadRules();
- // Assume we config: resource is `TestResource`, initial QPS threshold is 5.
- FlowQpsRunner runner = new FlowQpsRunner(KEY, 1, 100);
- runner.simulateTraffic();
- runner.tick();
- }
+ private static final String KEY = "TestResource";
+
+ public static void main(String[] args) {
+ loadRules();
+ // Assume we config: resource is `TestResource`, initial QPS threshold is 5.
+ FlowQpsRunner runner = new FlowQpsRunner(KEY, 1, 100);
+ runner.simulateTraffic();
+ runner.tick();
+ }
- private static void loadRules() {
- /**
- * Set up basic information, only for demo purpose. You may adjust them based on your actual environment.
- *
- * For more information, please refer https://github.com/ctripcorp/apollo
- */
- String appId = "sentinel-demo";
- String apolloMetaServerAddress = "http://localhost:8080";
- System.setProperty("app.id", appId);
- System.setProperty("apollo.meta", apolloMetaServerAddress);
+ private static void loadRules() {
+ // Set up basic information, only for demo purpose. You may adjust them based on your actual environment.
+ // For more information, please refer https://github.com/ctripcorp/apollo
+ String appId = "sentinel-demo";
+ String apolloMetaServerAddress = "http://localhost:8080";
+ System.setProperty("app.id", appId);
+ System.setProperty("apollo.meta", apolloMetaServerAddress);
- String namespaceName = "application";
- String flowRuleKey = "flowRules";
- String defaultFlowRules = "[]"; //it's better to provide a meaningful default value
+ String namespaceName = "application";
+ String flowRuleKey = "flowRules";
+ // It's better to provide a meaningful default value.
+ String defaultFlowRules = "[]";
- DataSource> flowRuleDataSource = new ApolloDataSource<>(namespaceName, flowRuleKey,
- defaultFlowRules, source -> JSON.parseObject(source, new TypeReference>() {
- }));
- FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
- }
+ ReadableDataSource> flowRuleDataSource = new ApolloDataSource<>(namespaceName,
+ flowRuleKey, defaultFlowRules, source -> JSON.parseObject(source, new TypeReference>() {
+ }));
+ FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
+ }
}
diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml b/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml
index 8ff5e0cc..24491ba1 100755
--- a/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml
+++ b/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml
@@ -26,4 +26,18 @@
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven.compiler.version}
+
+ 1.8
+ 1.8
+ ${java.encoding}
+
+
+
+
\ No newline at end of file
diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/FileDataSourceDemo.java b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/FileDataSourceDemo.java
index 3bd632ab..0dda2a2a 100644
--- a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/FileDataSourceDemo.java
+++ b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/FileDataSourceDemo.java
@@ -15,14 +15,12 @@
*/
package com.alibaba.csp.sentinel.demo.file.rule;
+import java.net.URLDecoder;
import java.util.List;
-import com.alibaba.csp.sentinel.datasource.ConfigParser;
-import com.alibaba.csp.sentinel.datasource.DataSource;
+import com.alibaba.csp.sentinel.datasource.Converter;
+import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
-import com.alibaba.csp.sentinel.demo.file.rule.parser.JsonDegradeRuleListParser;
-import com.alibaba.csp.sentinel.demo.file.rule.parser.JsonFlowRuleListParser;
-import com.alibaba.csp.sentinel.demo.file.rule.parser.JsonSystemRuleListParser;
import com.alibaba.csp.sentinel.property.PropertyListener;
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slots.block.Rule;
@@ -32,6 +30,8 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.TypeReference;
/**
*
@@ -40,9 +40,9 @@ import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
* inform the listener if the file is updated.
*
*
- * Each {@link DataSource} has a {@link SentinelProperty} to hold the deserialized config data.
+ * Each {@link ReadableDataSource} has a {@link SentinelProperty} to hold the deserialized config data.
* {@link PropertyListener} will listen to the {@link SentinelProperty} instead of the datasource.
- * {@link ConfigParser} is used for telling how to deserialize the data.
+ * {@link Converter} is used for telling how to deserialize the data.
*
* For other kinds of data source, such as Nacos,
- * Zookeeper, Git, or even CSV file, We could implement {@link DataSource} interface to read these
+ * Zookeeper, Git, or even CSV file, We could implement {@link ReadableDataSource} interface to read these
* configs.
*
*
* @author Carpenter Lee
+ * @author Eric Zhao
*/
public class FileDataSourceDemo {
@@ -64,7 +65,7 @@ public class FileDataSourceDemo {
FileDataSourceDemo demo = new FileDataSourceDemo();
demo.listenRules();
- /**
+ /*
* Start to require tokens, rate will be limited by rule in FlowRule.json
*/
FlowQpsRunner runner = new FlowQpsRunner();
@@ -72,25 +73,38 @@ public class FileDataSourceDemo {
runner.tick();
}
- public void listenRules() throws Exception {
+ private void listenRules() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
- String flowRulePath = classLoader.getResource("FlowRule.json").getFile();
- String degradeRulePath = classLoader.getResource("DegradeRule.json").getFile();
- String systemRulePath = classLoader.getResource("SystemRule.json").getFile();
+ 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
- DataSource> flowRuleDataSource = new FileRefreshableDataSource>(
- flowRulePath, new JsonFlowRuleListParser());
+ // Data source for FlowRule
+ FileRefreshableDataSource> flowRuleDataSource = new FileRefreshableDataSource<>(
+ flowRulePath, flowRuleListParser, this::encodeJson);
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
- // data source for DegradeRule
- DataSource> degradeRuleDataSource = new FileRefreshableDataSource>(
- degradeRulePath, new JsonDegradeRuleListParser());
+ // Data source for DegradeRule
+ FileRefreshableDataSource> degradeRuleDataSource
+ = new FileRefreshableDataSource<>(
+ degradeRulePath, degradeRuleListParser, this::encodeJson);
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
- // data source for SystemRule
- DataSource> systemRuleDataSource = new FileRefreshableDataSource>(
- systemRulePath, new JsonSystemRuleListParser());
+ // Data source for SystemRule
+ FileRefreshableDataSource> systemRuleDataSource
+ = new FileRefreshableDataSource<>(
+ systemRulePath, systemRuleListParser, this::encodeJson);
SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
+
+ private Converter> flowRuleListParser = source -> JSON.parseObject(source,
+ new TypeReference>() {});
+ private Converter> degradeRuleListParser = source -> JSON.parseObject(source,
+ new TypeReference>() {});
+ private Converter> systemRuleListParser = source -> JSON.parseObject(source,
+ new TypeReference>() {});
+
+ private String encodeJson(T t) {
+ return JSON.toJSONString(t);
+ }
}
diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonDegradeRuleListParser.java b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonDegradeRuleListParser.java
deleted file mode 100644
index bb0c4d50..00000000
--- a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonDegradeRuleListParser.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.alibaba.csp.sentinel.demo.file.rule.parser;
-
-import java.util.List;
-
-import com.alibaba.csp.sentinel.datasource.ConfigParser;
-import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.TypeReference;
-
-/**
- * A {@link ConfigParser} parses Json String to {@code List}.
- *
- * @author Carpenter Lee
- */
-public class JsonDegradeRuleListParser implements ConfigParser> {
- @Override
- public List parse(String source) {
- return JSON.parseObject(source, new TypeReference>() {});
- }
-}
diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonFlowRuleListParser.java b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonFlowRuleListParser.java
deleted file mode 100644
index 044d440d..00000000
--- a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonFlowRuleListParser.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.alibaba.csp.sentinel.demo.file.rule.parser;
-
-import java.util.List;
-
-import com.alibaba.csp.sentinel.datasource.ConfigParser;
-import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.TypeReference;
-
-/**
- * A {@link ConfigParser} parses Json String to {@code List}.
- *
- * @author Carpenter Lee
- */
-public class JsonFlowRuleListParser implements ConfigParser> {
- @Override
- public List parse(String source) {
- return JSON.parseObject(source, new TypeReference>() {});
- }
-}
diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonSystemRuleListParser.java b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonSystemRuleListParser.java
deleted file mode 100644
index 59ffeb0f..00000000
--- a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/parser/JsonSystemRuleListParser.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.alibaba.csp.sentinel.demo.file.rule.parser;
-
-import java.util.List;
-
-import com.alibaba.csp.sentinel.datasource.ConfigParser;
-import com.alibaba.csp.sentinel.slots.system.SystemRule;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.TypeReference;
-
-/**
- * A {@link ConfigParser} parses Json String to {@code List}.
- *
- * @author Carpenter Lee
- */
-public class JsonSystemRuleListParser implements ConfigParser> {
- @Override
- public List parse(String source) {
- return JSON.parseObject(source, new TypeReference>() {});
- }
-}
diff --git a/sentinel-demo/sentinel-demo-nacos-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/nacos/NacosDataSourceDemo.java b/sentinel-demo/sentinel-demo-nacos-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/nacos/NacosDataSourceDemo.java
index dcaf1a95..51856354 100644
--- a/sentinel-demo/sentinel-demo-nacos-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/nacos/NacosDataSourceDemo.java
+++ b/sentinel-demo/sentinel-demo-nacos-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/nacos/NacosDataSourceDemo.java
@@ -17,7 +17,7 @@ package com.alibaba.csp.sentinel.demo.datasource.nacos;
import java.util.List;
-import com.alibaba.csp.sentinel.datasource.DataSource;
+import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
@@ -49,7 +49,7 @@ public class NacosDataSourceDemo {
final String groupId = "Sentinel:Demo";
final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";
- DataSource> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
+ ReadableDataSource> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
source -> JSON.parseObject(source, new TypeReference>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
diff --git a/sentinel-demo/sentinel-demo-zookeeper-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperDataSourceDemo.java b/sentinel-demo/sentinel-demo-zookeeper-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperDataSourceDemo.java
index 2077b7d8..52bfe903 100644
--- a/sentinel-demo/sentinel-demo-zookeeper-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperDataSourceDemo.java
+++ b/sentinel-demo/sentinel-demo-zookeeper-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperDataSourceDemo.java
@@ -2,7 +2,7 @@ package com.alibaba.csp.sentinel.demo.datasource.zookeeper;
import java.util.List;
-import com.alibaba.csp.sentinel.datasource.DataSource;
+import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
@@ -10,7 +10,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
/**
- * Zookeeper DataSource Demo
+ * Zookeeper ReadableDataSource Demo
*
* @author guonanjun
*/
@@ -29,7 +29,7 @@ public class ZookeeperDataSourceDemo {
final String remoteAddress = "127.0.0.1:2181";
final String path = "/Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW";
- DataSource> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path,
+ ReadableDataSource> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path,
source -> JSON.parseObject(source, new TypeReference>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
@@ -49,15 +49,15 @@ public class ZookeeperDataSourceDemo {
// 规则会持久化到zk的/groupId/flowDataId节点
// groupId和和flowDataId可以用/开头也可以不用
// 建议不用以/开头,目的是为了如果从Zookeeper切换到Nacos的话,只需要改数据源类名就可以
- DataSource> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId,
+ ReadableDataSource> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId,
source -> JSON.parseObject(source, new TypeReference>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
- // DataSource> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId,
+ // ReadableDataSource> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId,
// source -> JSON.parseObject(source, new TypeReference>() {}));
// DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
//
- // DataSource> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId,
+ // ReadableDataSource> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId,
// source -> JSON.parseObject(source, new TypeReference>() {}));
// SystemRuleManager.register2Property(systemRuleDataSource.getProperty());