config-java.cn.md 34.7 KB
Newer Older
T
tristaZero 已提交
1 2
+++
title = "Java配置"
3
weight = 1
T
tristaZero 已提交
4 5
+++

6
## 配置示例
T
tristaZero 已提交
7

8
### 数据分片 
L
linzesi 已提交
9

T
tristaZero 已提交
10 11 12 13 14 15
```java
     DataSource getShardingDataSource() throws SQLException {
         ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
         shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
         shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
         shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
马晓光 已提交
16
         shardingRuleConfig.getBroadcastTables().add("t_config");
T
tristaZero 已提交
17
         shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
18
         shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new ModuloShardingTableAlgorithm()));
19
         return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
T
tristaZero 已提交
20 21
     }
     
T
tristaZero 已提交
22
     private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
23 24
         KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE", "order_id");
         return result;
T
tristaZero 已提交
25 26
     }
     
T
tristaZero 已提交
27
     TableRuleConfiguration getOrderTableRuleConfiguration() {
28
         TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");
T
tristaZero 已提交
29
         result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
30
         return result;
T
tristaZero 已提交
31 32 33
     }
     
     TableRuleConfiguration getOrderItemTableRuleConfiguration() {
34
         TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds${0..1}.t_order_item${0..1}");
35
         return result;
T
tristaZero 已提交
36 37 38
     }
     
     Map<String, DataSource> createDataSourceMap() {
T
tristaZero 已提交
39
         Map<String, DataSource> result = new HashMap<>();
T
tristaZero 已提交
40 41
         result.put("ds0", DataSourceUtil.createDataSource("ds0"));
         result.put("ds1", DataSourceUtil.createDataSource("ds1"));
T
tristaZero 已提交
42 43 44 45
         return result;
     }
```

46 47
### 读写分离

T
tristaZero 已提交
48 49
```java
     DataSource getMasterSlaveDataSource() throws SQLException {
L
lonyee 已提交
50 51
         MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("ds_master_slave", "ds_master", Arrays.asList("ds_slave0", "ds_slave1"));
         return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig, new Properties());
T
tristaZero 已提交
52 53 54
     }
     
     Map<String, DataSource> createDataSourceMap() {
55
         Map<String, DataSource> result = new HashMap<>();
56
         result.put("ds_master", DataSourceUtil.createDataSource("ds_master"));
T
tristaZero 已提交
57 58
         result.put("ds_slave0", DataSourceUtil.createDataSource("ds_slave0"));
         result.put("ds_slave1", DataSourceUtil.createDataSource("ds_slave1"));
T
tristaZero 已提交
59 60 61 62
         return result;
     }
```

L
Liang Zhang 已提交
63
### 数据加密
64 65 66

```java
    DataSource getEncryptDataSource() throws SQLException {
T
tristaZero 已提交
67
        return EncryptDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), getEncryptRuleConfiguration(), new Properties());
68 69
    }

T
tristaZero 已提交
70 71 72 73 74 75 76 77 78
    private static EncryptRuleConfiguration getEncryptRuleConfiguration() {
        Properties props = new Properties();
        props.setProperty("aes.key.value", "123456");
        EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", props);
        EncryptColumnRuleConfiguration columnConfig = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "aes");
        EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnConfig));
        EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration();
        encryptRuleConfig.getEncryptors().put("aes", encryptorConfig);
        encryptRuleConfig.getTables().put("t_encrypt", tableConfig);
L
lonyee 已提交
79
        return encryptRuleConfig;
80 81 82
    }
```

83
### 数据分片 + 读写分离
T
tristaZero 已提交
84 85

