SpringBoot2.0集成shardingsphere分库分表组件

目前sharding-jdbc已经正式更改为shardingsphere,

shardingsphere官方网站:http://shardingsphere.io/index_zh.html

本篇博客主要介绍springboot以配置的形式集成shardingsphere,仅供大家参考

1、创建springboot工程

可以直接使用Spring Initializr生成项目

2、添加依赖关系

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

3、创建application.properties文件

server.port = 8080
spring.profiles.active = '@profileActive@'
spring.main.allow-bean-definition-overriding = true
spring.application.name = '@artifactId@'
//是否显示SQL
spring.shardingsphere.props.sql.show = true
spring.shardingsphere.datasource.names = ds0
spring.shardingsphere.datasource.ds0.driver-class-name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url = <your mysql url>
spring.shardingsphere.datasource.ds0.username = <mysql username>
spring.shardingsphere.datasource.ds0.password = <mysql password>
spring.shardingsphere.datasource.ds0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.filters = stat
spring.shardingsphere.datasource.ds0.maxActive = 20
spring.shardingsphere.datasource.ds0.initialSize = 1
spring.shardingsphere.datasource.ds0.maxWait = 60000
spring.shardingsphere.datasource.ds0.minIdle = 1
spring.shardingsphere.datasource.ds0.timeBetweenEvictionRunsMillis = 60000
spring.shardingsphere.datasource.ds0.minEvictableIdleTimeMillis = 300000
spring.shardingsphere.datasource.ds0.validationQuery = select 'x'
spring.shardingsphere.datasource.ds0.testWhileIdle = true
spring.shardingsphere.datasource.ds0.testOnBorrow = false
spring.shardingsphere.datasource.ds0.testOnReturn = false
spring.shardingsphere.datasource.ds0.poolPreparedStatements = true
spring.shardingsphere.datasource.ds0.maxOpenPreparedStatements = 20
spring.shardingsphere.sharding.tables.sms_send_log.actual-data-nodes = ds0.sms_send_log_201906,ds0.sms_send_log_201907,ds0.sms_send_log_201908,ds0.sms_send_log_201909,ds0.sms_send_log_201910,ds0.sms_send_log_201911,ds0.sms_send_log_201912
spring.shardingsphere.sharding.tables.sms_send_log.table-strategy.standard.sharding-column = created_at
spring.shardingsphere.sharding.tables.sms_send_log.table-strategy.standard.precise-algorithm-class-name = com.XX.XX.XX.config.SmsLogShardingAlgorithm

4、分片算法

SmsLogShardingAlgorithm
package com.config
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
import java.util.Date;

/**
 * 复合分片算法
 */
@Slf4j
public class SmsLogShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Date> shardingValue) {
        String tableName = shardingValue.getLogicTableName() + "_";
//        Date date = DateUtils.parseDate(shardingValue.getValue(), DatePatternEnum.PATTERN_Y_M_D_HMS.getValue());
        Date date = shardingValue.getValue();
        String year = String.format("%tY", date);
        String month = String.format("%tm", date);
        tableName = tableName + year + month;
        log.debug("tableName---->{}", tableName);

        for (String each : availableTargetNames) {
            log.debug("table--->{}", each);
            if (each.equals(tableName)) {
                return each;
            }
        }
        throw new IllegalArgumentException();
    }

    public static void main(String[] args) {
        String tableName = "sms_send_log_";
        Date date = DateUtils.parseDate("2019-06-27 12:12:12", DatePatternEnum.PATTERN_Y_M_D_HMS.getValue());
        String year = String.format("%tY", date);
        String month = String.format("%tm", date);
        tableName = tableName + year + month;
        log.debug(tableName);
    }
}

5、使用mybatis组件,具体不做详解

最后测试,这里我省略不写,用户自己去完成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值