1,利用redis的原子类
一般都是 每天从1开始增长,生成的序列号带上日志就是唯一的了。

@Autowired
private RedisTemplate redisTemplate;

public String generateCodeString(String type) {
String date = DatePattern.PURE_DATE_FORMAT.format(new Date());
if (redisTemplate.getConnectionFactory() == null) {
return "";
}
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(date, redisTemplate.getConnectionFactory());
long l = redisAtomicLong.incrementAndGet();
if (l == 1) {
redisAtomicLong.expire(1, TimeUnit.DAYS);
}
return String.valueOf(l);
}