shardingsphere 整合spring boot的yaml配置

1. 搭建基本的sharding-jdbc整合springboot

  1. springboot导入sharding-jdbc相关依赖

复制代码
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile 'org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.0.0-RC1' // 这个是
    compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
    compile 'mysql:mysql-connector-java:8.0.15'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'
    compileOnly 'org.projectlombok:lombok:1.18.8'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}
复制代码
  2. sharding-jdbc的配spring# 相关的配置  shardingsphere:

复制代码
# 是否展示sql
    props:
      sql:
        show: true
    # 数据源配置
    datasource:
      # 数据源名称
      names: ds1
      # 数据源名称对应的连接池,url,用户名和密码配置
      ds1:
        # 数据库连接池类型:HikariDataSource 是springboot自带的数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
复制代码
  3.  编写启动类,springboot单元测试,进行数据库的插入,即可

 

2. sharding-jdbc的相关配置,以及分库分表

  1. 同库水平分表

复制代码
spring:
# 相关的配置
  shardingsphere:
    props:
      sql:
        show: true
    datasource:
      names: ds1
      ds1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        username: root
        password: root
    sharding:
      tables:
        # 这个地方注意: sharding-jdbc会根据名称去找本节点,所以写sql的时候,要写此节点的名称
        t_student:
          # 表达式, 健康节点: 根据上一个节点找到此值, {1..2}为groovy语言,$会替换成{1..2}的一个值,数据库表是: t_student_1 , t_student_2
          # 这个配置是告诉sharding有多少个表
          actual-data-nodes: ds1.t_student_$->{1..2}
          # 主键生成策略
          key-generator:
            # 对应的数据库表的主键
            column: n_id
            # 生成方式, 雪花模式
            type: SNOWFLAKE
          # 配置其分片策略和分片算法
          table-strategy:
            # 行表达式
            inline:
              # 配置sharding的计算列
              sharding-column: n_id
              # 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
              algorithm-expression: t_student_$->{n_id % 2 +1}
复制代码
  2. 水平分库,在水平分库基础上进行水平分表

复制代码
spring:
# 相关的配置
  shardingsphere:
    props:
      sql:
        show: true
    datasource:
      # 配置数据源的名称
      names: ds1,ds2
      # 第一个数据源
      ds1:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
      # 第二个数据源
      ds2:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
    # 分库的策略
    sharding:
      tables:
        # 这个地方注意: sharding-jdbc会根据名称去找本节点,所以写sql的时候,要写此节点的名称
        t_student:
          # 配置数据库的分库策略
          database-strategy:
            # 行表达式模式
            inline:
              # 选择需要分库的字段,根据那个字段进行区分
              sharding-column: n_card
              # 表达式,c_card需要和上面的一致,groovy表达式
              algorithm-expression: ds$->{n_card % 2 + 1}
          # 表达式, 健康节点: 根据上一个节点找到此值, {1..2}为groovy语言,$会替换成{1..2}的一个值,数据库表是: t_student_1 , t_student_2
          # 这个配置是告诉sharding有多少个表
          actual-data-nodes: ds1.t_student_$->{1..2}
          # 主键生成策略
          key-generator:
            # 对应的数据库表的主键
            column: n_id
            # 生成方式, 雪花模式
            type: SNOWFLAKE
          # 配置其分片策略和分片算法
          table-strategy:
            # 行表达式
            inline:
              # 配置sharding的计算列
              sharding-column: n_id
              # 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
              algorithm-expression: t_student_$->{n_id % 2 +1}

mybatis.configuration.map-underscore-to-camel-case: true
复制代码
  3.垂直分库

复制代码
spring:
# 相关的配置
  shardingsphere:
    props:
      sql:
        show: true
    datasource:
      # 配置数据源的名称
      names: ds1,ds2,ds3
      # 第一个数据源
      ds1:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
      # 第二个数据源
      ds2:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
      # 配置垂直分库策略
      ds3:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
    # 分库的策略
    sharding:
      tables:
        # 垂直分库、分表,其实就是写死了
        t_course:
          database-strategy:
            inline:
              sharding-column: n_id
              algorithm-expression: ds3
        # 虽然垂直分库没有必要但是还要配置一下分表规则
#          table-strategy:
#            inline:
#              sharding-column: n_id
#              algorithm-expression: t_course

        # 水平分库、分表
        # 这个地方注意: sharding-jdbc会根据名称去找本节点,所以写sql的时候,要写此节点的名称
        t_student:
          # 配置数据库的分库策略
          database-strategy:
            # 行表达式模式
            inline:
              # 选择需要分库的字段,根据那个字段进行区分
              sharding-column: n_card
              # 表达式,c_card需要和上面的一致,groovy表达式
              algorithm-expression: ds$->{n_card % 2 + 1}
          # 表达式, 健康节点: 根据上一个节点找到此值, {1..2}为groovy语言,$会替换成{1..2}的一个值,数据库表是: t_student_1 , t_student_2
          # 这个配置是告诉sharding有多少个表
          actual-data-nodes: ds1.t_student_$->{1..2}
          # 主键生成策略
          key-generator:
            # 对应的数据库表的主键
            column: n_id
            # 生成方式, 雪花模式
            type: SNOWFLAKE
          # 配置其分片策略和分片算法
          table-strategy:
            # 行表达式
            inline:
              # 配置sharding的计算列
              sharding-column: n_id
              # 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
              algorithm-expression: t_student_$->{n_id % 2 +1}

