[已经放弃,截止5.5.0版本,jshardingsphere-dbc无法动态建表,

对于期望插入,不存在则创建的用户无法友好使用,

目前插入的数据,在分片策略上设置的表名必须要提前创建]

 

dynamic-datasource:通过@DS("数据源"),动态切换数据库连接

hikari:轻量级超快连接池

shardingsphere:分库分表

 

1.shardingsphere分为jdbc和proxy,如果不想额外部署中间件,选择jdbc

2.参考官网文档,一定要选择自己版本的文档

 3.<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>这个依赖只有5.1.1以下才有<artifactId>shardingsphere-jdbc</artifactId>之后才有,两个都可以用但是每个版本却别很大,分片实现以及配置都有差别

4.与多数据源dynamic-datasource整合参考这个配置

--- # 数据源配置
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    # 动态数据源文档 https://www.kancloud.cn/tracy5546/dynamic-datasource/content
    dynamic:
      # 性能分析插件(有性能损耗 不建议生产环境使用)
      p6spy: true
      # 设置默认的数据源或者数据源组,默认值即为 master
      primary: master
      # 严格模式 匹配不到数据源则报错
      strict: true
      datasource:
        # 主库数据源
        master:
          type: ${spring.datasource.type}
          driverClassName: com.mysql.cj.jdbc.Driver
          # jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562
          # rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题)
          url: jdbc:mysql://localhost:3306/xxxxxx?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
          username: root
          password: root
        # 从库数据源
        sharding:
          url: jdbc:shardingsphere:classpath:sharding.yml
          driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
      hikari:
        # 最大连接池数量
        maxPoolSize: 20
        # 最小空闲线程数量
        minIdle: 10
        # 配置获取连接等待超时的时间
        connectionTimeout: 30000
        # 校验超时时间
        validationTimeout: 5000
        # 空闲连接存活最大时间,默认10分钟
        idleTimeout: 600000
        # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认30分钟
        maxLifetime: 1800000
        # 连接测试query(配置检测连接是否有效)
        connectionTestQuery: SELECT 1
        # 多久检查一次连接的活性
        keepaliveTime: 30000

  sharding.yml

dataSources:
  ds_0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
rules:
  - !SHARDING
    #    默认库策略
    #    defaultDatabaseStrategy:
    #      standard:
    #        shardingColumn: id
    #        shardingAlgorithmName: database-inline
    bindingTables:
      - ykj_msg
    # 表策略
    tables:
      ykj_msg:
        actualDataNodes: ds_0.ykj_msg_$->{0..1}
        tableStrategy:
          standard:
            shardingColumn: id
            shardingAlgorithmName: ykj_msg-inline
    shardingAlgorithms:
      #      database-inline:
      #        type: INLINE
      #        props:
      #          algorithm-expression: ds_$->{0}
      ykj_msg-inline:
        type: INLINE
        props:
          algorithm-expression: ykj_msg_$->{id % 2}
props:
  sql-show: true

5.需要注意的是,分片策略要先配置数据库分片策略,再配置表的策略,比如配置取模的分片策略,会先更具

database-inline.algorithm-expression找到数据库,再根据
ykj_msg-inline.algorithm-expression找到表

缺一不可,否则报错。

6.如果未使用@DS指定数据库连接,默认master连接,如果要使用分库分表,必须使用@DS("sharding"),这个在上边配置有

7.整合途中如果遇到,什么找不到方法,不要犹豫,就是依赖冲突了,去maven插件市场搜maven helper 检查冲突,取消或者排除。

8.shardingsphere不会自动建表,需要预先创建好需要分的表。