```java
T
tristaZero 已提交
86
    DataSource getDataSource() throws SQLException {
T
tristaZero 已提交
87 88 89 90
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
        shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
马晓光 已提交
91
        shardingRuleConfig.getBroadcastTables().add("t_config");
92 93
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm()));
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
T
tristaZero 已提交
94
        shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations());
L
lonyee 已提交
95
        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
T
tristaZero 已提交
96 97
    }
    
T
tristaZero 已提交
98
    private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
99 100
        KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE", "order_id");
        return result;
T
tristaZero 已提交
101 102
    }
    
T
tristaZero 已提交
103
    TableRuleConfiguration getOrderTableRuleConfiguration() {
104
        TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds_${0..1}.t_order_${[0, 1]}");
T
tristaZero 已提交
105
        result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
106
        return result;
T
tristaZero 已提交
107 108 109
    }
    
    TableRuleConfiguration getOrderItemTableRuleConfiguration() {
110
        TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
111
        return result;
T
tristaZero 已提交
112 113 114
    }
    
    List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
T
tristaZero 已提交
115 116
        MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration("ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
        MasterSlaveRuleConfiguration masterSlaveRuleConfig2 = new MasterSlaveRuleConfiguration("ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
T
tristaZero 已提交
117 118 119 120
        return Lists.newArrayList(masterSlaveRuleConfig1, masterSlaveRuleConfig2);
    }
    
    Map<String, DataSource> createDataSourceMap() {
T
tristaZero 已提交
121 122 123 124 125 126 127
        final Map<String, DataSource> result = new HashMap<>();
        result.put("demo_ds_master_0", DataSourceUtil.createDataSource("demo_ds_master_0"));
        result.put("demo_ds_master_0_slave_0", DataSourceUtil.createDataSource("demo_ds_master_0_slave_0"));
        result.put("demo_ds_master_0_slave_1", DataSourceUtil.createDataSource("demo_ds_master_0_slave_1"));
        result.put("demo_ds_master_1", DataSourceUtil.createDataSource("demo_ds_master_1"));
        result.put("demo_ds_master_1_slave_0", DataSourceUtil.createDataSource("demo_ds_master_1_slave_0"));
        result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1"));
128 129 130 131
        return result;
    }
```

L
Liang Zhang 已提交
132
### 数据分片 + 数据加密
T
tristaZero 已提交
133 134 135 136 137 138 139

```java
    public DataSource getDataSource() throws SQLException {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
        shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
        shardingRuleConfig.getTableRuleConfigs().add(getOrderEncryptTableRuleConfiguration());
T
tristaZero 已提交
140
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
T
tristaZero 已提交
141 142
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
T
tristaZero 已提交
143
        shardingRuleConfig.setEncryptRuleConfig(getEncryptRuleConfiguration());
T
tristaZero 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
    }
    
    private static TableRuleConfiguration getOrderTableRuleConfiguration() {
        TableRuleConfiguration result = new TableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
        result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
        return result;
    }
    
    private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
        TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
        result.setEncryptorConfig(new EncryptorConfiguration("MD5", "status", new Properties()));
        return result;
    }
    
T
tristaZero 已提交
159 160 161 162 163 164 165 166 167
    private static EncryptRuleConfiguration getEncryptRuleConfiguration() {
        Properties props = new Properties();
        props.setProperty("aes.key.value", "123456");
        EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", props);
        EncryptColumnRuleConfiguration columnConfig = new EncryptColumnRuleConfiguration("plain_order", "cipher_order", "", "aes");
        EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(Collections.singletonMap("order_id", columnConfig));
        EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration();
        encryptRuleConfig.getEncryptors().put("aes", encryptorConfig);
        encryptRuleConfig.getTables().put("t_order", tableConfig);
L
lonyee 已提交
168
		return encryptRuleConfig;
T
tristaZero 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }
    
    private static Map<String, DataSource> createDataSourceMap() {
        Map<String, DataSource> result = new HashMap<>();
        result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
        result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
        return result;
    }
    
    private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
        return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
    }
```

T
tristaZero 已提交
183
### 治理
184 185

