Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor data source hierarchy: spilt into readable and writable data source #124

Merged
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -37,34 +38,33 @@
* @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());
}
}
18 changes: 18 additions & 0 deletions sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@
<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>

<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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +30,8 @@
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>
Expand All @@ -40,9 +40,9 @@
* 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)},
Expand All @@ -52,45 +52,55 @@
* </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 {

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();
}

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);
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);
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);
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>>() {});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.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") + "/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);
}

private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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;
Expand Down Expand Up @@ -49,7 +49,7 @@ private static void loadRules() {
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());
}
Expand Down
Loading