Readwrite-splitting

Background

The read/write splitting configured in Java API form can be easily applied to various scenarios without relying on additional jar packages. Users only need to construct the read/write splitting data source through java code to be able to use the read/write splitting function.

Parameters Explained

Entry

Class name: org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration

Configurable Properties:

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

Primary-secondary Data Source Configuration

Class name: org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration

Configurable Properties:

NameDataTypeDescriptionDefault Value
nameStringReadwrite-splitting data source name-
staticStrategyStringStatic Readwrite-splitting configuration-
dynamicStrategyPropertiesDynamic Readwrite-splitting configuration-
loadBalancerName (?)StringLoad balance algorithm name of replica sourcesRound robin load balance algorithm

Class name:org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration

Configurable Properties:

NameDataTypeDescription
writeDataSourceNameStringWrite data source name
readDataSourceNamesList<String>Read data sources list

Class name:org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration

Configurable Properties:

NameDataTypeDescriptionDefault Value
autoAwareDataSourceNameStringDatabase discovery logic data source name-
writeDataSourceQueryEnabled (?)StringAll read data source are offline, write data source whether the data source is responsible for read traffictrue

Please refer to Built-in Load Balance Algorithm List for details on algorithm types. Please refer to Read-write splitting-Core features for more details about query consistent routing.

Operating Procedures

  1. Add read-write splitting data source
  2. Set load balancing algorithms
  3. Use read-write splitting data source

Configuration Examples

  1. public DataSource getDataSource() throws SQLException {
  2. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfig = new ReadwriteSplittingDataSourceRuleConfiguration(
  3. "demo_read_query_ds", new StaticReadwriteSplittingStrategyConfiguration("demo_write_ds",
  4. Arrays.asList("demo_read_ds_0", "demo_read_ds_1")), null,"demo_weight_lb");
  5. Properties algorithmProps = new Properties();
  6. algorithmProps.setProperty("demo_read_ds_0", "2");
  7. algorithmProps.setProperty("demo_read_ds_1", "1");
  8. Map<String, AlgorithmConfiguration> algorithmConfigMap = new HashMap<>(1);
  9. algorithmConfigMap.put("demo_weight_lb", new AlgorithmConfiguration("WEIGHT", algorithmProps));
  10. ReadwriteSplittingRuleConfiguration ruleConfig = new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceConfig), algorithmConfigMap);
  11. Properties props = new Properties();
  12. props.setProperty("sql-show", Boolean.TRUE.toString());
  13. return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(ruleConfig), props);
  14. }
  15. private Map<String, DataSource> createDataSourceMap() {
  16. Map<String, DataSource> result = new HashMap<>(3, 1);
  17. result.put("demo_write_ds", DataSourceUtil.createDataSource("demo_write_ds"));
  18. result.put("demo_read_ds_0", DataSourceUtil.createDataSource("demo_read_ds_0"));
  19. result.put("demo_read_ds_1", DataSourceUtil.createDataSource("demo_read_ds_1"));
  20. return result;
  21. }

References