```java
T
terrymanu 已提交
186
    DataSource getDataSource() throws SQLException {
187
        // OrchestrationShardingDataSourceFactory 可替换成 OrchestrationMasterSlaveDataSourceFactory 或 OrchestrationEncryptDataSourceFactory
T
terrymanu 已提交
188 189
        return OrchestrationShardingDataSourceFactory.createDataSource(
                createDataSourceMap(), createShardingRuleConfig(), new HashMap<String, Object>(), new Properties(), 
190
                new OrchestrationConfiguration(createCenterConfigurationMap()));
T
terrymanu 已提交
191
    }
192 193 194
    private Map<String, CenterConfiguration> createCenterConfigurationMap() {
        Map<String, CenterConfiguration> instanceConfigurationMap = new HashMap<String, CenterConfiguration>();
        CenterConfiguration config = createCenterConfiguration();
T
terrymanu 已提交
195
        instanceConfigurationMap.put("orchestration-shardingsphere-data-source", config);
196 197 198 199 200 201 202
        return instanceConfigurationMap;
    }
    private CenterConfiguration createCenterConfiguration() {
        Properties properties = new Properties();
        properties.setProperty("overwrite", overwrite);
        CenterConfiguration result = new CenterConfiguration("zookeeper", properties);
        result.setServerLists("localhost:2181");
T
terrymanu 已提交
203
        result.setNamespace("shardingsphere-orchestration");
204
        result.setOrchestrationType("registry_center,config_center,metadata_center");
205
        return result;
206 207
    }
```
T
tristaZero 已提交
208

209
## 配置项说明
T
tristaZero 已提交
210

211
### 数据分片
T
tristaZero 已提交
212

213
#### ShardingDataSourceFactory
T
tristaZero 已提交
214

215
数据分片的数据源创建工厂。
T
tristaZero 已提交
216

217 218 219 220
| *名称*             | *数据类型*                 | *说明*          |
| ------------------ |  ------------------------ | -------------- |
| dataSourceMap      | Map\<String, DataSource\> | 数据源配置      |
| shardingRuleConfig | ShardingRuleConfiguration | 数据分片配置规则 |
221
| props (?)          | Properties                | 属性配置        |
T
tristaZero 已提交
222

223
#### ShardingRuleConfiguration
T
tristaZero 已提交
224

225
分片规则配置对象。
T
tristaZero 已提交
226

T
tristaZero 已提交
227 228 229 230 231 232 233 234
| *名称*                                     | *数据类型*                                  | *说明*                                                                                         
| ----------------------------------------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------- |
| tableRuleConfigs                          | Collection\<TableRuleConfiguration\>       | 分片规则列表                                                                                      |
| bindingTableGroups (?)                    | Collection\<String\>                       | 绑定表规则列表                                                                                    |
| broadcastTables (?)                       | Collection\<String\>                       | 广播表规则列表                                                                                    |
| defaultDataSourceName (?)                 | String                                     | 未配置分片规则的表将通过默认数据源定位                                                                |
| defaultDatabaseShardingStrategyConfig (?) | ShardingStrategyConfiguration              | 默认分库策略                                                                                      |
| defaultTableShardingStrategyConfig (?)    | ShardingStrategyConfiguration              | 默认分表策略                                                                                      |
T
tristaZero 已提交
235
| defaultKeyGeneratorConfig (?)             | KeyGeneratorConfiguration                  | 默认自增列值生成器配置,缺省将使用org.apache.shardingsphere.core.keygen.generator.impl.SnowflakeKeyGenerator |
T
tristaZero 已提交
236
| masterSlaveRuleConfigs (?)                | Collection\<MasterSlaveRuleConfiguration\> | 读写分离规则,缺省表示不使用读写分离                                                                  |
T
tristaZero 已提交
237

238
#### TableRuleConfiguration
T
tristaZero 已提交
239

240
表分片规则配置对象。
T
tristaZero 已提交
241

242 243 244
| *名称*                              | *数据类型*                     | *说明*                                                                                                                                                                                                      |
| ---------------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| logicTable                         | String                        | 逻辑表名称                                                                                                                                                                                                   |
245
| actualDataNodes (?)                | String                        | 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况    |
246 247
| databaseShardingStrategyConfig (?) | ShardingStrategyConfiguration | 分库策略,缺省表示使用默认分库策略                                                                                                                                                                              |
| tableShardingStrategyConfig (?)    | ShardingStrategyConfiguration | 分表策略,缺省表示使用默认分表策略                                                                                                                                                                              |
T
tristaZero 已提交
248
| keyGeneratorConfig (?)             | KeyGeneratorConfiguration     | 自增列值生成器配置,缺省表示使用默认自增主键生成器                                                                                                                                                                |
T
tristaZero 已提交
249
| encryptorConfiguration (?)         | EncryptorConfiguration        | 加解密生成器配置                                                                                                                                                                                              |
T
tristaZero 已提交
250

251
#### StandardShardingStrategyConfiguration
T
tristaZero 已提交
252

