Signed-off-by: Eric Zhao <sczyh16@gmail.com>master
@@ -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. | |||
* <br /> | |||
* 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<String, List<FlowRule>> flowRuleDataSource = new ApolloDataSource<>(namespaceName, flowRuleKey, | |||
defaultFlowRules, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { | |||
})); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
} | |||
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ApolloDataSource<>(namespaceName, | |||
flowRuleKey, defaultFlowRules, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() { | |||
})); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
} | |||
} |
@@ -26,4 +26,18 @@ | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-compiler-plugin</artifactId> | |||
<version>${maven.compiler.version}</version> | |||
<configuration> | |||
<source>1.8</source> | |||
<target>1.8</target> | |||
<encoding>${java.encoding}</encoding> | |||
</configuration> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> |
@@ -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; | |||
/** | |||
* <p> | |||
@@ -40,9 +40,9 @@ import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; | |||
* inform the listener if the file is updated. | |||
* </p> | |||
* <p> | |||
* 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. | |||
* </p> | |||
* <p> | |||
* {@link FlowRuleManager#register2Property(SentinelProperty)}, | |||
@@ -52,11 +52,12 @@ import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; | |||
* </p> | |||
* <p> | |||
* For other kinds of data source, such as <a href="https://github.com/alibaba/nacos">Nacos</a>, | |||
* 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. | |||
* </p> | |||
* | |||
* @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<String, List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<List<FlowRule>>( | |||
flowRulePath, new JsonFlowRuleListParser()); | |||
// Data source for FlowRule | |||
FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>( | |||
flowRulePath, flowRuleListParser, this::encodeJson); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
// data source for DegradeRule | |||
DataSource<String, List<DegradeRule>> degradeRuleDataSource = new FileRefreshableDataSource<List<DegradeRule>>( | |||
degradeRulePath, new JsonDegradeRuleListParser()); | |||
// Data source for DegradeRule | |||
FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource | |||
= new FileRefreshableDataSource<>( | |||
degradeRulePath, degradeRuleListParser, this::encodeJson); | |||
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty()); | |||
// data source for SystemRule | |||
DataSource<String, List<SystemRule>> systemRuleDataSource = new FileRefreshableDataSource<List<SystemRule>>( | |||
systemRulePath, new JsonSystemRuleListParser()); | |||
// Data source for SystemRule | |||
FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource | |||
= new FileRefreshableDataSource<>( | |||
systemRulePath, systemRuleListParser, this::encodeJson); | |||
SystemRuleManager.register2Property(systemRuleDataSource.getProperty()); | |||
} | |||
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source, | |||
new TypeReference<List<FlowRule>>() {}); | |||
private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(source, | |||
new TypeReference<List<DegradeRule>>() {}); | |||
private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(source, | |||
new TypeReference<List<SystemRule>>() {}); | |||
private <T> String encodeJson(T t) { | |||
return JSON.toJSONString(t); | |||
} | |||
} |
@@ -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<DegradeRule>}. | |||
* | |||
* @author Carpenter Lee | |||
*/ | |||
public class JsonDegradeRuleListParser implements ConfigParser<String, List<DegradeRule>> { | |||
@Override | |||
public List<DegradeRule> parse(String source) { | |||
return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {}); | |||
} | |||
} |
@@ -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<FlowRule>}. | |||
* | |||
* @author Carpenter Lee | |||
*/ | |||
public class JsonFlowRuleListParser implements ConfigParser<String, List<FlowRule>> { | |||
@Override | |||
public List<FlowRule> parse(String source) { | |||
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}); | |||
} | |||
} |
@@ -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<SystemRule>}. | |||
* | |||
* @author Carpenter Lee | |||
*/ | |||
public class JsonSystemRuleListParser implements ConfigParser<String, List<SystemRule>> { | |||
@Override | |||
public List<SystemRule> parse(String source) { | |||
return JSON.parseObject(source, new TypeReference<List<SystemRule>>() {}); | |||
} | |||
} |
@@ -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<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, | |||
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, | |||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
} | |||
@@ -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<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, | |||
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, | |||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
@@ -49,15 +49,15 @@ public class ZookeeperDataSourceDemo { | |||
// 规则会持久化到zk的/groupId/flowDataId节点 | |||
// groupId和和flowDataId可以用/开头也可以不用 | |||
// 建议不用以/开头,目的是为了如果从Zookeeper切换到Nacos的话,只需要改数据源类名就可以 | |||
DataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId, | |||
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId, | |||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); | |||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | |||
// DataSource<String, List<DegradeRule>> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId, | |||
// ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId, | |||
// source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {})); | |||
// DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty()); | |||
// | |||
// DataSource<String, List<SystemRule>> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId, | |||
// ReadableDataSource<String, List<SystemRule>> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId, | |||
// source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {})); | |||
// SystemRuleManager.register2Property(systemRuleDataSource.getProperty()); | |||