Sharding-jdbc 整合springboot

/ Java / 没有评论 / 1327浏览

Sharding-jdbc 整合springboot

数据库中的主键设置

bigint类型,不是自增,使用mybatis的时候,可以不用自己管理其id,即: n_id,当然这个n_id可以自己生成,不使用框架提供的,但是不建议自增,因为不同表或库中可能会出现主键重复的问题。 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
  1. 编写启动类,springboot单元测试,进行数据库的插入,即可

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