基于 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];
    }


}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值