ShardingSphere中有哪些配置项呢?

欢喜 MyBatis 发布时间:2025-06-04 14:43:55 阅读数:11199 1
下文笔者讲述ShardingShere配置项的简介说明,如下所示

Apache ShardingSphere简介

Apache ShardingSphere
   是一个分布式数据库中间件
     提供丰富配置项
	 用于定义数据源、分片策略、读写分离、加密脱敏等功能

Apache ShardingSphere配置项

一、核心配置项(`rules`)

ShardingSphere 的配置是基于 `rules` 配置项进行扩展的,常见的规则如下:

1.数据分片(`sharding`)
-作用:定义水平分片、垂直分片、复合分片等。
-关键配置项:
  - `tables`: 定义逻辑表及其分片规则。
    ```yaml
    tables:
      t_order:
        actual-data-nodes: ds$->{0..1}.t_order$->{0..1}
        table-strategy:
          standard:
            sharding-column: order_id
            sharding-algorithm-name: t_order_table_inline
        key-generator:
          type: SNOWFLAKE

  - `sharding-algorithms`: 分片算法定义。
    ```yaml
    sharding-algorithms:
      t_order_table_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_$->{order_id % 2}
    ```

2.读写分离(`readwrite-splitting`)
-作用:将读操作和写操作路由到不同的数据库实例。
-关键配置项:
  ```yaml
  readwrite-splitting:
    data-sources:
      ds_0:
        write-data-source-name: master
        read-data-source-names:
          - slave1
          - slave2
        load-balancer-type: ROUND_ROBIN

3.数据加密 
-作用:对敏感字段进行加密存储。
-关键配置项:
  ```yaml
  encrypt:
    tables:
      user:
        columns:
          password:
            cipher-column: password_cipher
            plain-column: password_plain
            encryptor-name: aes_encryptor
    encryptors:
      aes_encryptor:
        type: AES
        props:
          aes-key-value: 123456
 

4.影子库配置(`shadow`)
- 作用:用于测试环境的数据分流,避免污染真实数据。
- 关键配置项:
  ```yaml
  shadow:
    mappings:
      t_order:
        production-data-source-names: ds0,ds1
        shadow-data-source-names: shadow_ds0
    shadow-algorithms:
      order-id-algorithm:
        type: VALUE_MATCH
        props:
          operation: INSERT
          value: 1024
 
5.数据脱敏(`masking`)
- 作用:对输出结果进行脱敏处理。
- 关键配置项:
  ```yaml
  masking:
    rules:
      - logic-table: user
        logic-column: id_card
        mask-algorithm: id_card_mask
    mask-algorithms:
      id_card_mask:
        type: ID_CARD

二数据源配置项(`dataSources`)

定义多个实际的数据源连接信息:

```yaml
dataSources:
  ds0:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ds0
    username: root
    password: root
 

三、全局配置项([props])

控制 ShardingSphere 的全局行为:
配置项 示例值 说明
`sql.show` `true` / `false` 是否打印解析后SQL
`executor.size` `16` 工作线程池大小
`check.table.metadata.enabled` `true` 是否检查元数据一致性
例(完整YAML格式)
```yaml
dataSources:
  ds0:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ds0
    username: root
    password: root

rules:
  - !SHARDING
    tables:
      t_order:
        actual-data-nodes: ds0.t_order_$->{0..1}
        table-strategy:
          standard:
            sharding-column: order_id
            sharding-algorithm-name: t_order_inline
    sharding-algorithms:
      t_order_inline:
        type: INLINE
        props:
          algorithm-expression: t_order_$->{order_id % 2}

  - !READWRITE_SPLITTING
    data-sources:
      ds_0:
        write-data-source-name: ds0
        read-data-source-names:
          - ds1
        load-balancer-type: ROUND_ROBIN

props:
  sql.show: true
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/MyBatis/202506/8468.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者