Java API

5.0.0-beta

Sharding

Root Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
tables (+)Collection<ShardingTableRuleConfiguration>Sharding table rules-
autoTables (+)Collection<ShardingAutoTableRuleConfiguration>Sharding automatic table rules-
bindingTableGroups ()Collection<String>Binding table rulesEmpty
broadcastTables ()Collection<String>Broadcast table rulesEmpty
defaultDatabaseShardingStrategy (?)ShardingStrategyConfigurationDefault database sharding strategyNot sharding
defaultTableShardingStrategy (?)ShardingStrategyConfigurationDefault table sharding strategyNot sharding
defaultKeyGenerateStrategy (?)KeyGeneratorConfigurationDefault key generatorSnowflake
shardingAlgorithms (+)Map<String, ShardingSphereAlgorithmConfiguration>Sharding algorithm name and configurationsNone
keyGenerators (?)Map<String, ShardingSphereAlgorithmConfiguration>Key generate algorithm name and configurationsNone

Sharding Table Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
logicTableStringName of sharding logic table-
actualDataNodes (?)StringDescribe data source names and actual tables, delimiter as point.
Multiple data nodes split by comma, support inline expression
Broadcast table or databases sharding only
databaseShardingStrategy (?)ShardingStrategyConfigurationDatabases sharding strategyUse default databases sharding strategy
tableShardingStrategy (?)ShardingStrategyConfigurationTables sharding strategyUse default tables sharding strategy
keyGenerateStrategy (?)KeyGeneratorConfigurationKey generator configurationUse default key generator

Sharding Automatic Table Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
logicTableStringName of sharding logic table-
actualDataSources (?)StringData source names.
Multiple data nodes split by comma
Use all configured data sources
shardingStrategy (?)ShardingStrategyConfigurationSharding strategyUse default sharding strategy
keyGenerateStrategy (?)KeyGeneratorConfigurationKey generator configurationUse default key generator

Sharding Strategy Configuration

Standard Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingColumnStringSharding column name
shardingAlgorithmNameStringSharding algorithm name
Complex Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingColumnsStringSharding column name, separated by commas
shardingAlgorithmNameStringSharding algorithm name
Hint Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingAlgorithmNameStringSharding algorithm name
None Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration

Attributes: None

Please refer to Built-in Sharding Algorithm List for more details about type of algorithm.

Key Generate Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration

Attributes:

NameDataTypeDescription
columnStringColumn name of key generate
keyGeneratorNameStringkey generate algorithm name

Please refer to Built-in Key Generate Algorithm List for more details about type of algorithm.

Readwrite-splitting

Root Configuration

Class name: ReadwriteSplittingRuleConfiguration

Attributes:

NameDataTypeDescription
dataSources (+)Collection<ReadwriteSplittingDataSourceRuleConfiguration>Data sources of write and reads
loadBalancers (*)Map<String, ShardingSphereAlgorithmConfiguration>Load balance algorithm name and configurations of replica data sources

Readwrite-splitting Data Source Configuration

Class name: ReadwriteSplittingDataSourceRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
nameStringReadwrite-splitting data source name-
writeDataSourceNameStringWrite sources source name-
readDataSourceNames (+)Collection<String>Read sources source name list-
loadBalancerName (?)StringLoad balance algorithm name of replica sourcesRound robin load balance algorithm

Please refer to Built-in Load Balance Algorithm List for more details about type of algorithm.

Encryption

Root Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
tables (+)Collection<EncryptTableRuleConfiguration>Encrypt table rule configurations
encryptors (+)Map<String, ShardingSphereAlgorithmConfiguration>Encrypt algorithm name and configurations
queryWithCipherColumn (?)booleanWhether query with cipher column for data encrypt. User you can use plaintext to query if havetrue

Encrypt Table Rule Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration

Attributes:

NameDataTypeDescription
nameStringTable name
columns (+)Collection<EncryptColumnRuleConfiguration>Encrypt column rule configurations

Encrypt Column Rule Configuration

Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration

Attributes:

