基于smilehappiness-framework-base,快速集成ShardingSphere JDBC-3 如何集成使用分库分表

时间:2024-02-24 07:36:44

3.1 添加maven依赖

在需要使用分库分表的项目中,添加如下依赖:


<parent>
    <groupId>cn.smilehappiness</groupId>
    <artifactId>smilehappiness-framework-base</artifactId>
    <version>3.0.3-RELEASE</version>
</parent>

<dependency>
    <groupId>cn.smilehappiness</groupId>
    <artifactId>smilehappiness-shardingjdbc</artifactId>
</dependency>

3.2 添加 shardingSphere 数据源

为需要使用分库分表的mapper类打上 @DS(“shardingSphere”) 注解添加指定数据源,示例如下:


@DS("shardingSphere")
@Repository
public interface TShardingTestModeMapper extends BaseMapper<TShardingTestMod> {
}

3.2 添加 sharding jdbc 配置

注:查询分表的数据时,需要包含分表的分片键,比如使用biz_id分表,查询数据时,where条件中,需要包含biz_id这个字段,否则将会查询所有的表数据。

3.2.1 分表配置示例

spring:
  #feign to configure
  cloud:
    openfeign:
      # 默认Feign使用的是JDK自带的URLConnection进行Http,如果需要使用okhttp client,开启enabled为true
      okhttp:
        enabled: true
  shardingsphere:
    mode:
      type: Standalone
    props:
      sql-show: true
    datasource:
      common:
        driver-class-name: com.mysql.cj.jdbc.Driver
      names: credit
      credit:
        type: com.zaxxer.hikari.HikariDataSource
        jdbc-url: jdbc:mysql://ip:port/xxx?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
        username: admin
        password: 666
    rules:
      sharding:
        tables:
          bpm_process_data_record:
            actual-data-nodes: credit.bpm_process_data_record_$->{0..19}
            # 分库策略
            databaseStrategy:
              none:
            # 分表策略
            table-strategy:
              standard:
                sharding-column: biz_id
                sharding-algorithm-name: bpm-process-data-record-inline
          bpm_process_record:
            actual-data-nodes: credit.bpm_process_record_$->{0..9}
            table-strategy:
              standard:
                sharding-column: biz_id
                sharding-algorithm-name: bpm-process-record-inline
        sharding-algorithms:
          bpm-process-data-record-inline:
            type: INLINE
            props:
              algorithm-expression: bpm_process_data_record_$->{Long.parseLong(biz_id) % 20}
          bpm-process-record-inline:
            type: INLINE
            props:
              algorithm-expression: bpm_process_record_$->{Long.parseLong(biz_id) % 10}

3.2.2 分库且分表配置示例

spring:
  #feign to configure
  cloud:
    openfeign:
      # 默认Feign使用的是JDK自带的URLConnection进行Http
      okhttp:
        enabled: true
  shardingsphere:
    mode:
      type: Standalone
    props:
      sql-show: true
    datasource:
      common:
        driver-class-name: com.mysql.cj.jdbc.Driver
      names: db0,db1,db2
      db0:
        type: com.zaxxer.hikari.HikariDataSource
        jdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db0?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
        username: java
        password: AtOd5SYDRapIOU2O!NyjL!cMK90
      db1:
        type: com.zaxxer.hikari.HikariDataSource
        jdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db1?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
        username: java
        password: AtOd5SYDRapIOU2O!NyjL!cMK90
      db2:
        type: com.zaxxer.hikari.HikariDataSource
        jdbc-url: jdbc:mysql://local-mysql.xxx.com:3306/db2?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
        username: java
        password: AtOd5SYDRapIOU2O!NyjL!cMK90
    rules:
      sharding:
        tables:
          t_sharding_test_mod:
            actual-data-nodes: db$->{0..1}.t_sharding_test_mod_$->{0..2}
            database-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: database-inline
            table-strategy:
              standard:
                sharding-column: user_id
                sharding-algorithm-name: table-inline
          t_sharding_time_range:
            #如果多种时间区间组合,可以使用如下配置方式
            #actual-data-nodes: db2.t_sharding_time_range_$->{2023}_$->{12},db2.t_sharding_time_range_$->{2024..2025}_$->{(1..12).collect{t ->t.toString().padLeft(2,'0')}}
            actual-data-nodes: db2.t_sharding_time_range_$->{2024..2025}_$->{(1..12).collect{t ->t.toString().padLeft(2,'0')}}
            table-strategy:
              standard:
                sharding-column: created_time
                sharding-algorithm-name: time-inline
        sharding-algorithms:
          database-inline:
            type: INLINE
            props:
              algorithm-expression: db$->{id % 2}
          table-inline:
            type: INLINE
            props:
              algorithm-expression: t_sharding_test_mod_$->{user_id % 3}
          time-inline:
            type: INTERVAL
            props:
              datetime-pattern: "yyyy-MM-dd HH:mm:ss"
              sharding-suffix-pattern: "yyyy_MM"
              datetime-lower: "2024-01-01 00:00:00"
              datetime-upper: "2099-12-30 00:00:00"
              datetime-interval-amount: 1
              datetime-interval-unit: MONTHS