shardingsphere-jdbc 分库的配置和实践-读写分离

shardingsphere-jdbc 分库的配置和实践-读写分离

主库:ds0,从库:s0,主库:ds1,从库:s1

配置

spring:
  shardingsphere:
    # 数据源配置
    datasource:
      names: ds0,s0,ds1,s1
      # 分片库配置
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ds0?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        initial-size: 5
        maxActive: 5
        maxWait: 60000
        poolPreparedStatements: true
      s0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/s0?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        initial-size: 5
        maxActive: 5
        maxWait: 60000
        poolPreparedStatements: true
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ds1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        initial-size: 5
        maxActive: 5
        maxWait: 60000
        poolPreparedStatements: true
      s1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/s1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        initial-size: 5
        maxActive: 5
        maxWait: 60000
        poolPreparedStatements: true
    sharding:
      default-database-strategy:
        standard:
          sharding-column: id
          precise-algorithm-class-name: com.clsu.algorithm.MyPreciseAlgorithm
          range-algorithm-class-name: com.clsu.algorithm.MyRangeAlgorithm
      tables:
        test:
          actual-data-nodes: ds$->{0..1}.test$->{0..1}
          table-strategy:
            standard:
              sharding-column: id
              range-algorithm-class-name: com.clsu.algorithm.MyRangeAlgorithm
              #split database impl
              precise-algorithm-class-name: com.clsu.algorithm.MyPreciseAlgorithm
          key-generator:
            column: id
            type: SNOWFLAKE
      # 读写分离配置
      master-slave-rules:
        ds0:
          master-data-source-name: ds0
          slave-data-source-names: s0
        ds1:
          master-data-source-name: ds1
          slave-data-source-names: s1
    props:
      sql:
        show: true


版权声明:本文为m0_37735277原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。