NameDataTypeDescription
logicColumnStringLogic column name
cipherColumnStringCipher column name
assistedQueryColumn (?)StringAssisted query column name
plainColumn (?)StringPlain column name
encryptorNameStringEncrypt algorithm name

Encrypt Algorithm Configuration

Class name: org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration

Attributes:

NameDataTypeDescription
nameStringEncrypt algorithm name
typeStringEncrypt algorithm type
propertiesPropertiesEncrypt algorithm properties

Please refer to Built-in Encrypt Algorithm List for more details about type of algorithm.

Shadow DB

Root Configuration

Class name: org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration

Attributes:

NameDataTypeDescription
columnStringShadow field name in SQL, SQL with a value of true will be routed to the shadow database for execution
sourceDataSourceNamesList<String>Source data source names
shadowDataSourceNamesList<String>Shadow data source names

Governance

Configuration Item Explanation

Management

Configuration Entrance

Class name: org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration

Attributes:

NameData TypeDescription
nameStringGovernance instance name
registryCenterConfigurationRegistryCenterConfigurationConfig of registry-center

The type of registryCenter could be Zookeeper or Etcd.

Governance Instance Configuration

Class name: org.apache.shardingsphere.governance.repository.api.config.ClusterPersistRepositoryConfiguration

Attributes:

NameData TypeDescription
typeStringGovernance instance type, such as: Zookeeper, etcd
serverListsStringThe list of servers that connect to governance instance, including IP and port number, use commas to separate, such as: host1:2181,host2:2181
propsPropertiesProperties for center instance config, such as options of zookeeper
overwritebooleanLocal configurations overwrite config center configurations or not; if they overwrite, each start takes reference of local configurations

ZooKeeper Properties Configuration

NameData TypeDescriptionDefault Value
digest (?)StringConnect to authority tokens in registry centerNo need for authority
operationTimeoutMilliseconds (?)intThe operation timeout milliseconds500 milliseconds
maxRetries (?)intThe maximum retry count3
retryIntervalMilliseconds (?)intThe retry interval milliseconds500 milliseconds
timeToLiveSeconds (?)intTime to live seconds for ephemeral nodes60 seconds

Etcd Properties Configuration

NameData TypeDescriptionDefault Value
timeToLiveSeconds (?)longTime to live seconds for data persist30 seconds

ShardingSphere-4.x

Sharding

ShardingDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Data sources configuration
shardingRuleConfigShardingRuleConfigurationData sharding configuration rule
props (?)PropertiesProperty configurations

ShardingRuleConfiguration

NameDataTypeExplanation
tableRuleConfigsCollectionSharding rule list
bindingTableGroups (?)CollectionBinding table rule list
broadcastTables (?)CollectionBroadcast table rule list
defaultDataSourceName (?)StringTables not configured with sharding rules will locate according to default data sources
defaultDatabaseShardingStrategyConfig (?)ShardingStrategyConfigurationDefault database sharding strategy
defaultTableShardingStrategyConfig (?)ShardingStrategyConfigurationDefault table sharding strategy
defaultKeyGeneratorConfig (?)KeyGeneratorConfigurationDefault key generator configuration, use user-defined ones or built-in ones, e.g. SNOWFLAKE/UUID. Default key generator is org.apache.shardingsphere.core.keygen.generator.impl.SnowflakeKeyGenerator
masterSlaveRuleConfigs (?)CollectionRead-write split rules, default indicates not using read-write split

TableRuleConfiguration

NameDataTypeExplanation
logicTableStringName of logic table
actualDataNodes (?)StringDescribe data source names and actual tables, delimiter as point, multiple data nodes split by comma, support inline expression. Absent means sharding databases only. Example: ds${0..7}.tbl${0..7}
databaseShardingStrategyConfig (?)ShardingStrategyConfigurationDatabases sharding strategy, use default databases sharding strategy if absent
tableShardingStrategyConfig (?)ShardingStrategyConfigurationTables sharding strategy, use default databases sharding strategy if absent
keyGeneratorConfig (?)KeyGeneratorConfigurationKey generator configuration, use default key generator if absent
encryptorConfiguration (?)EncryptorConfigurationEncrypt generator configuration

StandardShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.
NameDataTypeExplanation
shardingColumnStringSharding column name
preciseShardingAlgorithmPreciseShardingAlgorithmPrecise sharding algorithm used in = and IN
rangeShardingAlgorithm (?)RangeShardingAlgorithmRange sharding algorithm used in BETWEEN
ComplexShardingStrategyConfiguration

The implementation class of ShardingStrategyConfiguration, used in complex sharding situations with multiple sharding keys.

NameDataTypeExplanation
shardingColumnsStringSharding column name, separated by commas
shardingAlgorithmComplexKeysShardingAlgorithmComplex sharding algorithm
InlineShardingStrategyConfiguration

The implementation class of ShardingStrategyConfiguration, used in sharding strategy of inline expression.

NameDataTypeExplanation
shardingColumnsStringSharding column name, separated by commas
algorithmExpressionStringInline expression of sharding strategies, should conform to groovy syntax; refer to Inline expression for more details
HintShardingStrategyConfiguration

The implementation class of ShardingStrategyConfiguration, used to configure hint sharding strategies.

NameDataTypeDescription
shardingAlgorithmHintShardingAlgorithmHint sharding algorithm
NoneShardingStrategyConfiguration

The implementation class of ShardingStrategyConfiguration, used to configure none-sharding strategies.

KeyGeneratorConfiguration

NameDataTypeDescription
columnStringColumn name of key generator
typeStringType of key generator, use user-defined ones or built-in ones, e.g. SNOWFLAKE, UUID
propsPropertiesThe Property configuration of key generators

Properties

Property configuration that can include these properties of these key generators.

SNOWFLAKE

NameData TypeExplanation
worker.id (?)longThe unique id for working machine, the default value is 0
max.tolerate.time.difference.milliseconds (?)longThe max tolerate time for different server’s time difference in milliseconds, the default value is 10
max.vibration.offset (?)intThe max upper limit value of vibrate number, range [0, 4096), the default value is 1. Notice: To use the generated value of this algorithm as sharding value, it is recommended to configure this property. The algorithm generates key mod 2^n (2^n is usually the sharding amount of tables or databases) in different milliseconds and the result is always 0 or 1. To prevent the above sharding problem, it is recommended to configure this property, its value is (2^n)-1

Readwrite-splitting

MasterSlaveDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Mapping of data source and its name
masterSlaveRuleConfigMasterSlaveRuleConfigurationMaster slave rule configuration
props (?)PropertiesProperty configurations

MasterSlaveRuleConfiguration

NameDataTypeExplanation
nameStringReadwrite-splitting data source name
masterDataSourceNameStringMaster database source name
slaveDataSourceNamesCollectionSlave database source name list
loadBalanceAlgorithm (?)MasterSlaveLoadBalanceAlgorithmSlave database load balance

Properties

Property configuration items, can be of the following properties.

NameData TypeExplanation
sql.show (?)booleanPrint SQL parse and rewrite log or not, default value: false
executor.size (?)intBe used in work thread number implemented by SQL; no limits if it is 0. default value: 0
max.connections.size.per.query (?)intThe maximum connection number allocated by each query of each physical database, default value: 1
check.table.metadata.enabled (?)booleanCheck meta-data consistency or not in initialization, default value: false

Data Masking

EncryptDataSourceFactory

NameDataTypeExplanation
dataSourceDataSourceData source
encryptRuleConfigEncryptRuleConfigurationencrypt rule configuration
props (?)PropertiesProperty configurations

EncryptRuleConfiguration

NameDataTypeExplanation
encryptorsMap<String, EncryptorRuleConfiguration>Encryptor names and encryptors
tablesMap<String, EncryptTableRuleConfiguration>Encrypt table names and encrypt tables

Properties

Property configuration items, can be of the following properties.

NameData TypeExplanation
sql.show (?)booleanPrint SQL parse and rewrite log or not, default value: false
query.with.cipher.column (?)booleanWhen there is a plainColumn, use cipherColumn or not to query, default value: true

