Extensible data source implementations for Sentinel flow control and circuit breaker library.
npx @tessl/cli install tessl/maven-com-alibaba-csp--sentinel-datasource-extension@1.8.0Sentinel DataSource Extension provides extensible data source implementations for Sentinel's flow control and circuit breaker library. It enables dynamic configuration management by allowing integration with various external configuration sources like files, databases, or configuration centers, supporting both read and write operations with automatic refresh mechanisms for real-time rule updates.
pom.xml:<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
<version>1.8.8</version>
</dependency>import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource;import com.alibaba.csp.sentinel.datasource.*;
import java.io.File;
// Create a converter for rule parsing
Converter<String, List<FlowRule>> converter = source -> {
// Parse JSON/XML configuration to FlowRule objects
return JSON.parseArray(source, FlowRule.class);
};
// File-based readable data source with auto-refresh
ReadableDataSource<String, List<FlowRule>> readableDs =
new FileRefreshableDataSource<>(
new File("/path/to/rules.json"),
converter
);
// Get the current rules
List<FlowRule> rules = readableDs.loadConfig();
// Access property for dynamic updates
SentinelProperty<List<FlowRule>> property = readableDs.getProperty();
property.addListener(rules -> {
// Update flow rules when file changes
FlowRuleManager.loadRules(rules);
});
// File-based writable data source
Converter<List<FlowRule>, String> encoder = rules -> JSON.toJSONString(rules);
WritableDataSource<List<FlowRule>> writableDs =
new FileWritableDataSource<>("/path/to/rules.json", encoder);
// Write updated rules
writableDs.write(updatedRules);Sentinel DataSource Extension is built around several key components:
ReadableDataSource, WritableDataSource, and Converter define the contracts for data source operationsAbstractDataSource and AutoRefreshDataSource provide common functionality and auto-refresh capabilitiesFundamental interfaces that define the contracts for readable and writable data sources, plus the converter pattern for data transformation.
public interface ReadableDataSource<S, T> {
T loadConfig() throws Exception;
S readSource() throws Exception;
SentinelProperty<T> getProperty();
void close() throws Exception;
}
public interface WritableDataSource<T> {
void write(T value) throws Exception;
void close() throws Exception;
}
public interface Converter<S, T> {
T convert(S source);
}Base classes providing common data source functionality and automatic refresh capabilities for building custom data source implementations.
public abstract class AbstractDataSource<S, T> implements ReadableDataSource<S, T> {
public AbstractDataSource(Converter<S, T> parser);
public T loadConfig() throws Exception;
public T loadConfig(S conf) throws Exception;
public SentinelProperty<T> getProperty();
}
public abstract class AutoRefreshDataSource<S, T> extends AbstractDataSource<S, T> {
public AutoRefreshDataSource(Converter<S, T> configParser);
public AutoRefreshDataSource(Converter<S, T> configParser, long recommendRefreshMs);
public void close() throws Exception;
protected boolean isModified();
}Implementations for file-based data sources supporting local files, JAR-embedded resources, and automatic refresh mechanisms.
public class FileRefreshableDataSource<T> extends AutoRefreshDataSource<String, T> {
public FileRefreshableDataSource(File file, Converter<String, T> configParser) throws FileNotFoundException;
public FileRefreshableDataSource(String fileName, Converter<String, T> configParser) throws FileNotFoundException;
// Additional constructors with buffer size and charset options
}
public class FileWritableDataSource<T> implements WritableDataSource<T> {
public FileWritableDataSource(String filePath, Converter<T, String> configEncoder);
public FileWritableDataSource(File file, Converter<T, String> configEncoder);
public void write(T value) throws Exception;
}Additional data source implementations including JAR-embedded file support and empty data sources for default configurations.
public class FileInJarReadableDataSource<T> extends AbstractDataSource<String, T> {
public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter<String, T> configParser) throws IOException;
public String readSource() throws Exception;
}
public final class EmptyDataSource implements ReadableDataSource<Object, Object> {
public static final ReadableDataSource<Object, Object> EMPTY_DATASOURCE;
public Object loadConfig() throws Exception;
public SentinelProperty<Object> getProperty();
}// External dependencies from sentinel-core
interface SentinelProperty<T> {
void addListener(PropertyListener<T> listener);
void updateValue(T newValue);
T getValue();
}
interface PropertyListener<T> {
void configUpdate(T value);
void configLoad(T value);
}
// Common exceptions
class FileNotFoundException extends Exception {}
class IOException extends Exception {}
class IllegalArgumentException extends RuntimeException {}
class IllegalStateException extends RuntimeException {}