Background

Build the running mode through Java API.

Parameters

Class name: org.apache.shardingsphere.infra.config.mode.ModeConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
typeStringType of mode configuration
Values could be: Standalone or Cluster
Standalone
repositoryPersistRepositoryConfigurationPersist repository configuration
Standalone type uses StandalonePersistRepositoryConfiguration
Cluster type uses ClusterPersistRepositoryConfiguration

Standalone Persist Configuration

Class name: org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration

Attributes:

NameDataTypeDescription
typeStringType of persist repository
propsPropertiesProperties of persist repository

Cluster Persist Configuration

Class name: org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration

Attributes:

NameData TypeDescription
typeStringType of persist repository
namespaceStringNamespace of registry center
server-listsStringServer lists of registry center
propsPropertiesProperties of persist repository

Notes

  1. Cluster mode deployment is recommended for production environment.
  2. The ZooKeeper registry center is recommended for cluster mode deployment.
  3. If there is configuration information in the ZooKeeper, please refer to the config information there.

Procedure

Introduce Maven Dependency

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

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

Sample

Standalone Mode

  1. ModeConfiguration modeConfig = createModeConfiguration();
  2. Map<String, DataSource> dataSourceMap = ... // Building real data sources
  3. Collection<RuleConfiguration> ruleConfigs = ... // Build specific rules
  4. Properties props = ... // Build property configuration
  5. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig, dataSourceMap, ruleConfigs, props);
  6. private ModeConfiguration createModeConfiguration() {
  7. return new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("JDBC", new Properties()));
  8. }
  1. ModeConfiguration modeConfig = createModeConfiguration();
  2. Map<String, DataSource> dataSourceMap = ... // Building real data sources
  3. Collection<RuleConfiguration> ruleConfigs = ... // Build specific rules
  4. Properties props = ... // Build property configuration
  5. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig, dataSourceMap, ruleConfigs, props);
  6. private ModeConfiguration createModeConfiguration() {
  7. return new ModeConfiguration("Cluster", new ClusterPersistRepositoryConfiguration("ZooKeeper", "governance-sharding-db", "localhost:2181", new Properties()));
  8. }