springboot整合shardingJdbc常用配置

springboot整合shardingJdbc常用配置

起男 715 2022-10-25

springboot整合shardingJdbc常用配置

配置基于5.1.1

配置模式

spring:
  shardingsphere:
    mode:
      type: Memory

可选模式:

  • Memory:内存模式
  • Standalone:单机模式
  • Cluster:集群模式

Standalone和Cluster需要额外配置

配置数据源

spring:
  shardingsphere:
    datasource:
      names: db1,db2... #数据源名称列表
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/db1
        username: root
        password: dqn123
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3307/db2
        username: root
        password: dqn123
	......

开启日志

spring:
  shardingsphere:
    props:
      sql-show: true

读写分离

spring:
  shardingsphere:
    rules:
      readwrite-splitting:
        data-sources:
          myds:
            type: Static
            props:
              write-data-source-name: master #写数据源名称
              read-data-source-names: slave1,slave2 #读数据源名称,可配置多个
            load-balancer-name: my_weight #使用的负载均衡策略
        load-balancers: #定义负载均衡策略
          my_robin:
            type: ROUND_ROBIN #轮询
          my_random:
            type: RANDOM #随机
          my_weight:
            type: WEIGHT #权重
            props:
              slave1: 1
              slave2: 2

垂直分片

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t1:
            actual-data-nodes: db1.t1
          t2:
            actual-data-nodes: db2.t2

水平分片

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t1: #逻辑表名
            actual-data-nodes: db$->{0..2}.t$->{[0,1]} #分片表配置
            database-strategy: #分库策略
              standard:
                sharding-column: u_id  #根据哪个字段分库
                sharding-algorithm-name: my_mod #分库算法
            table-strategy: #分表策略
              standard:
                sharding-column: o_no #根据哪个字段分表
                sharding-algorithm-name: my_hash_mod #分表算法
        sharding-algorithms: #定义算法
          my_inline:
            type: INLINE #行表达式分片算法
            props:
              algorithm-expression: db$->{u_id % 2} #表达式
          my_mod:
            type: MOD #取模
            props:
              sharding-count: 2 #分片数量
          my_hash_mod:
            type: HASH_MOD #哈希取模(可对字符串取模)
            props:
              sharding-count: 2

生成主键

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t1: #逻辑表名
            key-generate-strategy: #生成主键
              column: id #主键字段名
              key-generator-name: my_snowflake #使用算法
        key-generators: #定义算法
          my_snowflake:
            type: SNOWFLAKE #雪花算法
          my_uuid:
            type: UUID #uuid算法

如果使用mybatisplus,需要在主键字段配置@TableId(type = IdType.AUTO)

绑定表

spring:
  shardingsphere:
    rules:
      sharding:
        binding-tables:
          - db1,db2
          #......

绑定表:指分片规则一致的一组分片表。使用绑定表进行多表关联查询时,必须使用分片键进行关联,否则会出现笛卡儿积关联或跨库关联,从而影响效率

广播表

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t1:
            actual-data-nodes: db$->{0..2}.t1 #表位置
        broadcast-tables: #指定广播表
          - t1

广播表,指所有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中完全一致