Overview

YAML configuration provides interaction with ShardingSphere JDBC through configuration files. When used with the governance module together, the configuration of persistence in the configuration center is YAML format.

Note: The YAML configuration file supports more than 3MB of configuration content.

YAML configuration is the most common configuration mode, which can omit the complexity of programming and simplify user configuration.

Usage

Import Maven Dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc-core</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>

YAML Format

ShardingSphere-JDBC YAML file consists of database name, mode configuration, data source map, rule configurations and properties.

Note: The example connection pool is HikariCP, which can be replaced with other connection pools according to business scenarios.

  1. # JDBC logic database name. Through this parameter to connect ShardingSphere-JDBC and ShardingSphere-Proxy.
  2. # Default value: logic_db
  3. databaseName (?):
  4. mode:
  5. dataSources:
  6. rules:
  7. - !FOO_XXX
  8. ...
  9. - !BAR_XXX
  10. ...
  11. props:
  12. key_1: value_1
  13. key_2: value_2

Please refer to Mode Configuration for more mode details.

Please refer to Data Source Configuration for more data source details.

Please refer to Rules Configuration for more rule details.

Create Data Source

The ShardingSphereDataSource created by YamlShardingSphereDataSourceFactory implements the standard JDBC DataSource interface.

  1. File yamlFile = // Indicate YAML file
  2. DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);

Use Data Source

Same with Java API.

YAML Syntax Explanation

!! means instantiation of that class

! means self-defined alias

- means one or multiple can be included

[] means array, can substitutable with - each other