Orchestration

OrchestrationShardingDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Same as ShardingDataSourceFactory
shardingRuleConfigShardingRuleConfigurationSame as ShardingDataSourceFactory
props (?)PropertiesSame as ShardingDataSourceFactory
orchestrationConfigOrchestrationConfigurationOrchestration rule configurations

OrchestrationMasterSlaveDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Same as MasterSlaveDataSourceFactory
masterSlaveRuleConfigMasterSlaveRuleConfigurationSame as MasterSlaveDataSourceFactory
configMap (?)Map<String, Object>Same as MasterSlaveDataSourceFactory
props (?)PropertiesSame as ShardingDataSourceFactory
orchestrationConfigOrchestrationConfigurationOrchestration rule configurations

OrchestrationEncryptDataSourceFactory

NameDataTypeExplanation
dataSourceDataSourceSame as EncryptDataSourceFactory
encryptRuleConfigEncryptRuleConfigurationSame as EncryptDataSourceFactory
props (?)PropertiesSame as EncryptDataSourceFactory
orchestrationConfigOrchestrationConfigurationOrchestration rule configurations

OrchestrationConfiguration

NameDataTypeExplanation
instanceConfigurationMapMap<String, CenterConfiguration>config map of config-center&registry-center,the key is center’s name,the value is the config-center/registry-center

CenterConfiguration

NameDataTypeExplanation
typeStringThe type of center instance(zookeeper/etcd/apollo/nacos)
propertiesStringProperties for center instance config, such as options of zookeeper
orchestrationTypeStringThe type of orchestration center: config-center or registry-center, if both, use “setOrchestrationType(“registry_center,config_center”);”
serverListsStringConnect to server lists in center, including IP address and port number; addresses are separated by commas, such as host1:2181,host2:2181
namespace (?)StringNamespace of center instance

Properties

Property configuration items, can be of the following properties.

NameData TypeExplanation
overwritebooleanLocal configurations overwrite center configurations or not; if they overwrite, each start takes reference of local configurations

If type of center is zookeeper with config-center&registry-center, properties could be set with the follow options:

NameData TypeExplanation
digest (?)StringConnect to authority tokens in registry center; default indicates no need for authority
operationTimeoutMilliseconds (?)intThe operation timeout millisecond number, default to be 500 milliseconds
maxRetries (?)intThe maximum retry count, default to be 3 times
retryIntervalMilliseconds (?)intThe retry interval millisecond number, default to be 500 milliseconds
timeToLiveSeconds (?)intThe living time for temporary nodes, default to be 60 seconds

If type of center is etcd with config-center&registry-center, properties could be set with the follow options:

NameData TypeExplanation
timeToLiveSeconds (?)longThe etcd TTL in seconds, default to be 30 seconds

If type of center is apollo with config-center&registry-center, properties could be set with the follow options:

NameData TypeExplanation
appId (?)StringApollo appId, default to be “APOLLO_SHARDINGSPHERE”
env (?)StringApollo env, default to be “DEV”
clusterName (?)StringApollo clusterName, default to be “default”
administrator (?)StringApollo administrator, default to be “”
token (?)StringApollo token, default to be “”
portalUrl (?)StringApollo portalUrl, default to be “”
connectTimeout (?)intApollo connectTimeout, default to be 1000 milliseconds
readTimeout (?)intApollo readTimeout, default to be 5000 milliseconds

If type of center is nacos with config-center&registry-center, properties could be set with the follow options:

NameData TypeExplanation
group (?)StringNacos group, “SHARDING_SPHERE_DEFAULT_GROUP” in default
timeout (?)longNacos timeout, default to be 3000 milliseconds

ShardingSphere-3.x

Sharding

ShardingDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Data sources configuration
shardingRuleConfigShardingRuleConfigurationData sharding configuration rule
configMap (?)Map<String, Object>Config map
props (?)PropertiesProperty configurations

ShardingRuleConfiguration

