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

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

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

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

1、创建springboot工程

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

2、添加依赖关系


    org.apache.shardingsphere
    sharding-jdbc-spring-boot-starter
    4.0.0-RC1

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 = 
spring.shardingsphere.datasource.ds0.username = 
spring.shardingsphere.datasource.ds0.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 {

    @Override
    public String doSharding(Collection availableTargetNames, PreciseShardingValue 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组件,具体不做详解

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

你可能感兴趣的:(java,分库分表)