253
ShardingStrategyConfiguration的实现类,用于单分片键的标准分片场景。
T
tristaZero 已提交
254

255 256 257 258 259
| *名称*                      | *数据类型*                | *说明*                  |
| -------------------------- | ------------------------ | ----------------------- |
| shardingColumn             | String                   | 分片列名称               |
| preciseShardingAlgorithm   | PreciseShardingAlgorithm | 精确分片算法,用于=和IN   |
| rangeShardingAlgorithm (?) | RangeShardingAlgorithm   | 范围分片算法,用于BETWEEN |
T
tristaZero 已提交
260

261
#### ComplexShardingStrategyConfiguration
T
tristaZero 已提交
262

263
ShardingStrategyConfiguration的实现类,用于多分片键的复合分片场景。
T
tristaZero 已提交
264

265 266 267 268
| *名称*             | *数据类型*                    | *说明*                   |
| ----------------- | ---------------------------- | ------------------------ |
| shardingColumns   | String                       | 分片列名称,多个列以逗号分隔 |
| shardingAlgorithm | ComplexKeysShardingAlgorithm | 复合分片算法               |
T
tristaZero 已提交
269

270
#### InlineShardingStrategyConfiguration
T
tristaZero 已提交
271

272
ShardingStrategyConfiguration的实现类,用于配置行表达式分片策略。
T
tristaZero 已提交
273

T
terrymanu 已提交
274 275 276
| *名称*               | *数据类型*  | *说明*                                                                                                   |
| ------------------- | ----------- | ------------------------------------------------------------------------------------------------------- |
| shardingColumn      |  String     | 分片列名称                                                                                               |
T
trista 已提交
277
| algorithmExpression |  String     | 分片算法行表达式,需符合groovy语法,详情请参考[行表达式](/cn/features/sharding/other-features/inline-expression) |
T
tristaZero 已提交
278

279
#### HintShardingStrategyConfiguration
T
tristaZero 已提交
280

281
ShardingStrategyConfiguration的实现类,用于配置Hint方式分片策略。
T
tristaZero 已提交
282

283 284 285
| *名称*             | *数据类型*             | *说明*      |
| ----------------- | --------------------- | ----------- |
| shardingAlgorithm | HintShardingAlgorithm | Hint分片算法 |
T
tristaZero 已提交
286

287
#### NoneShardingStrategyConfiguration
T
tristaZero 已提交
288

289
ShardingStrategyConfiguration的实现类,用于配置不分片的策略。
T
tristaZero 已提交
290

T
tristaZero 已提交
291
#### KeyGeneratorConfiguration
T
tristaZero 已提交
292

T
tristaZero 已提交
293 294 295
| *名称*             | *数据类型*                    | *说明*                                                                         |
| ----------------- | ---------------------------- | ------------------------------------------------------------------------------ |
| column            | String                       | 自增列名称                                                                      |
kimmking's avatar
kimmking 已提交
296 297
| type              | String                       | 自增列值生成器类型,可自定义或选择内置类型:SNOWFLAKE/UUID |
| props             | Properties                   | 自增列值生成器的相关属性配置                                                      |
298

299
#### Properties
300 301 302 303

属性配置项,可以为以下自增列值生成器的属性。

##### SNOWFLAKE
kimmking's avatar
kimmking 已提交
304

305 306 307 308 309
| *名称*                                              | *数据类型*  | *说明*                                                                                                   |
| --------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------- |
| worker.id (?)                                        | long       | 工作机器唯一id,默认为0                                                                                  |
| max.tolerate.time.difference.milliseconds (?)        | long       | 最大容忍时钟回退时间,单位:毫秒。默认为10毫秒                                                               |
| max.vibration.offset (?)                             | int        | 最大抖动上限值,范围[0, 4096),默认为1。注:若使用此算法生成值作分片值,建议配置此属性。此算法在不同毫秒内所生成的key取模2^n (2^n一般为分库或分表数) 之后结果总为0或1。为防止上述分片问题,建议将此属性值配置为(2^n)-1 |
T
tristaZero 已提交
310

T
tristaZero 已提交
311 312 313 314 315
#### EncryptRuleConfiguration

