sharding-jdbc(5.0.0)整合springboot

功能需求:根据年份进行分表

需要注意的点:

1.ShardingAlgorithmTool  中的数据配置

  /** 数据库配置 */
    private static final Environment ENV = SpringUtil.getApplicationContext().getEnvironment();
    private static final String DATASOURCE_URL = ENV.getProperty("spring.shardingsphere.datasource.mydb.url");
    private static final String DATASOURCE_USERNAME = ENV.getProperty("spring.shardingsphere.datasource.mydb.username");
    private static final String DATASOURCE_PASSWORD = ENV.getProperty("spring.shardingsphere.datasource.mydb.password");

2. TimeShardingAlgorithm 中要改对应的时间格式

    private static final DateTimeFormatter TABLE_SHARD_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy");

        在运行过程中,发现错误的时候其他时间格式注意有时候也需要改。

3. SpringUtil 会找不到    ApplicationContext applicationContext

        在运行过程中会爆出  applicationContext  =  null   

        原因:可能是工具类的问题

        解决:换个其他的springutils工具类。(可以使用 hutool 中的)

4.雪花id主键生成配置,可以在相应的位置进行配置,如下yml。还有相关的 sharding 其他配置。

spring:
    ### 处理连接池冲突 #####
    main:
        allow-bean-definition-overriding: true
    shardingsphere:
        # 是否启用 Sharding
        enabled: true
        # 打印sql
        #    props:
        #      sql-show: true
        datasource:
            names: mydb
            mydb:
                type: com.alibaba.druid.pool.DruidDataSource
                url: jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
                driver-class-name: com.mysql.cj.jdbc.Driver
                username: root
                password: 123456
                # 初始连接数
                initialSize: 5
                # 最小连接池数量
                minIdle: 10
                # 最大连接池数量
                maxActive: 20
                # 配置获取连接等待超时的时间
                maxWait: 60000
                # 配置连接超时时间
                connectTimeout: 30000
                # 配置网络超时时间
                socketTimeout: 60000
                # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
                timeBetweenEvictionRunsMillis: 60000
                # 配置一个连接在池中最小生存的时间,单位是毫秒
                minEvictableIdleTimeMillis: 300000
                # 配置一个连接在池中最大生存的时间,单位是毫秒
                maxEvictableIdleTimeMillis: 900000
                # 配置检测连接是否有效
                validationQuery: SELECT 1 FROM DUAL
                testWhileIdle: true
                testOnBorrow: false
                testOnReturn: false
                webStatFilter:
                    enabled: true
                statViewServlet:
                    enabled: true
                    # 设置白名单,不填则允许所有访问
                    allow:
                    url-pattern: /druid/*
                    # 控制台管理用户名和密码
                    login-username: ruoyi
                    login-password: 123456
                filter:
                    stat:
                        enabled: true
                        # 慢SQL记录
                        log-slow-sql: true
                        slow-sql-millis: 1000
                        merge-sql: true
                    wall:
                        config:
                            multi-statement-allow: true
        rules:
            sharding:
                # 表策略配置
                tables:
                    # t_user 是逻辑表
                    t_user:
                        # 配置数据节点,这里是按月分表
                        # 示例1:时间范围设置在202201 ~ 210012
                        # actualDataNodes: mydb.t_user_$->{2022..2100}0$->{1..9},mydb.t_user_$->{2022..2100}1$->{0..2}
                        # 示例2:时间范围设置在202201 ~ 202203
                        actualDataNodes: mydb.t_user_$->{2024..2030}
                        tableStrategy:
                            # 使用标准分片策略
                            standard:
                                # 配置分片字段
                                shardingColumn: addtime
                                # 分片算法名称,不支持大写字母和下划线,否则启动就会报错
                                shardingAlgorithmName: time-sharding-algorithm
                        key-generate-strategy: # 主键生成策略
                            column: fid  # 主键列
                            key-generator-name: snowflake  # 策略算法名称(推荐使用雪花算法)
                # 分片算法配置
                shardingAlgorithms:
                    # 分片算法名称,不支持大写字母和下划线,否则启动就会报错
                    time-sharding-algorithm:
                        # 类型:自定义策略
                        type: CLASS_BASED
                        props:
                            # 分片策略
                            strategy: standard
                            # 分片算法类
                            algorithmClassName: com.ruoyi.web.controller.visitor.config.TimeShardingAlgorithm
                key-generators:
                    snowflake:
                        type: SNOWFLAKE

参考文章:Sharding-JDBC(六)5.1.0版本,实现按月分表、自动建表、自动刷新节点_shardingsphere 建表-CSDN博客

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值