ShardingSphere分库+数据脱敏配置方式

145 阅读1分钟

近日对新表进行分库处理+数据脱敏处理后发现根据官网的配置方式进行处理仅能实现对数据进行脱敏,分库与主键配置均不生效

官网配置:

dataSources: # Configure the real data source name.
  write_ds:
    # ...Omit specific configuration.
  read_ds_0:
    # ...Omit specific configuration.
  read_ds_1:
    # ...Omit specific configuration.

rules:
  - !SHARDING # Configure data sharding rules.
    tables:
      t_user:
        actualDataNodes: ds.t_user_${0..1} # Data source name 'ds' uses the logical data source name of the readwrite-splitting configuration.
        tableStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: t_user_inline
    shardingAlgorithms:
      t_user_inline:
        type: INLINE
        props:
          algorithm-expression: t_user_${user_id % 2}
  
  - !ENCRYPT # Configure data encryption rules.
    tables:
      t_user: # Table `t_user` is the name of the logical table that uses the data sharding configuration.
        columns:
          pwd:
            plainColumn: plain_pwd
            cipherColumn: cipher_pwd
            encryptorName: encryptor_aes
    encryptors:
      encryptor_aes:
        type: aes
        props:
          aes-key-value: 123456abc
  
  - !READWRITE_SPLITTING # Configure readwrite-splitting rules.
    dataSources:
      ds: # The logical data source name 'ds' for readwrite-splitting is used in data sharding.
        type: Static
        props:
          write-data-source-name: write_ds # Use the real data source name 'write_ds'.
          read-data-source-names: read_ds_0, read_ds_1 # Use the real data source name 'read_ds_0', 'read_ds_1'.
        loadBalancerName: roundRobin
    loadBalancers:
      roundRobin:
        type: ROUND_ROBIN

props:
  sql-show: true

经过代码翻阅后发现实际配置方式应为如下:

正确配置

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://ip:port/database?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: 123456
      ds1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://ip:port/database?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: 123456
    sharding:
      tables:
        t_user:
          actual-data-nodes: ds$->{0..1}.t_user
          database-strategy:
            inline:
              sharding-column: id
              algorithm-expression: ds${id % 2}
          #指定id
          key-generator:
            column: id
            type: SNOWFLAKE
      encrypt-rule:
        encryptors:
          dn_pwd:
            type: aes
            props:
              aes:
                key:
                  value: 1234
        tables:
          t_user:
            columns:
              pwd:
                #plain-column: pwd_plain
                cipher-column: pwd_cipher
                encryptor: dn_pwd
    props:
      query:
        with:
          cipher:
            column: true