mybatis.configuration.map-underscore-to-camel-case: true
复制代码
  4. 读写分离,sharding-jdbc只负责路由,即:帮助我们把select或者insert、update、delete路由到不同的数据库上,但是不会帮助我们同步数据库数据

  

复制代码
spring:
# 相关的配置
  shardingsphere:
    props:
      sql:
        show: true
    datasource:
      # 配置数据源的名称
      names: ds1,ds2,ds3
      # 第一个数据源
      ds1:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
      # 第二个数据源
      ds2:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
      # 配置垂直分库策略
      ds3:
        # 数据库连接池
        type: com.zaxxer.hikari.HikariDataSource
        # 数据库驱动
        driver-class-name: com.mysql.jdbc.Driver
        # 数据库链接
        jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
        # 数据库用户名
        username: root
        # 数据库密码
        password: root
    # 分库的策略
    sharding:
      tables:
        # 垂直分库、分表,其实就是写死了
        t_course:
          database-strategy:
            inline:
              sharding-column: n_id
              algorithm-expression: ds3
        # 虽然垂直分库没有必要但是还要配置一下分表规则
#          table-strategy:
#            inline:
#              sharding-column: n_id
#              algorithm-expression: t_course

        # 水平分库、分表
        # 这个地方注意: sharding-jdbc会根据名称去找本节点,所以写sql的时候,要写此节点的名称
        t_student:
          # 配置数据库的分库策略
          database-strategy:
            # 行表达式模式
            inline:
              # 选择需要分库的字段,根据那个字段进行区分
              sharding-column: n_card
              # 表达式,c_card需要和上面的一致,groovy表达式
              algorithm-expression: ds$->{n_card % 2 + 1}
          # 表达式, 健康节点: 根据上一个节点找到此值, {1..2}为groovy语言,$会替换成{1..2}的一个值,数据库表是: t_student_1 , t_student_2
          # 这个配置是告诉sharding有多少个表
          actual-data-nodes: ds1.t_student_$->{1..2}
          # 主键生成策略
          key-generator:
            # 对应的数据库表的主键
            column: n_id
            # 生成方式, 雪花模式
            type: SNOWFLAKE
          # 配置其分片策略和分片算法
          table-strategy:
            # 行表达式
            inline:
              # 配置sharding的计算列
              sharding-column: n_id
              # 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
              algorithm-expression: t_student_$->{n_id % 2 +1}

      # 主从规则,未做验证
      master-slave-rules:
        # 随机起一个名称,如果配置主从,那么需要修改分表策略:::公共表修改
        # 分表策略改成: spring.shardingsphere.sharding.tables.t_public.actual-data-nodes=ms0.t_public
        # ms0 主从已经指向了 ds1、ds2数据源
        ms0:
          # 主节点
          master-data-source-name: ds1
          # 从节点
          slave-data-source-names: ds2


mybatis.configuration.map-underscore-to-camel-case: true
复制代码
 

分类: sharding-jdbc

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ShardingSphere-JDBC 的 YAML 配置规则如下: 1. 数据源配置 ```yaml dataSources: ds0: url: jdbc:mysql://localhost:3306/db0?serverTimezone=UTC username: root password: root driverClassName: com.mysql.jdbc.Driver ds1: url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC username: root password: root driverClassName: com.mysql.jdbc.Driver ``` 2. 分片规则配置 ```yaml shardingRule: tables: user: actualDataNodes: ds$->{0..1}.user$->{0..1} tableStrategy: standard: shardingColumn: user_id preciseAlgorithmClassName: com.xxx.PreciseShardingAlgorithm rangeAlgorithmClassName: com.xxx.RangeShardingAlgorithm shardingAlgorithmClassName: com.xxx.ComplexKeysShardingAlgorithm keyGenerateStrategy: column: user_id type: SNOWFLAKE ``` 3. 数据库和表的默认分片策略 ```yaml defaultDatabaseStrategy: standard: shardingColumn: user_id preciseAlgorithmClassName: com.xxx.PreciseShardingAlgorithm rangeAlgorithmClassName: com.xxx.RangeShardingAlgorithm shardingAlgorithmClassName: com.xxx.ComplexKeysShardingAlgorithm defaultTableStrategy: none: ``` 4. 数据源和表的映射关系配置 ```yaml props: sql: show: true # 从库负载均衡策略 executor.size: 20 executor.keepAliveSeconds: 300 executor.blockQueueSize: 1000 executor.threadFactory: net.sourceforge.jtds.jdbc.ThreadFactoryImpl slave.load.balance.strategy.type: ROUND_ROBIN slave.load.balance.algorithm.type: HASH # 执行超时时间 max.timeout.milliseconds: 2000 # 是否打印 SQL sql.show: true # 是否打印 SQL 时输出原始 SQL sql.simple: false # 是否开启动态表路由 dynamic.table: true # 是否允许表规则不存在,如果不存在则直接路由到主库 allow.table.without.rule: true # 是否允许无法路由的 SQL 走主库 allow.broadcast.without.sharding: true ``` 以上就是 ShardingSphere-JDBC 的 YAML 配置规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值