1 雪花算法为何会重复?什么情况下会重复?
雪花算法生成的Id由:1bit 不用 + 41bit时间戳+10bit工作机器id+12bit序列号,如下图:

集群部署的微服务,当随机的机器ID相同,刚好在同一毫秒生成ID,时间戳相同,并且序列号也相同时,那么雪花算法的ID就会出现重复的问题。
如果不重视该问题,会出现数据入库失败,从而丢失关键数据的问题。

2 如何解决重复问题
工作机器id:10bit,表示工作机器id,用于处理分布式部署id不重复问题,可支持2^10 = 1024个节点
我们只需要给同一个微服务分配不同的工作机器ID即可
在redis中存储一个当前workerId的最大值

每次生成workerId时,从redis中获取到当前workerId最大值,并+1作为当前workerId,并存入redis

如果workerId为1023,自增为1024,则重置0,作为当前workerId,并存入redis
以上方案参考唐江旭链接:https://www.jianshu.com/p/71286e89e0c5

3 代码实现
因为项目使用MyBatisPlus,所以我们直接替换他的ID生成器即可。
 

@Component
public class IdWorkConfig {

    private final RedisTemplate<String, Object> redisTemplate;
    private final String applicationName;

    public IdWorkConfig(RedisTemplate<String, Object> redisTemplate,
                        @Value("${spring.application.name}") String applicationName) {
        this.redisTemplate = redisTemplate;
        this.applicationName = applicationName;
    }

    /**
     * 自定义workerId,保证该应用的ID不会重复
     *
     * @return 新的id生成器
     */
    @Bean
    public DefaultIdentifierGenerator defaultIdentifierGenerator() {
        String MAX_ID = applicationName + "-worker-id";
        Long maxId = this.getWorkerId(MAX_ID);
        String maxIdStr = Long.toBinaryString(maxId);
        // 将数据补全为10位
        maxIdStr = StringUtils.leftPad(maxIdStr, 10, "0");

        // 从中间进行拆分
        String datacenterStr = maxIdStr.substring(0, 5);
        String workerStr = maxIdStr.substring(5, 10);

        // 将拆分后的数据转换成dataCenterId和workerId
        long dataCenterId = Integer.parseInt(datacenterStr, 2);
        long workerId = Integer.parseInt(workerStr, 2);
        return new DefaultIdentifierGenerator(workerId, dataCenterId);
    }

    /**
     * LUA脚本获取workerId,保证每个节点获取的workerId都不相同
     *
     * @param key 当前微服务的名称
     * @return workerId
     */
    private Long getWorkerId(String key) {
        String luaStr = "local isExist = redis.call('exists', KEYS[1])\n" +
                "if isExist == 1 then\n" +
                "    local workerId = redis.call('get', KEYS[1])\n" +
                "    workerId = (workerId + 1) % 1024\n" +
                "    redis.call('set', KEYS[1], workerId)\n" +
                "    return workerId\n" +
                "else\n" +
                "    redis.call('set', KEYS[1], 0)\n" +
                "    return 0\n" +
                "end";
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        // 以下两种二选一即可
        redisScript.setScriptText(luaStr);
        //redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("redis/redis_worker_id.lua")));
        redisScript.setResultType(Long.class);
        return redisTemplate.execute(redisScript, Collections.singletonList(key));
    }
}

如果选第二种需要建立redis_worker_id.lua文件,内容如下

local isExist = redis.call('exists', KEYS[1])
if isExist == 1 then
    local workerId = redis.call('get', KEYS[1])
    workerId = (workerId + 1) % 1024
    redis.call('set', KEYS[1], workerId)
    return workerId
else
    redis.call('set', KEYS[1], 0)
    return 0
end

这里为了保证从redis获取workerId的原子性,使用了lua脚本。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