Spring boot 整合 SardingSphere (精确分表策略)

概要:

application.properties

# 精确分表策略
spring.shardingsphere.sharding.tables.s_user.table-strategy.standard.sharding-column=age
spring.shardingsphere.sharding.tables.s_user.table-strategy.standard.precise-algorithm-class-name=com.sharding.demo.config.TableShardingAlgorithm

TableShardingAlgorithm

package com.sharding.demo.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;

@Slf4j
public class TableShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> preciseShardingValue) {
        try{
            log.info("preciseShardingValue:{},column:{}",preciseShardingValue.getValue(),preciseShardingValue.getColumnName());
            for (String each : availableTargetNames) {
                log.info("each:" + each);
            }
            // 返回 就是最后落实的表
            return "s_user0";
        } catch (Exception e){
            e.printStackTrace();
        }
        throw new IllegalArgumentException();
    }
}

 

上一篇:分库分表ShardingSphere<三> _ 分布式事务


下一篇:shardingsphere-jdbc各版本分表组件使用