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.

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 schema 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. # Alias of the datasource in JDBC.
  2. # Through this parameter to connect, ShardingSphere-JDBC and ShardingSphere-Proxy.
  3. # Default value: logic_db
  4. schemaName (?):
  5. mode:
  6. dataSources:
  7. rules:
  8. - !FOO_XXX
  9. ...
  10. - !BAR_XXX
  11. ...
  12. props:
  13. key_1: value_1
  14. key_2: value_2

Please refer to Mode Confiugration for more mode details.

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

Please refer to Rules Confiugration 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