or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

base-classes.mdcore-interfaces.mdfile-datasources.mdindex.mdspecialized-datasources.md
tile.json

tessl/maven-com-alibaba-csp--sentinel-datasource-extension

Extensible data source implementations for Sentinel flow control and circuit breaker library.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.alibaba.csp/sentinel-datasource-extension@1.8.x

To install, run

npx @tessl/cli install tessl/maven-com-alibaba-csp--sentinel-datasource-extension@1.8.0

index.mddocs/

Sentinel DataSource Extension

Sentinel 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.

Package Information

  • Package Name: sentinel-datasource-extension
  • Package Type: maven
  • Language: Java
  • Installation: Add to your Maven pom.xml:
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-extension</artifactId>
    <version>1.8.8</version>
</dependency>

Core Imports

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;

Basic Usage

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

Architecture

Sentinel DataSource Extension is built around several key components:

  • Core Interfaces: ReadableDataSource, WritableDataSource, and Converter define the contracts for data source operations
  • Abstract Base Classes: AbstractDataSource and AutoRefreshDataSource provide common functionality and auto-refresh capabilities
  • File-based Implementations: Concrete implementations for local file systems and JAR-embedded resources
  • Converter Pattern: Flexible data transformation between source formats and target types
  • Property System: Integration with Sentinel's property system for dynamic rule updates

Capabilities

Core Interfaces

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

Core Interfaces

Abstract Base Classes

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

Base Classes

File Data Sources

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

File Data Sources

Specialized Data Sources

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

Specialized Data Sources

Types

// 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 {}