| *名称*               |*数据类型*                                    | *说明*                                                                          |
| ------------------- | ------------------------------------------- | ------------------------------------------------------------------------------ |
| encryptors          | Map<String, EncryptorRuleConfiguration>     | 加解密器配置列表,可自定义或选择内置类型:MD5/AES                                    |
316
| tables              | Map<String, EncryptTableRuleConfiguration>  | 加密表配置列表                                                                   |
T
tristaZero 已提交
317

T
tristaZero 已提交
318
#### EncryptorRuleConfiguration
T
tristaZero 已提交
319

T
tristaZero 已提交
320 321
| *名称*               |*数据类型*                    | *说明*                                                                          |
| ------------------- | ---------------------------- | ------------------------------------------------------------------------------ |
T
tristaZero 已提交
322
| type                | String                       | 加解密器类型,可自定义或选择内置类型:MD5/AES                                       |
kimmking's avatar
kimmking 已提交
323
| properties          | Properties                   | 属性配置, 注意:使用AES加密器,需要配置AES加密器的KEY属性:aes.key.value              |
T
tristaZero 已提交
324 325 326 327 328 329 330 331 332 333

#### EncryptTableRuleConfiguration

| *名称*               |*数据类型*                                     | *说明*                            |
| ------------------- | -------------------------------------------- | --------------------------------- |
| tables              | Map<String, EncryptColumnRuleConfiguration>  | 加密列配置列表                      |

#### EncryptColumnRuleConfiguration

| *名称*               |*数据类型*                    | *说明*                                                                          |
334
| ------------------- | ---------------------------- | ------------------------------------------------------------------------------ |
T
tristaZero 已提交
335 336 337
| plainColumn        | String                       | 存储明文的字段                                                                   |
| cipherColumn       | String                       | 存储密文的字段                                                                   |
| assistedQueryColumn| String                       | 辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询            |
338
| encryptor          | String                       | 加解密器名字                                                                      |
T
tristaZero 已提交
339

340
#### Properties
T
tristaZero 已提交
341

342
属性配置项,可以为以下属性。
T
tristaZero 已提交
343

344 345 346 347 348
| *名称*                             | *数据类型*  | *说明*                                          |
| ----------------------------------| --------- | -------------------------------------------------|
| sql.show (?)                      | boolean   | 是否开启SQL显示,默认值: false                      |
| executor.size (?)                 | int       | 工作线程数量,默认值: CPU核数                       |
| max.connections.size.per.query (?)| int       | 每个物理数据库为每次查询分配的最大连接数量。默认值: 1   |
T
tristaZero 已提交
349
| check.table.metadata.enabled (?)  | boolean   | 是否在启动时检查分表元数据一致性,默认值: false        |
T
tristaZero 已提交
350
| query.with.cipher.column (?)      | boolean   | 当存在明文列时,是否使用密文列查询,默认值: true        |
351
| allow.range.query.with.inline.sharding (?)    | boolean   | 当使用inline分表策略时,是否允许范围查询,默认值: false        |
T
tristaZero 已提交
352

353
### 读写分离
T
tristaZero 已提交
354

355
#### MasterSlaveDataSourceFactory
T
tristaZero 已提交
356

357
读写分离的数据源创建工厂。
T
tristaZero 已提交
358

359 360
| *名称*                 | *数据类型*                    | *说明*             |
| --------------------- | ---------------------------- | ------------------ |
T
tristaZero 已提交
361 362 363
| dataSourceMap         | Map\<String, DataSource\>    | 数据源与其名称的映射  |
| masterSlaveRuleConfig | MasterSlaveRuleConfiguration | 读写分离规则         |
| props (?)             | Properties                   | 属性配置            |
T
tristaZero 已提交
364

365
#### MasterSlaveRuleConfiguration
T
tristaZero 已提交
366

367
读写分离规则配置对象。
T
tristaZero 已提交
368

369 370 371 372 373 374
| *名称*                    | *数据类型*                       | *说明*           |
| ------------------------ | ------------------------------- | ---------------- |
| name                     | String                          | 读写分离数据源名称 |
| masterDataSourceName     | String                          | 主库数据源名称    |
| slaveDataSourceNames     | Collection\<String\>            | 从库数据源名称列表 |
| loadBalanceAlgorithm (?) | MasterSlaveLoadBalanceAlgorithm | 从库负载均衡算法   |
T
tristaZero 已提交
375

