1.文章背景
突然客户改变需求,嫌弃系统中的id非常杂乱,要根据图片中的设计重新生成有规律的id,如图所示。
需求的大概意思是:这个单子的id由两位前缀+日期+流水三位号组成。每一天的单子id流水号从01开始。
2.利用redis的incr原子性实现该功能
1:面对的问题,并发,当系统出现并发时,生成的id会不会重复。id会不会如预期的一样准确拼接并自增。
2:代码实现
package com.hero.utils;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Component
public class AutoIdUtil {
private final RedisTemplate redisTemplate;
private static final String KEY_PREFIX = "autoid:";
public AutoIdUtil(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public String SmartAutoId(String type){
String key = KEY_PREFIX + type + DateUtil.format(new Date(), "yyyyMMdd");
int increment = 1;
long seconds = DateUtil.between(new Date(), DateUtil.endOfDay(new Date()), DateUnit.SECOND);
increment = Math.toIntExact(redisTemplate.opsForValue().increment(key,1));
redisTemplate.expire(key,seconds, TimeUnit.SECONDS);
if (increment < 100){
return type + DateUtil.format(new Date(), "yyyyMMdd") + String.format("%02d", increment);
}
return type + DateUtil.format(new Date(), "yyyyMMdd") + increment;
}
}
3:测试
在这里我就简单写了一个for循环,大家有兴趣的可以用jmeter测试一下并发情况
@Test
public void autoIdTest1(){
for (int i = 0; i <100; i++) {
String test = autoIdUtil.SmartAutoId("test");
System.out.println("当前时间:"+System.currentTimeMillis()+",id为:"+test);
}
}
测试结果如下
当前时间:1721540448335,id为:test2024072101
当前时间:1721540448402,id为:test2024072102
当前时间:1721540448428,id为:test2024072103
当前时间:1721540448463,id为:test2024072104
当前时间:1721540448486,id为:test2024072105
当前时间:1721540448551,id为:test2024072106
当前时间:1721540448575,id为:test2024072107
当前时间:1721540448598,id为:test2024072108
当前时间:1721540448657,id为:test2024072109
当前时间:1721540448723,id为:test2024072110
当前时间:1721540448781,id为:test2024072111
当前时间:1721540448803,id为:test2024072112
当前时间:1721540448863,id为:test2024072113
当前时间:1721540448929,id为:test2024072114
当前时间:1721540448999,id为:test2024072115
当前时间:1721540449058,id为:test2024072116
当前时间:1721540449080,id为:test2024072117
当前时间:1721540449138,id为:test2024072118
当前时间:1721540449195,id为:test2024072119
当前时间:1721540449219,id为:test2024072120
当前时间:1721540449275,id为:test2024072121
当前时间:1721540449333,id为:test2024072122
当前时间:1721540449356,id为:test2024072123
当前时间:1721540449413,id为:test2024072124
当前时间:1721540449434,id为:test2024072125
当前时间:1721540449457,id为:test2024072126
当前时间:1721540449479,id为:test2024072127
当前时间:1721540449540,id为:test2024072128
当前时间:1721540449563,id为:test2024072129
当前时间:1721540449619,id为:test2024072130
当前时间:1721540449641,id为:test2024072131
当前时间:1721540449663,id为:test2024072132
当前时间:1721540449688,id为:test2024072133
当前时间:1721540449710,id为:test2024072134
当前时间:1721540449733,id为:test2024072135
当前时间:1721540449792,id为:test2024072136
当前时间:1721540449815,id为:test2024072137
当前时间:1721540449873,id为:test2024072138
当前时间:1721540449894,id为:test2024072139
当前时间:1721540449952,id为:test2024072140
当前时间:1721540450010,id为:test2024072141
当前时间:1721540450034,id为:test2024072142
当前时间:1721540450091,id为:test2024072143
当前时间:1721540450148,id为:test2024072144
当前时间:1721540450171,id为:test2024072145
当前时间:1721540450228,id为:test2024072146
当前时间:1721540450287,id为:test2024072147
当前时间:1721540450309,id为:test2024072148
当前时间:1721540450367,id为:test2024072149
当前时间:1721540450424,id为:test2024072150
当前时间:1721540450447,id为:test2024072151
当前时间:1721540450506,id为:test2024072152
当前时间:1721540450562,id为:test2024072153
当前时间:1721540450586,id为:test2024072154
当前时间:1721540450643,id为:test2024072155
当前时间:1721540450667,id为:test2024072156
当前时间:1721540450691,id为:test2024072157
当前时间:1721540450713,id为:test2024072158
当前时间:1721540450779,id为:test2024072159
当前时间:1721540450838,id为:test2024072160
当前时间:1721540450860,id为:test2024072161
当前时间:1721540450883,id为:test2024072162
当前时间:1721540450906,id为:test2024072163
当前时间:1721540450929,id为:test2024072164
当前时间:1721540450952,id为:test2024072165
当前时间:1721540451009,id为:test2024072166
当前时间:1721540451032,id为:test2024072167
当前时间:1721540451054,id为:test2024072168
当前时间:1721540451113,id为:test2024072169
当前时间:1721540451170,id为:test2024072170
当前时间:1721540451193,id为:test2024072171
当前时间:1721540451250,id为:test2024072172
当前时间:1721540451309,id为:test2024072173
当前时间:1721540451331,id为:test2024072174
当前时间:1721540451387,id为:test2024072175
当前时间:1721540451446,id为:test2024072176
当前时间:1721540451467,id为:test2024072177
当前时间:1721540451526,id为:test2024072178
当前时间:1721540451584,id为:test2024072179
当前时间:1721540451606,id为:test2024072180
当前时间:1721540451664,id为:test2024072181
当前时间:1721540451742,id为:test2024072182
当前时间:1721540451802,id为:test2024072183
当前时间:1721540451860,id为:test2024072184
当前时间:1721540451882,id为:test2024072185
当前时间:1721540451903,id为:test2024072186
当前时间:1721540451927,id为:test2024072187
当前时间:1721540451949,id为:test2024072188
当前时间:1721540452009,id为:test2024072189
当前时间:1721540452066,id为:test2024072190
当前时间:1721540452089,id为:test2024072191
当前时间:1721540452112,id为:test2024072192
当前时间:1721540452135,id为:test2024072193
当前时间:1721540452157,id为:test2024072194
当前时间:1721540452180,id为:test2024072195
当前时间:1721540452239,id为:test2024072196
当前时间:1721540452262,id为:test2024072197
当前时间:1721540452285,id为:test2024072198
当前时间:1721540452342,id为:test2024072199
当前时间:1721540452407,id为:test20240721100