All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ltsopensource.core.loadbalance.ConsistentHashLoadBalance Maven / Gradle / Ivy

package com.github.ltsopensource.core.loadbalance;

import com.github.ltsopensource.core.commons.concurrent.ThreadLocalRandom;
import com.github.ltsopensource.core.support.ConsistentHashSelector;

import java.util.List;

/**
 * 一致性hash算法
 * Robert HG ([email protected]) on 3/25/15.
 */
public class ConsistentHashLoadBalance extends AbstractLoadBalance {

    @Override
    protected  S doSelect(List shards, String seed) {
        if(seed == null || seed.length() == 0){
            seed = "HASH-".concat(String.valueOf(ThreadLocalRandom.current().nextInt()));
        }
        ConsistentHashSelector selector = new ConsistentHashSelector(shards);
        return selector.selectForKey(seed);
    }
}