376
#### Properties
377 378 379

属性配置项,可以为以下属性。

380
| *名称*                              | *数据类型* | *说明*                                            |
T
terrymanu 已提交
381 382
| ---------------------------------- | --------- | ------------------------------------------------- |
| sql.show (?)                       | boolean   | 是否打印SQL解析和改写日志,默认值: false              |
383
| executor.size (?)                  | int       | 用于SQL执行的工作线程数量,为零则表示无限制。默认值: 0   |
T
terrymanu 已提交
384
| max.connections.size.per.query (?) | int       | 每个物理数据库为每次查询分配的最大连接数量。默认值: 1    |
T
tristaZero 已提交
385
| check.table.metadata.enabled (?)   | boolean   | 是否在启动时检查分表元数据一致性,默认值: false         |
386

L
Liang Zhang 已提交
387
### 数据加密
388 389 390 391 392 393

#### EncryptDataSourceFactory

| *名称*                 | *数据类型*                    | *说明*             |
| --------------------- | ---------------------------- | ------------------ |
| dataSource            | DataSource                   | 数据源,任意连接池    |
L
Liang Zhang 已提交
394
| encryptRuleConfig     | EncryptRuleConfiguration     | 数据加密规则         |
395 396 397 398
| props (?)             | Properties                   | 属性配置            |

#### EncryptRuleConfiguration

T
tristaZero 已提交
399 400 401 402 403
| *名称*               |*数据类型*                                    | *说明*                                                                          |
| ------------------- | ------------------------------------------- | ------------------------------------------------------------------------------ |
| encryptors          | Map<String, EncryptorRuleConfiguration>     | 加解密器配置列表,可自定义或选择内置类型:MD5/AES                                    |
| tables              | Map<String, EncryptTableRuleConfiguration>  | 加密表配置列表                      |

404
#### Properties
T
tristaZero 已提交
405 406 407 408 409 410 411

属性配置项,可以为以下属性。

| *名称*                             | *数据类型*  | *说明*                                          |
| ----------------------------------| --------- | -------------------------------------------------|
| sql.show (?)                      | boolean   | 是否开启SQL显示,默认值: false                      |
| query.with.cipher.column (?)      | boolean   | 当存在明文列时,是否使用密文列查询,默认值: true       |
412

T
tristaZero 已提交
413
### 治理
414

T
tristaZero 已提交
415
#### OrchestrationShardingDataSourceFactory
416

T
tristaZero 已提交
417
数据分片 + 治理的数据源工厂。
418 419 420

| *名称*               | *数据类型*                  | *说明*                      |
| ------------------- |  ------------------------- | --------------------------- |
T
tristaZero 已提交
421 422 423
| dataSourceMap       | Map\<String, DataSource\>  | 同ShardingDataSourceFactory |
| shardingRuleConfig  | ShardingRuleConfiguration  | 同ShardingDataSourceFactory |
| props (?)           | Properties                 | 同ShardingDataSourceFactory |
T
tristaZero 已提交
424
| orchestrationConfig | OrchestrationConfiguration | 治理规则配置              |
425

T
tristaZero 已提交
426
#### OrchestrationMasterSlaveDataSourceFactory
427

T
tristaZero 已提交
428
读写分离 + 治理的数据源工厂。
429

430 431
| *名称*                 | *数据类型*                    | *说明*                         |
| --------------------- | ---------------------------- | ------------------------------ |
T
tristaZero 已提交
432 433 434
| dataSourceMap         | Map\<String, DataSource\>    | 同MasterSlaveDataSourceFactory |
| masterSlaveRuleConfig | MasterSlaveRuleConfiguration | 同MasterSlaveDataSourceFactory |
| props (?)             | Properties                   | 同ShardingDataSourceFactory    |
T
tristaZero 已提交
435
| orchestrationConfig   | OrchestrationConfiguration   | 治理规则配置                 |
T
tristaZero 已提交
436

437 438
#### OrchestrationEncryptDataSourceFactory

L
Liang Zhang 已提交
439
数据加密 + 治理的数据源工厂。
440 441 442 443 444 445 446 447

