Change History
5.0.0-alpha
Replica Query
Configuration Item Explanation
dataSource: # Omit the data source configuration, please refer to the usage
rules:
- !REPLICA_QUERY
dataSources:
<data-source-name> (+): # Logic data source name of replica query
primaryDataSourceName: # Primary data source name
replicaDataSourceNames:
- <replica-data-source-name> (+) # Replica data source name
loadBalancerName: # Load balance algorithm name
# Load balance algorithm configuration
loadBalancers:
<load-balancer-name> (+): # Load balance algorithm name
type: # Load balance algorithm type
props: # Load balance algorithm properties
# ...
props:
# ...
Please refer to Built-in Load Balance Algorithm List for more details about type of algorithm.
ShardingSphere-4.x
Readwrite-splitting
Configuration Item Explanation
dataSources:
ds_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_master
username: root
password:
ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave0
username: root
password:
ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave1
username: root
password:
masterSlaveRule:
name: ds_ms
masterDataSourceName: ds_master
slaveDataSourceNames: [ds_slave0, ds_slave1]
props:
sql.show: true
Create a DataSource
through the YamlMasterSlaveDataSourceFactory
factory class:
DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);
ShardingSphere-3.x
Readwrite-splitting
Configuration Item Explanation
dataSources:
ds_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_master
username: root
password:
ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave0
username: root
password:
ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave1
username: root
password:
masterSlaveRule:
name: ds_ms
masterDataSourceName: ds_master
slaveDataSourceNames: [ds_slave0, ds_slave1]
props:
sql.show: true
configMap:
key1: value1
Create a DataSource
through the MasterSlaveDataSourceFactory
factory class:
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
- Provides a readwrite-splitting configuration with one master and multiple slaves, which can be used independently or with sub-databases and sub-meters.
- Independent use of readwrite-splitting to support SQL transparent transmission.
- 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.
- Spring namespace.
- Hint-based mandatory main library routing.
Unsupported
- Data synchronization between the master library and the slave library.
- Data inconsistency caused by the data synchronization delay of the master library and the slave library.
- Double writing or multiple writing in the main library.
Configuration Item Explanation
dataSources:
db_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db_slave_0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db_slave_1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
masterSlaveRule:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames: [db_slave_0, db_slave_1]
configMap:
key1: value1
Create a DataSource
through the MasterSlaveDataSourceFactory
factory class:
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);
当前内容版权归 ShardingSphere 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ShardingSphere .