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:
Name | DataType | Description |
---|---|---|
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:
Name | DataType | Description | Default Value |
---|---|---|---|
name | String | Readwrite-splitting data source name | - |
writeDataSourceName | String | Write data source name | - |
readDataSourceNames | List<String> | Read data sources list | - |
transactionalReadQueryStrategy (?) | TransactionalReadQueryStrategy | Routing strategy for read query within a transaction, values include: PRIMARY (to primary), FIXED (to fixed data source), DYNAMIC (to any data source) | DYNAMIC |
loadBalancerName (?) | String | Load balance algorithm name of replica sources | Round robin load balance algorithm |
Please refer to Built-in Load Balance Algorithm List for details on algorithm types.
Operating Procedures
- Add read-write splitting data source
- Set load balancing algorithms
- Use read-write splitting data source
Configuration Examples
public DataSource getDataSource() throws SQLException {
ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfig = new ReadwriteSplittingDataSourceRuleConfiguration(
"demo_read_query_ds", "demo_write_ds", Arrays.asList("demo_read_ds_0", "demo_read_ds_1"), "demo_weight_lb");
Properties algorithmProps = new Properties();
algorithmProps.setProperty("demo_read_ds_0", "2");
algorithmProps.setProperty("demo_read_ds_1", "1");
Map<String, AlgorithmConfiguration> algorithmConfigMap = new HashMap<>(1);
algorithmConfigMap.put("demo_weight_lb", new AlgorithmConfiguration("WEIGHT", algorithmProps));
ReadwriteSplittingRuleConfiguration ruleConfig = new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceConfig), algorithmConfigMap);
Properties props = new Properties();
props.setProperty("sql-show", Boolean.TRUE.toString());
return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(ruleConfig), props);
}
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new HashMap<>(3, 1);
result.put("demo_write_ds", DataSourceUtil.createDataSource("demo_write_ds"));
result.put("demo_read_ds_0", DataSourceUtil.createDataSource("demo_read_ds_0"));
result.put("demo_read_ds_1", DataSourceUtil.createDataSource("demo_read_ds_1"));
return result;
}
References
当前内容版权归 ShardingSphere 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ShardingSphere .