docs/document/content/user-manual/shardingsphere-jdbc/java-api/mode.cn.md
+++ title = "模式配置" weight = 1 chapter = true +++
通过 Java API 方式构建运行模式。
类名称:org.apache.shardingsphere.infra.config.mode.ModeConfiguration
可配置属性:
| 名称 | 数据类型 | 说明 | 默认值 |
|---|---|---|---|
| type | String | 运行模式类型 | |
| 可选配置:Standalone、Cluster | Standalone | ||
| repository | PersistRepositoryConfiguration | 持久化仓库配置 | |
| Standalone 类型使用 StandalonePersistRepositoryConfiguration | |||
| Cluster 类型使用 ClusterPersistRepositoryConfiguration | false |
类名称:org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepositoryConfiguration
可配置属性:
| 名称 | 数据类型 | 说明 |
|---|---|---|
| type | String | 持久化仓库类型 |
| props | Properties | 持久化仓库所需属性 |
类名称:org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration
可配置属性:
| 名称 | 数据类型 | 说明 |
|---|---|---|
| type | String | 持久化仓库类型 |
| namespace | String | 注册中心命名空间 |
| server-lists | String | 注册中心连接地址 |
| props | Properties | 持久化仓库所需属性 |
ZooKeeper 注册中心。ZooKeeper 存在配置信息时,则以 ZooKeeper 中的配置为准。<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>${latest.release.version}</version>
</dependency>
注意:请将
${latest.release.version}更改为实际的版本号。
ModeConfiguration modeConfig = createModeConfiguration();
Map<String, DataSource> dataSourceMap = ... // 构建真实数据源
Collection<RuleConfiguration> ruleConfigs = ... // 构建具体规则
Properties props = ... // 构建属性配置
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig, dataSourceMap, ruleConfigs, props);
private ModeConfiguration createModeConfiguration() {
return new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("JDBC", new Properties()));
}
ModeConfiguration modeConfig = createModeConfiguration();
Map<String, DataSource> dataSourceMap = ... // 构建真实数据源
Collection<RuleConfiguration> ruleConfigs = ... // 构建具体规则
Properties props = ... // 构建属性配置
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(databaseName, modeConfig, dataSourceMap, ruleConfigs, props);
private ModeConfiguration createModeConfiguration() {
return new ModeConfiguration("Cluster", new ClusterPersistRepositoryConfiguration("ZooKeeper", "governance-sharding-db", "localhost:2181", new Properties()));
}