Change History

5.0.0-alpha

Replica Query

Configuration Item Explanation

  1. dataSource: # Omit the data source configuration, please refer to the usage
  2. rules:
  3. - !REPLICA_QUERY
  4. dataSources:
  5. <data-source-name> (+): # Logic data source name of replica query
  6. primaryDataSourceName: # Primary data source name
  7. replicaDataSourceNames:
  8. - <replica-data-source-name> (+) # Replica data source name
  9. loadBalancerName: # Load balance algorithm name
  10. # Load balance algorithm configuration
  11. loadBalancers:
  12. <load-balancer-name> (+): # Load balance algorithm name
  13. type: # Load balance algorithm type
  14. props: # Load balance algorithm properties
  15. # ...
  16. props:
  17. # ...

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

ShardingSphere-4.x

Readwrite-splitting

Configuration Item Explanation

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames: [ds_slave0, ds_slave1]
  21. props:
  22. sql.show: true

Create a DataSource through the YamlMasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);

ShardingSphere-3.x

Readwrite-splitting

Configuration Item Explanation

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames: [ds_slave0, ds_slave1]
  21. props:
  22. sql.show: true
  23. configMap:
  24. key1: value1

Create a DataSource through the MasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);

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.

Configuration Item Explanation

  1. dataSources:
  2. db_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: org.h2.Driver
  4. url: jdbc:h2:mem:db_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. maxActive: 100
  8. db_slave_0: !!org.apache.commons.dbcp.BasicDataSource
  9. driverClassName: org.h2.Driver
  10. url: jdbc:h2:mem:db_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  11. username: sa
  12. password:
  13. maxActive: 100
  14. db_slave_1: !!org.apache.commons.dbcp.BasicDataSource
  15. driverClassName: org.h2.Driver
  16. url: jdbc:h2:mem:db_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  17. username: sa
  18. password:
  19. maxActive: 100
  20. masterSlaveRule:
  21. name: db_ms
  22. masterDataSourceName: db_master
  23. slaveDataSourceNames: [db_slave_0, db_slave_1]
  24. configMap:
  25. key1: value1

Create a DataSource through the MasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);