ShardingSphere-JDBC

Scenarios

There are four ways you can configure Apache ShardingSphere: Java and YAML. Developers can choose the preferred method according to their requirements.

Limitations

Currently only Java language is supported.

Requirements

The development environment requires Java JRE 8 or later.

Procedure

  1. Rules configuration.

Please refer to User Manual for more details.

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

Notice: Please change ${latest.release.version} to the actual version.

  1. Create YAML configuration file
  1. # JDBC database name. In cluster mode, use this parameter to connect ShardingSphere-JDBC and ShardingSphere-Proxy.
  2. # Default: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
  1. Take spring boot as an example, edit application.properties.
  1. # Configuring DataSource Drivers
  2. spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
  3. # Specify a YAML configuration file
  4. spring.datasource.url=jdbc:shardingsphere:classpath:xxx.yaml

For details, see Spring Boot.