init
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-demo</artifactId>
|
||||
<version>1.8.3</version>
|
||||
</parent>
|
||||
<artifactId>sentinel-demo-dynamic-file-rule</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-extension</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
|
||||
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.property.PropertyListener;
|
||||
import com.alibaba.csp.sentinel.property.SentinelProperty;
|
||||
import com.alibaba.csp.sentinel.slots.block.Rule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
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>
|
||||
* This Demo shows how to use {@link FileRefreshableDataSource} to read {@link Rule}s from file. The
|
||||
* {@link FileRefreshableDataSource} will automatically fetches the backend file every 3 seconds, and
|
||||
* inform the listener if the file is updated.
|
||||
* </p>
|
||||
* <p>
|
||||
* 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 Converter} is used for telling how to deserialize the data.
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link FlowRuleManager#register2Property(SentinelProperty)},
|
||||
* {@link DegradeRuleManager#register2Property(SentinelProperty)},
|
||||
* {@link SystemRuleManager#register2Property(SentinelProperty)} could be called for listening the
|
||||
* {@link Rule}s change.
|
||||
* </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 ReadableDataSource} interface to read these
|
||||
* configs.
|
||||
* </p>
|
||||
*
|
||||
* @author Carpenter Lee
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class FileDataSourceDemo {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
FileDataSourceDemo demo = new FileDataSourceDemo();
|
||||
demo.listenRules();
|
||||
|
||||
/*
|
||||
* Start to require tokens, rate will be limited by rule in FlowRule.json
|
||||
*/
|
||||
FlowQpsRunner runner = new FlowQpsRunner();
|
||||
runner.simulateTraffic();
|
||||
runner.tick();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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>>() {});
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
|
||||
import com.alibaba.csp.sentinel.init.InitFunc;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A sample showing how to register readable and writable data source via Sentinel init SPI mechanism.
|
||||
* </p>
|
||||
* <p>
|
||||
* To activate this, you can add the class name to `com.alibaba.csp.sentinel.init.InitFunc` file
|
||||
* in `META-INF/services/` directory of the resource directory. Then the data source will be automatically
|
||||
* registered during the initialization of Sentinel.
|
||||
* </p>
|
||||
*
|
||||
* @author Eric Zhao
|
||||
*/
|
||||
public class FileDataSourceInit implements InitFunc {
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
// A fake path.
|
||||
String flowRuleDir = System.getProperty("user.home") + File.separator + "sentinel" + File.separator + "rules";
|
||||
String flowRuleFile = "flowRule.json";
|
||||
String flowRulePath = flowRuleDir + File.separator + 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);
|
||||
}
|
||||
|
||||
private <T> String encodeJson(T t) {
|
||||
return JSON.toJSONString(t);
|
||||
}
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.alibaba.csp.sentinel.Entry;
|
||||
import com.alibaba.csp.sentinel.SphU;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
/**
|
||||
* Flow Rule demo.
|
||||
*
|
||||
* @author Carpenter Lee
|
||||
*/
|
||||
class FlowQpsRunner {
|
||||
private static final String KEY = "abc";
|
||||
|
||||
private static AtomicInteger pass = new AtomicInteger();
|
||||
private static AtomicInteger block = new AtomicInteger();
|
||||
private static AtomicInteger total = new AtomicInteger();
|
||||
|
||||
private static volatile boolean stop = false;
|
||||
|
||||
private static final int threadCount = 1;
|
||||
private static int seconds = 60 + 40;
|
||||
|
||||
public void simulateTraffic() {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
Thread t = new Thread(new RunTask());
|
||||
t.setName("simulate-traffic-Task");
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
Thread timer = new Thread(new TimerTask());
|
||||
timer.setName("sentinel-timer-task");
|
||||
timer.start();
|
||||
}
|
||||
|
||||
static final class RunTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stop) {
|
||||
Entry entry = null;
|
||||
|
||||
try {
|
||||
entry = SphU.entry(KEY);
|
||||
// token acquired, means pass
|
||||
pass.addAndGet(1);
|
||||
} catch (BlockException e1) {
|
||||
block.incrementAndGet();
|
||||
} catch (Exception e2) {
|
||||
// biz exception
|
||||
} finally {
|
||||
total.incrementAndGet();
|
||||
if (entry != null) {
|
||||
entry.exit();
|
||||
}
|
||||
}
|
||||
|
||||
Random random2 = new Random();
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(random2.nextInt(50));
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final class TimerTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
long start = System.currentTimeMillis();
|
||||
System.out.println("begin to statistic!!!");
|
||||
|
||||
long oldTotal = 0;
|
||||
long oldPass = 0;
|
||||
long oldBlock = 0;
|
||||
while (!stop) {
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
long globalTotal = total.get();
|
||||
long oneSecondTotal = globalTotal - oldTotal;
|
||||
oldTotal = globalTotal;
|
||||
|
||||
long globalPass = pass.get();
|
||||
long oneSecondPass = globalPass - oldPass;
|
||||
oldPass = globalPass;
|
||||
|
||||
long globalBlock = block.get();
|
||||
long oneSecondBlock = globalBlock - oldBlock;
|
||||
oldBlock = globalBlock;
|
||||
|
||||
System.out.println(seconds + " send qps is: " + oneSecondTotal);
|
||||
System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal
|
||||
+ ", pass:" + oneSecondPass
|
||||
+ ", block:" + oneSecondBlock);
|
||||
|
||||
if (seconds-- <= 0) {
|
||||
stop = true;
|
||||
}
|
||||
}
|
||||
|
||||
long cost = System.currentTimeMillis() - start;
|
||||
System.out.println("time cost: " + cost + " ms");
|
||||
System.out.println("total:" + total.get() + ", pass:" + pass.get()
|
||||
+ ", block:" + block.get());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package com.alibaba.csp.sentinel.demo.file.rule;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.datasource.FileInJarReadableDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||
import com.alibaba.csp.sentinel.property.PropertyListener;
|
||||
import com.alibaba.csp.sentinel.property.SentinelProperty;
|
||||
import com.alibaba.csp.sentinel.slots.block.Rule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
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.SystemRuleManager;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This Demo shows how to use {@link FileInJarReadableDataSource} to read {@link Rule}s from jarfile. The
|
||||
* {@link FileInJarReadableDataSource} will automatically fetches the backend file every 3 seconds, and
|
||||
* inform the listener if the file is updated.
|
||||
* </p>
|
||||
* <p>
|
||||
* 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 Converter} is used for telling how to deserialize the data.
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link FlowRuleManager#register2Property(SentinelProperty)},
|
||||
* {@link DegradeRuleManager#register2Property(SentinelProperty)},
|
||||
* {@link SystemRuleManager#register2Property(SentinelProperty)} could be called for listening the
|
||||
* {@link Rule}s change.
|
||||
* </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 ReadableDataSource} interface to read these
|
||||
* configs.
|
||||
* </p>
|
||||
*
|
||||
* @author dingq
|
||||
*/
|
||||
public class JarFileDataSourceDemo {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
JarFileDataSourceDemo demo = new JarFileDataSourceDemo();
|
||||
demo.listenRules();
|
||||
|
||||
// Start to require tokens, rate will be limited by rule of FlowRule.json in jar.
|
||||
FlowQpsRunner runner = new FlowQpsRunner();
|
||||
runner.simulateTraffic();
|
||||
runner.tick();
|
||||
}
|
||||
|
||||
private void listenRules() throws Exception {
|
||||
// Modify the path with your real path.
|
||||
String jarPath = System.getProperty("user.dir") + "/sentinel-demo/sentinel-demo-dynamic-file-rule/target/"
|
||||
+ "sentinel-demo-dynamic-file-rule.jar";
|
||||
// eg: if flowRuleInJarName full path is 'sentinel-demo-dynamic-file-rule.jar!/classes/FlowRule.json',
|
||||
// your flowRuleInJarName is 'classes/FlowRule.json'
|
||||
String flowRuleInJarPath = "FlowRule.json";
|
||||
|
||||
FileInJarReadableDataSource<List<FlowRule>> flowRuleDataSource = new FileInJarReadableDataSource<>(
|
||||
jarPath,flowRuleInJarPath, flowRuleListParser);
|
||||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
|
||||
}
|
||||
|
||||
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source,
|
||||
new TypeReference<List<FlowRule>>() {});
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
[
|
||||
{
|
||||
"resource": "abc0",
|
||||
"count": 20.0,
|
||||
"grade": 0,
|
||||
"passCount": 0,
|
||||
"timeWindow": 10
|
||||
},
|
||||
{
|
||||
"resource": "abc1",
|
||||
"count": 15.0,
|
||||
"grade": 0,
|
||||
"passCount": 0,
|
||||
"timeWindow": 10
|
||||
}
|
||||
]
|
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"resource": "abc",
|
||||
"controlBehavior": 0,
|
||||
"count": 20.0,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
},
|
||||
{
|
||||
"resource": "abc1",
|
||||
"controlBehavior": 0,
|
||||
"count": 20.0,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
]
|
@@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"avgRt": 10,
|
||||
"highestSystemLoad": 5.0,
|
||||
"maxThread": 10,
|
||||
"qps": 20.0
|
||||
}
|
||||
]
|
Reference in New Issue
Block a user