Readwrite-splitting

Background

Spring namespace read/write splitting configuration method is suitable for conventional Spring projects, determine sharding rules and properties through namespace XML configuration files, and let Spring do the creation and management of ShardingSphereDataSource objects, avoiding additional coding work.

Parameters Explained

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting/readwrite-splitting-5.2.1.xsd

<readwrite-splitting:rule />

NameTypeDescription
idAttributeSpring Bean Id
data-source-rule (+)TagReadwrite-splitting data source rule configuration

<readwrite-splitting:data-source-rule />

NameTypeDescription
idAttributeReadwrite-splitting data source rule name
static-strategyTagStatic Readwrite-splitting type
dynamic-strategyTagDynamic Readwrite-splitting type
load-balance-algorithm-refAttributeLoad balance algorithm name

<readwrite-splitting:static-strategy />

NameTypeDescription
idAttributeStatic readwrite-splitting name
write-data-source-nameAttributeWrite data source name
read-data-source-namesAttributeRead data source names, multiple data source names separated with comma
load-balance-algorithm-refAttributeLoad balance algorithm name

<readwrite-splitting:dynamic-strategy />

NameTypeDescription
idAttributeDynamic readwrite-splitting name
auto-aware-data-source-nameAttributeDatabase discovery logic data source name
write-data-source-query-enabledAttributeAll read data source are offline, write data source whether the data source is responsible for read traffic
load-balance-algorithm-refAttributeLoad balance algorithm name

<readwrite-splitting:load-balance-algorithm />

NameTypeDescription
idAttributeLoad balance algorithm name
typeAttributeLoad balance algorithm type
props (?)TagLoad balance algorithm properties

Please refer to Built-in Load Balance Algorithm List for more details about type of algorithm. 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 the load balancing algorithm.
  3. Using read/write splitting data sources.

Configuration Example

  1. <readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  2. <readwrite-splitting:rule id="readWriteSplittingRule">
  3. <readwrite-splitting:data-source-rule id="demo_ds" load-balance-algorithm-ref="randomStrategy">
  4. <readwrite-splitting:static-strategy id="staticStrategy" write-data-source-name="demo_write_ds" read-data-source-names="demo_read_ds_0, demo_read_ds_1"/>
  5. </readwrite-splitting:data-source-rule>
  6. </readwrite-splitting:rule>
  7. <shardingsphere:data-source id="readWriteSplittingDataSource" data-source-names="demo_write_ds, demo_read_ds_0, demo_read_ds_1" rule-refs="readWriteSplittingRule" />