使用redis生成唯一編號及原理示例詳解
在系統(tǒng)開發(fā)中,保證數(shù)據(jù)的唯一性是至關重要的一件事,目前開發(fā)中常用的方式有使用數(shù)據(jù)庫的自增序列、UUID生成唯一編號、時間戳或者時間戳+隨機數(shù)等。
在某些特定業(yè)務場景中,可能會要求我們使用特定格式的唯一編號,比如我有一張訂單表(t_order),我需要生成“yewu(ORDER)+日期(yyyyMMdd)+序列號(00000000)”格式的訂單編號,比如今天的日期是20200716,那我今天第一個訂單號就是ORDER2020071600000001、第二個訂單號就是ORDER2020071600000002,明天的日期是20200717,那么明天的第一個訂單號就是ORDER2020071700000001、第二個訂單號就是ORDER2020071700000002,以此類推。
今天介紹下如何使用redis生成唯一的序列號,其實主要思想還是利用redis單線程的特性,可以保證操作的原子性,使讀寫同一個key時不會出現(xiàn)不同的數(shù)據(jù)。以SpringBoot項目為例,添加以下依賴。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>${spring.boot.version}</version> </dependency>
application.properties中配置redis,我本地redis沒有設置密碼,所以注釋了密碼這一行
server.port=9091 server.servlet.context-path=/ spring.redis.host=127.0.0.1 spring.redis.port=6379 #spring.redis.password=1234 spring.redis.database=0
創(chuàng)建SequenceService類用于生成特定業(yè)務編號
package com.xiaochun.service; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.support.atomic.RedisAtomicLong; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @Service public class SequenceService { private static Logger logger = LoggerFactory.getLogger(SequenceService.class); @Resource private RedisTemplate redisTemplate; //用作存放redis中的key private static String ORDER_KEY = "order_key"; //生成特定的業(yè)務編號,prefix為特定的業(yè)務代碼 public String getOrderNo(String prefix){ return getSeqNo(ORDER_KEY, prefix); } //SequenceService類中公用部分,傳入制定的key和prefix private String getSeqNo(String key, String prefix) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); //設置過期時間,這里設置為當天的23:59:59 Date expireDate = calendar.getTime(); //返回當前redis中的key的最大值 Long seq = generate(redisTemplate, key, expireDate); //獲取當天的日期,格式為yyyyMMdd String date = new SimpleDateFormat("yyyyMMdd").format(expireDate); //生成八為的序列號,如果seq不夠八位,seq前面補0, //如果seq位數(shù)超過了八位,那么無需補0直接返回當前的seq String sequence = StringUtils.leftPad(seq.toString(), 8, "0"); if (prefix == null) { prefix = ""; } //拼接業(yè)務編號 String seqNo = prefix + date + sequence; logger.info("KEY:{}, 序列號生成:{}, 過期時間:{}", key, seqNo, String.format("%tF %tT ", expireDate, expireDate)); return seqNo; } /** * @param key * @param expireTime <i>過期時間</i> * @return */ public static long generate(RedisTemplate<?,?> redisTemplate,String key,Date expireTime) { //RedisAtomicLong為原子類,根據(jù)傳入的key和redis鏈接工廠創(chuàng)建原子類 RedisAtomicLong counter = new RedisAtomicLong(key,redisTemplate.getConnectionFactory()); //設置過期時間 counter.expireAt(expireTime); //返回redis中key的值,內部實現(xiàn)下面詳細說明 return counter.incrementAndGet(); } }
接下來,啟動項目,使用接口的形式訪問,或者寫Test方法執(zhí)行,就可以得到諸如ORDER2020071600000001、ORDER2020071600000002的編號,而且在高并發(fā)環(huán)境中也不會出現(xiàn)數(shù)據(jù)重復的情況。實現(xiàn)原理:上面生成特定業(yè)務編號主要分為三部分,如下圖
前綴和日期部分,沒什么需要解釋的,主要是redis中的生成的序列號,而這需要依靠RedisAtomicLong來實現(xiàn),先看下上面生成redis序列過程中發(fā)生了什么
- 獲取redis中對應業(yè)務的key,生成過期時間expireTime
- 獲取了RedisTemplate對象,通過該對象獲取RedisConnectionFactory對象
- 將key,RedisConnectionFactory對象作為構造參數(shù)生成RedisAtomicLong對象,并設置過期時間
- 調用RedisAtomicLong的incrementAndGet()方法
看下RedisAtomicLong源碼,當然只放一部分源碼,不會放全部,RedisAtomicLong的結構,主要構造函數(shù),和上面提到過的incrementAndGet()方法
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> { private static final long serialVersionUID = 1L; //redis中的key,用volatile修飾,獲得原子性 private volatile String key; //當前的key-value對象,根據(jù)傳入的key獲取value值 private ValueOperations<String, Long> operations; //傳入當前redisTemplate對象,為RedisTemplate對象的頂級接口 private RedisOperations<String, Long> generalOps; public RedisAtomicLong(String redisCounter, RedisConnectionFactory factory) { this(redisCounter, (RedisConnectionFactory)factory, (Long)null); } private RedisAtomicLong(String redisCounter, RedisConnectionFactory factory, Long initialValue) { Assert.hasText(redisCounter, "a valid counter name is required"); Assert.notNull(factory, "a valid factory is required"); //初始化一個RedisTemplate對象 RedisTemplate<String, Long> redisTemplate = new RedisTemplate(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); redisTemplate.setExposeConnection(true); //設置當前的redis連接工廠 redisTemplate.setConnectionFactory(factory); redisTemplate.afterPropertiesSet(); //設置傳入的key this.key = redisCounter; //設置當前的redisTemplate this.generalOps = redisTemplate; //獲取當前的key-value集合 this.operations = this.generalOps.opsForValue(); //設置默認值,如果傳入為null,則key獲取operations中的value,如果value為空,設置默認值為0 if (initialValue == null) { if (this.operations.get(redisCounter) == null) { this.set(0L); } //不為空則設置為傳入的值 } else { this.set(initialValue); } } //將傳入key的value+1并返回 public long incrementAndGet() { return this.operations.increment(this.key, 1L); }
其實主要還是通過redis的自增序列來實現(xiàn)
到此這篇關于如何使用redis生成唯一編號及原理的文章就介紹到這了,更多相關redis生成唯一編號內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
RedisTemplate中boundHashOps的使用小結
redisTemplate.boundHashOps(key)?是 RedisTemplate 類的一個方法,本文主要介紹了RedisTemplate中boundHashOps的使用小結,具有一定的參考價值,感興趣的可以了解一下2024-04-04一次關于Redis內存詭異增長的排查過程實戰(zhàn)記錄
這篇文章主要給大家分享了一次關于Redis內存詭異增長的排查過程實戰(zhàn)記錄,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07CentOS7.5使用mysql_multi方式安裝MySQL5.7.28多實例(詳解)
這篇文章主要介紹了CentOS7.5使用mysql_multi方式安裝MySQL5.7.28多實例,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01