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.
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);
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);
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.
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);