分布式锁实现

package com.zmc.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

@Component
public class RandomNumUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 随机数生成
     *
     * @param len 随机数长度
     * @return
     */
    public String gen(int len) {
        if (len < 2) {
            throw new RuntimeException("随机数长度不能低于两位");
        }
        StringBuffer seqNum=new StringBuffer();
        while (true) {
            //生成指定长度随机数
            double pow = Math.pow(10, len);
            int num = (int) (Math.random() * pow);
            seqNum.append(num);
            //生成的随机数小于指定长度则在前方加0
            while (seqNum.length()

你可能感兴趣的:(分布式)