NameDataTypeExplanation
tableRuleConfigsCollectionTable rule configuration
bindingTableGroups (?)CollectionBinding table groups
broadcastTables (?)CollectionBroadcast table groups
defaultDataSourceName (?)StringTables not configured with sharding rules will locate according to default data sources
defaultDatabaseShardingStrategyConfig (?)ShardingStrategyConfigurationDefault database sharding strategy
defaultTableShardingStrategyConfig (?)ShardingStrategyConfigurationDefault table sharding strategy
defaultKeyGeneratorConfig (?)KeyGeneratorDefault key generator, default value is io.shardingsphere.core.keygen.DefaultKeyGenerator
masterSlaveRuleConfigs (?)CollectionRead-write splitting rule configuration

TableRuleConfiguration

NameDataTypeExplanation
logicTableStringName of logic table
actualDataNodes (?)StringDescribe data source names and actual tables, delimiter as point, multiple data nodes split by comma, support inline expression. Absent means sharding databases only. Example: ds${0..7}.tbl${0..7}
databaseShardingStrategyConfig (?)ShardingStrategyConfigurationDatabases sharding strategy, use default databases sharding strategy if absent
tableShardingStrategyConfig (?)ShardingStrategyConfigurationTables sharding strategy, use default databases sharding strategy if absent
logicIndex (?)StringName if logic index. If use DROP INDEX XXX SQL in Oracle/PostgreSQL, This property needs to be set for finding the actual tables
keyGeneratorConfig (?)StringKey generator column name, do not use Key generator if absent
keyGenerator (?)KeyGeneratorKey generator, use default key generator if absent

StandardShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.

NameDataTypeExplanation
shardingColumnStringSharding column name
preciseShardingAlgorithmPreciseShardingAlgorithmPrecise sharding algorithm used in = and IN
rangeShardingAlgorithm (?)RangeShardingAlgorithmRange sharding algorithm used in BETWEEN
ComplexShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.

NameDataTypeExplanation
shardingColumnsStringSharding column name, separated by commas
shardingAlgorithmComplexKeysShardingAlgorithmComplex sharding algorithm
InlineShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.

NameDataTypeExplanation
shardingColumnsStringSharding column name, separated by commas
algorithmExpressionStringInline expression of sharding strategies, should conform to groovy syntax; refer to Inline expression for more details
HintShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.

NameDataTypeDescription
shardingAlgorithmHintShardingAlgorithmHint sharding algorithm
NoneShardingStrategyConfiguration

Subclass of ShardingStrategyConfiguration.

Properties

Enumeration of properties.

NameData TypeExplanation
sql.show (?)booleanPrint SQL parse and rewrite log, default value: false
executor.size (?)intThe number of SQL execution threads, zero means no limit. default value: 0
max.connections.size.per.query (?)intMax connection size for every query to every actual database. default value: 1
check.table.metadata.enabled (?)booleanCheck the metadata consistency of all the tables, default value : false

configMap

User-defined arguments.

Readwrite-splitting

MasterSlaveDataSourceFactory

NameDataTypeDescription
dataSourceMapMap<String, DataSource>Map of data sources and their names
masterSlaveRuleConfigMasterSlaveRuleConfigurationMaster slave rule configuration
configMap (?)Map<String, Object>Config map
props (?)PropertiesProperties

MasterSlaveRuleConfiguration

NameDataTypeDescription
nameStringName of master slave data source
masterDataSourceNameStringName of master data source
slaveDataSourceNamesCollectionNames of Slave data sources
loadBalanceAlgorithm (?)MasterSlaveLoadBalanceAlgorithmLoad balance algorithm

configMap

User-defined arguments.

PropertiesConstant

Enumeration of properties.

NameDataTypeDescription
sql.show (?)booleanTo show SQLS or not, default value: false
executor.size (?)intThe number of working threads, default value: CPU count
max.connections.size.per.query (?)intMax connection size for every query to every actual database. default value: 1
check.table.metadata.enabled (?)booleanCheck the metadata consistency of all the tables, default value : false

Orchestration

OrchestrationShardingDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Same as ShardingDataSourceFactory
shardingRuleConfigShardingRuleConfigurationSame as ShardingDataSourceFactory
configMap (?)Map<String, Object>Same with ShardingDataSourceFactory
props (?)PropertiesSame as ShardingDataSourceFactory
orchestrationConfigOrchestrationConfigurationOrchestration rule configurations

OrchestrationMasterSlaveDataSourceFactory

NameDataTypeExplanation
dataSourceMapMap<String, DataSource>Same as MasterSlaveDataSourceFactory
masterSlaveRuleConfigMasterSlaveRuleConfigurationSame as MasterSlaveDataSourceFactory
configMap (?)Map<String, Object>Same as MasterSlaveDataSourceFactory
props (?)PropertiesSame as ShardingDataSourceFactory
orchestrationConfigOrchestrationConfigurationOrchestration configurations

OrchestrationConfiguration

NameDataTypeExplanation
nameStringName of orchestration instance
overwritebooleanUse local configuration to overwrite registry center or not
regCenterConfigRegistryCenterConfigurationRegistry center configuration

RegistryCenterConfiguration

NameDataTypeExplanation
serverListsStringRegistry servers list, multiple split as comma. Example: host1:2181,host2:2181
namespace (?)StringNamespace of registry
digest (?)StringDigest for registry. Default is not need digest.
operationTimeoutMilliseconds (?)intOperation timeout time in milliseconds. Default value is 500 milliseconds.
maxRetries (?)intMax number of times to retry. Default value is 3
retryIntervalMilliseconds (?)intTime interval in milliseconds on each retry. Default value is 500 milliseconds.
timeToLiveSeconds (?)intTime to live in seconds of ephemeral keys. Default value is 60 seconds.

ShardingSphere-2.x

Readwrite-splitting

concept

In order to relieve the pressure on the database, the write and read operations are separated into different data sources. The write library is called the master library, and the read library is called the slave library. One master library can be configured with multiple slave libraries.

Supported

  1. Provides a readwrite-splitting configuration with one master and multiple slaves, which can be used independently or with sub-databases and sub-meters.
  2. Independent use of readwrite-splitting to support SQL transparent transmission.
  3. In the same thread and the same database connection, if there is a write operation, subsequent read operations will be read from the main library to ensure data consistency.
  4. Spring namespace.
  5. Hint-based mandatory main library routing.

Unsupported

  1. Data synchronization between the master library and the slave library.
  2. Data inconsistency caused by the data synchronization delay of the master library and the slave library.
  3. Double writing or multiple writing in the main library.

Code development example

only readwrite-splitting
  1. // Constructing a readwrite-splitting data source, the readwrite-splitting data source implements the DataSource interface, which can be directly processed as a data source. masterDataSource, slaveDataSource0, slaveDataSource1, etc. are real data sources configured using connection pools such as DBCP
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. dataSourceMap.put("masterDataSource", masterDataSource);
  4. dataSourceMap.put("slaveDataSource0", slaveDataSource0);
  5. dataSourceMap.put("slaveDataSource1", slaveDataSource1);
  6. // Constructing readwrite-splitting configuration
  7. MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
  8. masterSlaveRuleConfig.setName("ms_ds");
  9. masterSlaveRuleConfig.setMasterDataSourceName("masterDataSource");
  10. masterSlaveRuleConfig.getSlaveDataSourceNames().add("slaveDataSource0");
  11. masterSlaveRuleConfig.getSlaveDataSourceNames().add("slaveDataSource1");
  12. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig);
