打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Redis在集群环境中生成唯一ID

概述

设计目标:每秒最大生成10万个ID,ID单调递增且唯一。Reidis可以不需要持久化ID。
要求:集群时钟不能倒退。
总体思路:集群中每个节点预生成生成ID;然后与redis的已经存在的ID做比较。如果大于,则取节点生成的ID;小于的话,取Redis中最大ID自增。

Java代码

import org.apache.commons.lang3.RandomStringUtils;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.time.FastDateFormat;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import static com.google.common.base.Preconditions.checkArgument;/** * 生成递增的唯一序列号, 可以用来生成订单号,例如216081817202494579 * <p/> * 生成规则: * 业务类型 + redis中最大的序列号 * <p/> * 约定: * redis中最大的序列号长度为17,包括{6位日期 + 6位时间 + 3位毫秒数 + 2位随机} * <p/> * 建议: * 为了容错和服务降级, SeqGenerator生成失败时最好采用UUID替换 * <p/> * Created by juemingzi on 16/8/19. */public class SeqGenerator {    private static final Logger logger = LoggerFactory.getLogger(SeqGenerator.class);    private static final Path filePath = Paths.get(Thread.currentThread().getContextClassLoader().getResource("lua/get_next_seq.lua").getPath());    //线程安全    private static final FastDateFormat seqDateFormat = FastDateFormat.getInstance("yyMMddHHmmssSSS");    private static final RedisExtraService redisExtraService = SpringContext.getBean(RedisExtraService.class);    private final byte[] keyName;    private final byte[] incrby;    private byte[] sha1;    public SeqGenerator(String keyName) throws IOException {        this(keyName, 1);    }    /**     * @param keyName     * @param incrby     */    public SeqGenerator(String keyName, int incrby) throws IOException {        checkArgument(keyName != null && incrby > 0);        this.keyName = keyName.getBytes();        this.incrby = Integer.toString(incrby).getBytes();        init();    }    private void init() throws IOException {        byte[] script;        try {            script = Files.readAllBytes(filePath);        } catch (IOException e) {            logger.error("读取文件出错, path: {}", filePath);            throw e;        }        sha1 = redisExtraService.scriptLoad(script);    }    public String getNextSeq(String bizType) {        checkArgument(StringUtils.isNotBlank(bizType));        return bizType + getMaxSeq();    }    private String generateSeq() {        String seqDate = seqDateFormat.format(System.currentTimeMillis());        String candidateSeq = new StringBuilder(17).append(seqDate).append(RandomStringUtils.randomNumeric(2)).toString();        return candidateSeq;    }    /**     * 通过redis生成17位的序列号,lua脚本保证序列号的唯一性     *     * @return     */    public String getMaxSeq() {        String maxSeq = new String((byte[]) redisExtraService.evalsha(sha1, 3, keyName, incrby, generateSeq().getBytes()));        return maxSeq;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

lua脚本

---- 获取最大的序列号,样例为16081817202494579---- Created by IntelliJ IDEA.-- User: juemingzi-- Date: 16/8/18-- Time: 17:22local function get_max_seq()    local key = tostring(KEYS[1])    local incr_amoutt = tonumber(KEYS[2])    local seq = tostring(KEYS[3])    local month_in_seconds = 24 * 60 * 60 * 30    if (1 == redis.call(\'setnx\', key, seq))    then        redis.call(\'expire\', key, month_in_seconds)        return seq    else        local prev_seq = redis.call(\'get\', key)        if (prev_seq < seq)        then            redis.call(\'set\', key, seq)            return seq        else        --[[            不能直接返回redis.call(\'incr\', key),因为返回的是number浮点数类型,会出现不精确情况。            注意: 类似"16081817202494579"数字大小已经快超时lua和reids最大数值,请谨慎的增加seq的位数        --]]            redis.call(\'incrby\', key, incr_amoutt)            return redis.call(\'get\', key)        end    endendreturn get_max_seq()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

测试代码

public class SeqGeneratorTest extends BaseTest {    @Test    public void testGetNextSeq() throws Exception {        final SeqGenerator seqGenerater = new SeqGenerator("orderId");        String orderId = seqGenerater.getNextSeq(Integer.toString(WaitingOrder.KIND_TAKE_OUT));        assertNotNull(orderId);        System.out.println("orderId is: " + orderId);    }    @Test    public void testGetNextSeqWithMultiThread() throws Exception {        int cpus = Runtime.getRuntime().availableProcessors();        CountDownLatch begin = new CountDownLatch(1);        CountDownLatch end = new CountDownLatch(cpus);        final Set<String> seqSet = new ConcurrentSkipListSet<>();        ExecutorService executorService = Executors.newFixedThreadPool(cpus);        final SeqGenerator seqGenerater = new SeqGenerator("orderId");        for (int i = 0; i < cpus; i++) {            executorService.execute(new Worker(seqGenerater, seqSet, begin, end));        }        begin.countDown();        end.await();        assertEquals(seqSet.size(), cpus * 10000);        System.out.println("finish!");    }    private static class Worker implements Runnable {        private final CountDownLatch begin;        private final CountDownLatch end;        private final Set<String> seqSet;        private final SeqGenerator seqGenerator;        public Worker(SeqGenerator seqGenerator, Set<String> seqSet, CountDownLatch begin, CountDownLatch end) {            this.seqGenerator = seqGenerator;            this.seqSet = seqSet;            this.begin = begin;            this.end = end;        }        @Override        public void run() {            try {                begin.await();                for (int i = 0; i < 10000; i++) {                    String seq = seqGenerator.getNextSeq("2");                    if (!seqSet.add(seq)) {                        System.out.println(seq);                        fail();                    }                }                System.out.println("end");            } catch (Exception e) {                e.printStackTrace();            } finally {                end.countDown();            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
redis队列及多线程应用
秒杀系统设计架构与实现
Spring Boot 使用 AOP 防止重复提交
利用redis + lua解决抢红包高并发的问题
Redis学习笔记1--入门篇 (转)
项目实战
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服