【Redis场景拓展】秒杀问题-全局唯一ID生成策略

全局唯一ID

为什么要使用全局唯一ID:

当用户抢购时,就会生成订单并保存到订单表中,而订单表如果使用数据库自增ID就存在一些问题:

  • 受单表数据量的限制
  • id的规律性太明显

场景分析一:如果我们的id具有太明显的规则,用户或者说商业对手很容易猜测出来我们的一些敏感信息,比如商城在一天时间内,卖出了多少单,这明显不合适。

场景分析二:随着我们商城规模越来越大,mysql的单表的容量不宜超过500W,数据量过大之后,我们要进行拆库拆表,但拆分表了之后,他们从逻辑上讲他们是同一张表,所以他们的id是不能一样的, 于是乎我们需要保证id的唯一性。

场景分析三:如果全部使用数据库自增长ID,那么多张表都会出现相同的ID,不满足业务需求。

在分布式系统下全局唯一ID需要满足的特点:

  1. 唯一性
  2. 递增性
  3. 安全性
  4. 高可用(服务稳定)
  5. 高性能(生成速度够快)

为了提高数据库性能,这里采用Java中的数值类型(Long--8(Byte)字节,64位),

  • ID的组成部分:符号位:1bit,永远为0
  • 时间戳:31bit,以秒为单位,可以使用69年
  • 序列号:32bit,秒内的计数器,支持每秒产生2^32个不同ID

img

类雪花算法开发

我们的生成策略是基于redis的自增长,及序列号部分,在实现的时候需要传入不同的前缀(即不同业务不同序列号)

我们开始实现时间戳位数,先设置一个基准值,即某一时间的秒数,使用的时候用当前时间秒数-基准时间=所得秒数即时间戳;

基准值计算:这里我是用2023/1/1 0:0:0;秒数为:1672531200

public static void main(String[] args) {
    LocalDateTime time = LocalDateTime.of(2023, 1, 1, 0, 0, 0);
    //设置时区
    long l = time.toEpochSecond(ZoneOffset.UTC);
    System.out.println(l);
}

开始生成时间戳:获得当前时间的秒数-基准值(BEGIN_TIMESTAMP=1672531200)

LocalDateTime dateTime = LocalDateTime.now();
//秒数设置时区
long nowSecond = dateTime.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;

然后生成序列号,采用Redis的自增操作实现。keyPrefix业务Key(传入的)

long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix);

这一行代码的使用问题是,同一个业务使用的同一个key,但是redis的自增上限为2^64,总有时候会超过32位,所以最好是让其同一业务也要有不同的key值,这里我们可以加上当前时间。

//获取当日日期,精确到天
String date = dateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
//自增长上限2^64
long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);

这样做的好处是:

  1. 在redis中缓存是分层的,方便查看,也方便统计每天、每月的订单量或者其他数据等
  2. 不会超过Redis的自增长的值,安全性提高

img

最后将时间戳和序列号进行拼接即可,位运算。COUNT_BITS=32

timestamp << COUNT_BITS | count;

首先将时间戳左移32位,低处补零,然后进行或运算(遇1得1),这样实现整个的全局唯一ID。

测试

在同一个业务中使用全局唯一ID生成。

/**
 * 测试全局唯一ID生成器
 * @throws InterruptedException
 */
@Test
public  void testIdWorker() throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(300);
    ExecutorService executorService = Executors.newFixedThreadPool(300);
    Runnable task = ()->{
        for (int i = 0; i < 100; i++) {
            long id = redisIdWorker.nextId("order");
            System.out.println("id:"+id);
        }
        //计数-1
        countDownLatch.countDown();
    };
    long begin = System.currentTimeMillis();
    for (int i = 0; i < 300; i++) {
        executorService.submit(task);
    }
    //等待子线程结束
    countDownLatch.await();
    long endTime = System.currentTimeMillis();
    System.out.println("time= "+(endTime-begin));
}

time= 2608ms=2.68s,生成数量:30000

取两个相近的十进制转为二进制对比:

id : 148285184708444304

0010 0000 1110 1101 0000 1001 0111 0000 0000 0000 0000 0000 0000 1001 0000

id : 148285184708444305

0010 0000 1110 1101 0000 1001 0111 0000 0000 0000 0000 0000 0000 1001 0001

短码生成策略

仅支持很小的调用量,用于生成活动配置类编号,保证全局唯一

import java.util.Calendar;
import java.util.Random;

/**
 * @author xbhog
 * @describe:短码生成策略,仅支持很小的调用量,用于生成活动配置类编号,保证全局唯一
 * @date 2022/9/18
 */
@Slf4j
@Component
public class ShortCode implements IIdGenerator {
    @Override
    public synchronized long nextId() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        log.info("年:{},周:{},日:{},小时:{}",year, week,day,hour);
        //打乱顺序:2020年为准 + 小时 + 周期 + 日 + 三位随机数
        StringBuilder idStr = new StringBuilder();
        idStr.append(year-2020);
        idStr.append(hour);
        idStr.append(String.format("%02d",week));
        idStr.append(day);
        idStr.append(String.format("%03d",new Random().nextInt(1000)));
        log.info("查看拼接之后的值:{}",idStr);
        return Long.parseLong(idStr.toString());
    }

    public static void main(String[] args) {
        long l = new ShortCode().nextId();
        System.out.println(l);
    }
}

日志记录:

14:40:22.336 [main] INFO ShortCode - 年:2023,周:5,日:7,小时:14
14:40:22.341 [main] INFO ShortCode - 查看拼接之后的值:314057012
314057012
posted @ 2023-02-04 14:51  Xbhog  阅读(204)  评论(0编辑  收藏  举报