sharding table and database + readwrite-splitting
  1. // Constructing a readwrite-splitting data source, the readwrite-splitting data source implements the DataSource interface, which can be directly processed as a data source. masterDataSource, slaveDataSource0, slaveDataSource1, etc. are real data sources configured using connection pools such as DBCP
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. dataSourceMap.put("masterDataSource0", masterDataSource0);
  4. dataSourceMap.put("slaveDataSource00", slaveDataSource00);
  5. dataSourceMap.put("slaveDataSource01", slaveDataSource01);
  6. dataSourceMap.put("masterDataSource1", masterDataSource1);
  7. dataSourceMap.put("slaveDataSource10", slaveDataSource10);
  8. dataSourceMap.put("slaveDataSource11", slaveDataSource11);
  9. // Constructing readwrite-splitting configuration
  10. MasterSlaveRuleConfiguration masterSlaveRuleConfig0 = new MasterSlaveRuleConfiguration();
  11. masterSlaveRuleConfig0.setName("ds_0");
  12. masterSlaveRuleConfig0.setMasterDataSourceName("masterDataSource0");
  13. masterSlaveRuleConfig0.getSlaveDataSourceNames().add("slaveDataSource00");
  14. masterSlaveRuleConfig0.getSlaveDataSourceNames().add("slaveDataSource01");
  15. MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration();
  16. masterSlaveRuleConfig1.setName("ds_1");
  17. masterSlaveRuleConfig1.setMasterDataSourceName("masterDataSource1");
  18. masterSlaveRuleConfig1.getSlaveDataSourceNames().add("slaveDataSource10");
  19. masterSlaveRuleConfig1.getSlaveDataSourceNames().add("slaveDataSource11");
  20. // Continue to create ShardingDataSource through ShardingSlaveDataSourceFactory
  21. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  22. shardingRuleConfig.getMasterSlaveRuleConfigs().add(masterSlaveRuleConfig0);
  23. shardingRuleConfig.getMasterSlaveRuleConfigs().add(masterSlaveRuleConfig1);
  24. DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig);

ShardingSphere-1.x

Readwrite-splitting

concept

In order to relieve the pressure on the database, the write and read operations are separated into different data sources. The write library is called the master library, and the read library is called the slave library. One master library can be configured with multiple slave libraries.

Supported

  1. Provides a readwrite-splitting configuration with one master and multiple slaves, which can be used independently or with sub-databases and sub-meters.
  2. In the same thread and the same database connection, if there is a write operation, subsequent read operations will be read from the main library to ensure data consistency.
  3. Spring namespace.
  4. Hint-based mandatory main library routing.

Unsupported

  1. Data synchronization between the master library and the slave library.
  2. Data inconsistency caused by the data synchronization delay of the master library and the slave library.
  3. Double writing or multiple writing in the main library.

Code development example

  1. // Constructing a readwrite-splitting data source, the readwrite-splitting data source implements the DataSource interface, which can be directly processed as a data source. masterDataSource, slaveDataSource0, slaveDataSource1, etc. are real data sources configured using connection pools such as DBCP
  2. Map<String, DataSource> slaveDataSourceMap0 = new HashMap<>();
  3. slaveDataSourceMap0.put("slaveDataSource00", slaveDataSource00);
  4. slaveDataSourceMap0.put("slaveDataSource01", slaveDataSource01);
  5. // You can choose the master-slave library load balancing strategy, the default is ROUND_ROBIN, and there is RANDOM to choose from, or customize the load strategy
  6. DataSource masterSlaveDs0 = MasterSlaveDataSourceFactory.createDataSource("ms_0", "masterDataSource0", masterDataSource0, slaveDataSourceMap0, MasterSlaveLoadBalanceStrategyType.ROUND_ROBIN);
  7. Map<String, DataSource> slaveDataSourceMap1 = new HashMap<>();
  8. slaveDataSourceMap1.put("slaveDataSource10", slaveDataSource10);
  9. slaveDataSourceMap1.put("slaveDataSource11", slaveDataSource11);
  10. DataSource masterSlaveDs1 = MasterSlaveDataSourceFactory.createDataSource("ms_1", "masterDataSource1", masterDataSource1, slaveDataSourceMap1, MasterSlaveLoadBalanceStrategyType.ROUND_ROBIN);
  11. // Constructing readwrite-splitting configuration
  12. Map<String, DataSource> dataSourceMap = new HashMap<>();
  13. dataSourceMap.put("ms_0", masterSlaveDs0);
  14. dataSourceMap.put("ms_1", masterSlaveDs1);