Introduction #
YAML provides a means of interacting with DBPlusEngine-Driver via configuration files. When used with the Governance module, the configuration persisted in the Configuration Centre is done in YAML format.
Note: YAML configuration files support configuration content over 3MB.
YAML configuration is the most common configuration method, eliminating the need for programming complexity and simplifying user configuration.
Usage steps #
Introduce Maven dependency #
<dependency>
<groupId>com.sphere-ex</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
Configure YAML #
The YAML configuration file of DBPlusEngine-Driver supports configuring multiple logical databases, and each logical database corresponds to a YAML configuration file. If there is only one logical database, all configurations including global configuration and logical database configuration can be written in one YAML configuration file.
Single logical database configuration #
The YAML configuration file for DBPlusEngine-Driver consists of a Schema name, a runtime schema, a collection of data sources, a collection of rules, and a configuration of attributes.
# Alias for the data source in JDBC. In cluster mode, use this parameter to associate DBPlusEngine-Driver with DBPlusEngine-Proxy.
# Default value: logic_db
databaseName (?):
mode:
dataSources:
rules:
- !FOO_XXX
...
- !BAR_XXX
...
props:
key_1: value_1
key_2: value_2
SeeMode Configurationfor details of the mode.
SeeData Source Configurationfor details of the data source.
SeeRule Configurationfor details of the rules.
Multiple logic database configuration #
1.Required to specify server.yaml
to configure global properties, as shown below:
mode:
...
authority:
...
props:
key_1: value_1
key_2: value_2
2.Configuration config-xxx.yaml
is used to configure the data source and rules of each logical database, as follows:
databaseName:
dataSources:
rules:
- !FOO_XXX
...
- !BAR_XXX
...
3.Specify the configuration path and default database name in the URL string of DBPlusEngine-Driver as follows:
url: jdbc:shardingsphere:absolutepath:/Users/configDirName?databaseName=sharding_db
Build data sources #
The ShardingSphereDataSource
created by the YamlShardingSphereDataSourceFactory
factory implements DataSource, a standard interface from JDBC.
File yamlFile = // Specify YAML file path
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);
Use data sources #
Used in the same way as the Java API.
Syntax notes #
!!
indicates that the class is instantiated
!
indicates custom aliases
-
indicates that it can contain one or more
[]
indicates an array and can be used interchangeably with the minus sign
Support using Apollo to store yaml configuration #
When using the Apollo loader, you need to add the corresponding apollo pom dependency. The apollo configuration method supports the use of properties and YAML two configuration formats in apollo. The currently adapted version is 1.9.0, as follows:
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo.version}</version>
</dependency>