| *名称*                 | *数据类型*                    | *说明*                         |
| --------------------- | ---------------------------- | ------------------------------ |
| dataSource            | DataSource                   | 同EncryptDataSourceFactory     |
| encryptRuleConfig     | EncryptRuleConfiguration     | 同EncryptDataSourceFactory     |
| props (?)             | Properties                   | 同ShardingDataSourceFactory    |
| orchestrationConfig   | OrchestrationConfiguration   | 治理规则配置                 |

448 449
#### OrchestrationConfiguration

T
tristaZero 已提交
450
治理规则配置对象。
451

452 453
| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
454
| instanceConfigurationMap | Map\<String, CenterConfiguration\>  | 配置/注册/元数据中心的配置map,key为名称,value为配置/注册/元数据中心   |
455

456
#### CenterConfiguration
457

458
用于配置配置/注册/元数据中心。
459

T
terrymanu 已提交
460 461
| *名称*                             | *数据类型* | *说明*                                                                               |
| --------------------------------- | ---------- | ----------------------------------------------------------------------------------- |
462
| instanceType                      | String     | 配置/注册/元数据中心的实例类型,例如zookeeper或etcd、apollo、nacos                                       |
463
| properties                        | String     | 配置本实例需要的其他参数,例如zookeeper的连接参数等,具体参考properties配置                         |
464 465 466
| orchestrationType                 | String     | 治理类型,例如config_center/registry_center/metadata_center,如果都是,可以"setOrchestrationType("registry_center,config_center,metadata_center");"              |
| serverLists                       | String     | 连接配置/注册/元数据中心服务器的列表,包括IP地址和端口号,多个地址用逗号分隔。如: host1:2181,host2:2181 |
| namespace (?)                     | String     | 配置/注册/元数据中心的命名空间                                                                     |
467 468 469 470 471

其中properties的通用配置如下:

| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
472
| overwrite                         | boolean    | 本地配置是否覆盖配置中心配置,如果可覆盖,每次启动都以本地配置为准                         |
473

474
如果采用了zookeeper作为配置中心或(和)注册中心或 (和) 元数据中心,那么properties还可以配置:
475 476 477

| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
T
terrymanu 已提交
478 479 480 481 482
| digest (?)                        | String     | 连接注册中心的权限令牌。缺省为不需要权限验证                                             |
| operationTimeoutMilliseconds (?)  | int        | 操作超时的毫秒数,默认500毫秒                                                          |
| maxRetries (?)                    | int        | 连接失败后的最大重试次数,默认3次                                                       |
| retryIntervalMilliseconds (?)     | int        | 重试间隔毫秒数,默认500毫秒                                                            |
| timeToLiveSeconds (?)             | int        | 临时节点存活秒数,默认60秒                                                             |
483

484
如果采用了etcd作为配置中心或(和)注册中心或 (和) 元数据中心,那么properties还可以配置:
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
| timeToLiveSeconds (?)             | long        | TTL时间,单位为秒,默认30秒                                     |

如果采用了apollo作为配置中心,那么properties还可以配置:

| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
| appId (?)          | String        | apollo appId,默认值为"APOLLO_SHARDINGSPHERE"                               |
| env (?)            | String        | apollo env,默认值为"DEV"                                                   |
| clusterName (?)    | String        | apollo clusterName,默认值为"default"                                       |
| administrator (?)  | String        | apollo administrator,默认值为""                                            |
| token (?)          | String        | apollo token,默认值为""                                                    |
| portalUrl (?)      | String        | apollo portalUrl,默认值为""                                                |
| connectTimeout (?) | int           | apollo connectTimeout,默认值为1000毫秒                                      |
| readTimeout (?)    | int           | apollo readTimeout,默认值为5000毫秒                                         |

503
如果采用了nacos作为配置中心或 (和) 注册中心,那么properties还可以配置:
504 505 506 507 508

| *名称*           | *数据类型*                   | *说明*                                                     |
| --------------- | --------------------------- | ---------------------------------------------------------- |
| group (?)          | String        | nacos group配置,默认值为"SHARDING_SPHERE_DEFAULT_GROUP"                     |
| timeout (?)        | long          | nacos 获取数据超时时间,单位为毫秒,默认值为3000毫秒                            |