springboot如何使用redis的incr創(chuàng)建分布式自增id
使用redis的incr創(chuàng)建分布式自增id
測試使用springboot加載類測試,使用本地redis, 模擬多線程去生成規(guī)律的自增id
@Component
public class Common implements CommandLineRunner {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
?
? ? @Override
? ? public void run(String... args) throws Exception {
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? Thread thread1 = new Thread(new Test1());
? ? ? ? Thread thread2 = new Thread(new Test1());
? ? ? ? Thread thread3 = new Thread(new Test1());
? ? ? ? thread1.start();
? ? ? ? thread2.start();
? ? ? ? thread3.start();
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("耗時:"+(end-start));
? ? }
?
? ? class Test1 implements Runnable{
?
? ? ? ? @Override
? ? ? ? public void run() {
? ? ? ? ? ? getId();
?
? ? ? ? }
?
? ? }
?
? ? public void getId(){
? ? ? ? synchronized (this) {
? ? ? ? ? ? RedisAtomicLong entityIdCounter = null;
?
? ? ? ? ? ? for(int i=0;i<10;i++){
? ? ? ? ? ? ? ? if(!redisTemplate.hasKey("ceid")){
? ? ? ? ? ? ? ? ? ? redisTemplate.opsForValue().increment("ceid", 1);
? ? ? ? ? ? ? ? ? ? System.out.println("test1使用redis緩存保存數(shù)據(jù)成功");
? ? ? ? ? ? ? ? ? ? entityIdCounter= new RedisAtomicLong("ceid", redisTemplate.getConnectionFactory());
? ? ? ? ? ? ? ? ? ? //incr 默認(rèn)初始值從0開始,
? ? ? ? ? ? ? ? ? ? //可以設(shè)置初始值,
? ? ? ? ? ? ? ? ? ? entityIdCounter.set(123);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? entityIdCounter=new RedisAtomicLong("ceid", redisTemplate.getConnectionFactory());
? ? ? ? ? ? ? ? long increment=0;
?
? ? ? ? ? ? ? ? increment = entityIdCounter.incrementAndGet();
? ? ? ? ? ? ? ? if (i == 5) {
? ? ? ? ? ? ? ? ? ? increment = entityIdCounter.decrementAndGet();
? ? ? ? ? ? ? ? ? ? System.out.println("test1:失敗,返回上個id");
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? System.out.println(increment);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? }
? ? }
?
}redis配置在application.properties中
# REDIS # Redis數(shù)據(jù)庫索引(默認(rèn)為0) spring.redis.database=0 ? # Redis服務(wù)器地址 (默認(rèn)為127.0.0.1) spring.redis.host=127.0.0.1 # Redis服務(wù)器連接端口 (默認(rèn)為6379) spring.redis.port=6379 ? # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password= ? # 連接超時時間(毫秒) spring.redis.timeout=2000ms
springboot redis自增編號控制 踩坑
近段期間,公司 接手一個訂單號生成服務(wù),規(guī)則的話已經(jīng)由項目經(jīng)理他們規(guī)定好了,主要是后面的四位數(shù)代表的關(guān)于當(dāng)前訂單號已經(jīng)執(zhí)行第幾個了。而這里面有一個要求就是支持分布式。
為了實現(xiàn)這個東西,剛開始我使用了redis的incr來解決這個問題,因為我們后端開發(fā)用的是Spring boot,所以我網(wǎng)上找了一個代碼如下:
/**
*
* @param key
* @param liveTime
* @return
*/
public Long incr(String key, long liveTime) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
Long increment = entityIdCounter.getAndIncrement();
if ((null == increment || increment.longValue() == 0) && liveTime > 0) {//初始設(shè)置過期時間
entityIdCounter.expire(liveTime, TimeUnit.SECONDS);
}
return increment;
}結(jié)果測試的時候,看著后面的數(shù)很滿意,心里面有點小小的激動哦~~
但是當(dāng)我將數(shù)據(jù)從小到大排序的時候,發(fā)現(xiàn)了一點異樣,即剛開始的幾個是存在問題的。
所以通過測試發(fā)現(xiàn)了,當(dāng)redis里面還沒有設(shè)置計時器的一剎那,分布式服務(wù)下,會存在前幾個重復(fù)的現(xiàn)象。

發(fā)現(xiàn)這個問題之后,于是我通過redis鎖,當(dāng)判斷redis下面還沒存在計數(shù)key的情況下,鎖住,然后在鎖住的情況下,其他人進來調(diào)用的時候,線程睡眠500ms,然后再往下執(zhí)行。順利解決~~~
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中dubbo+zookeeper實現(xiàn)分布式開發(fā)的應(yīng)用詳解
- 2020最新IDEA SpringBoot整合Dubbo的實現(xiàn)(zookeeper版)
- SpringBoot系列教程之dubbo和Zookeeper集成方法
- 淺談Java(SpringBoot)基于zookeeper的分布式鎖實現(xiàn)
- SpringBoot整合XxlJob分布式任務(wù)調(diào)度平臺
- SpringBoot?2.5.5整合輕量級的分布式日志標(biāo)記追蹤神器TLog的詳細(xì)過程
- SpringBoot集成redis與session實現(xiàn)分布式單點登錄
- springboot 使用zookeeper實現(xiàn)分布式隊列的基本步驟
相關(guān)文章
解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
JAVA中跳出當(dāng)前多重嵌套循環(huán)的方法詳解
今天在看面試題時,發(fā)現(xiàn)了這個問題,因為在PHP中跳出多次循環(huán)可以使用break數(shù)字來跳出多層循環(huán),但這在java中并不好使,這篇文章主要給大家介紹了關(guān)于JAVA中跳出當(dāng)前多重嵌套循環(huán)的相關(guān)資料,需要的朋友可以參考下2022-01-01
java 數(shù)據(jù)結(jié)構(gòu)中棧結(jié)構(gòu)應(yīng)用的兩個實例
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)中棧結(jié)構(gòu)應(yīng)用的兩個實例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Mybatis-plus selectByMap條件查詢方式
這篇文章主要介紹了Mybatis-plus selectByMap條件查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Java?設(shè)計模式以虹貓藍兔的故事講解簡單工廠模式
簡單工廠模式是屬于創(chuàng)建型模式,又叫做靜態(tài)工廠方法(Static Factory Method)模式,但不屬于23種GOF設(shè)計模式之一。簡單工廠模式是由一個工廠對象決定創(chuàng)建出哪一種產(chǎn)品類的實例。簡單工廠模式是工廠模式家族中最簡單實用的模式,可以理解為是不同工廠模式的一個特殊實現(xiàn)2022-03-03
詳解spring boot實現(xiàn)websocket
這篇文章主要介紹了詳解spring boot實現(xiàn)websocket,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
java發(fā)送form-data請求實現(xiàn)文件上傳的示例代碼
最近做一個需求,需要請求第三方接口上傳文件,該請求類型是form-data請求,本文就來介紹一下java發(fā)送form-data請求實現(xiàn)文件上傳的示例代碼,感興趣的可以了解一下2023-12-12

