基于 Redis 的 分布式 ID 生成器

  • 通过 Redis 分发 ID 号段实现分布式生成
  • 通过 Hashid 算法实现固定长度的 62 位编码的 ID 字符串,可用于短链 Key 的生成
import org.hashids.Hashids;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;


/**
 * 发号器
 *
 * @author Neo
 * @since 2023/9/13 14:43
 */
@Service
public class SignalSenderService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${signal.sender.hash.id.salt:N7Y16ioS}")
    private String hashIdSalt;

    private final Hashids hashids = new Hashids(hashIdSalt, 6);

    private static final String SIGNAL_SENDER_INCR_KEY = "signal.sender.incr.key";

    @Value("${signal.sender.range.batch:10000}")
    private int rangBatch;

    private AtomicLong current;
    private Long end = 0L;

    /**
     * 初始化
     *
     * @author Neo
     * @since 2023/9/15 10:38
     */
    @PostConstruct
    public void init() {
        this.range();
    }


    /**
     * 批量获取固定长度的ID
     *
     * @author Neo
     * @since 2023/9/15 09:26
     */
    public List<String> getBase62Id(Integer num) {
        if (Objects.isNull(num) || num < 1) {
            return Collections.emptyList();
        }
        List<String> result = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            result.add(this.getBase62Id());
        }
        return result;
    }

    /**
     * 获取固定长度的ID
     *
     * @author Neo
     * @since 2023/9/15 09:26
     */
    public String getBase62Id() {
        return this.encode(this.getId());
    }

    /**
     * 获取ID
     *
     * @author Neo
     * @since 2023/9/14 09:01
     */
    public List<Long> getId(Integer num) {
        if (Objects.isNull(num) || num < 1) {
            return Collections.emptyList();
        }
        List<Long> result = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            result.add(this.getId());
        }
        return result;
    }

    /**
     * 获取ID
     *
     * @author Neo
     * @since 2023/9/14 09:01
     */
    public synchronized Long getId() {
        while (true) {
            long result = current.getAndIncrement();
            if (result > end) {
                this.range();
            } else {
                return result;
            }
        }
    }

    /**
     * 获取号段范围
     *
     * @author Neo
     * @since 2023/9/15 09:15
     */
    private synchronized void range() {
        if (Objects.nonNull(this.current) && this.current.get() <= this.end) {
            return;
        }
        this.end = redisTemplate.opsForValue().increment(SIGNAL_SENDER_INCR_KEY, rangBatch);
        this.current = new AtomicLong(this.end - rangBatch + 1);
    }


    /**
     * ID 编码
     *
     * @author Neo
     * @since 2023/9/15 09:43
     */
    public String encode(Long id) {
        return this.hashids.encode(id);
    }


    /**
     * ID 解码
     *
     * @author Neo
     * @since 2023/9/15 09:44
     */
    public Long decode(String key) {
        return hashids.decode(key)[0];
    }


}

你可能感兴趣的:(常用工具类,redis,分布式,ID,生成器)