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-
writeDataSourceNameStringWrite data source name-
readDataSourceNamesList<String>Read data sources list-
transactionalReadQueryStrategy (?)TransactionalReadQueryStrategyRouting strategy for read query within a transaction, values include: PRIMARY (to primary), FIXED (to fixed data source), DYNAMIC (to any data source)DYNAMIC
loadBalancerName (?)StringLoad balance algorithm name of replica sourcesRound robin load balance algorithm

Please refer to Built-in Load Balance Algorithm List for details on algorithm types.

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

References