前言
分布式ID生成策略有多种,各有利弊。这里记录下在工作中我结合Redis在Spring架构体系中使用雪花算法生成分布式ID的方式。
一、代码部分
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author sungm
* @since 2021-11-06 21:16
*/
@Slf4j
@Service
public class SnowflakeManager {
/** 开始时间戳: 2020-01-01 00:00:00 */
private static final Long START_TIMESTAMP = 1577808000000L;
/** 12位最大序号: 2^12 - 1 */