REDIS生成分布式环境下自增ID

相信大家在分布式系统一定会遇到如何生成唯一Id; 

uuid 可以但是,作为数据库主键很浪费性能(索引相关),

雪花算法可以, 但是很依赖于环境.

这里推荐大家使用redis生成;


1. 需求明确

我们生成的Id组成为, 时间戳+ 自增Id, 补0 

例如: 今天是2020.2.28 ,  补6个0 , 那么生成的id范围是20200228000001-202002289999999

2.技术选型

Redis中的RedisAtomicLong 具备原子性, 可以通过其incr方法进行递增.

2. ID生成类

 
package com.dyhospital.cloudhis.common.redis.util;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.support.atomic.RedisAtomicLong;

import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.concurrent.TimeUnit;


/**

* Description: 分布式Id生成工厂

* User: zhouzhou

* Date: 2020-02-28

* Time: 10:14

*/

@Component

public class IdGeneratorFactory {


@Autowired

RedisTemplate<String, String> redisTemplate;


/**

* @param key

* @param value

* @param expireTime

* @Title: set

* @Description: set cache.

*/

public void set(String key, int value, Date expireTime) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

counter.set(value);

counter.expireAt(expireTime);

}


/**

* @param key

* @param value

* @param timeout

* @param unit

* @Title: set

* @Description: set cache.

*/

public void set(String key, int value, long timeout, TimeUnit unit) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

counter.set(value);

counter.expire(timeout, unit);

}


/**

* @param key

* @return

* @Title: generate

* @Description: Atomically increments by one the current value.

*/

public long generate(String key) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

return counter.incrementAndGet();

}


/**

* @param key

* @return

* @Title: generate

* @Description: Atomically increments by one the current value.

*/

public long generate(String key, Date expireTime) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

counter.expireAt(expireTime);

return counter.incrementAndGet();

}


/**

* @param key

* @param increment

* @return

* @Title: generate

* @Description: Atomically adds the given value to the current value.

*/

public long generate(String key, int increment) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

return counter.addAndGet(increment);

}


/**

* @param key

* @param increment

* @param expireTime

* @return

* @Title: generate

* @Description: Atomically adds the given value to the current value.

*/

public long generate(String key, int increment, Date expireTime) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

counter.expireAt(expireTime);

return counter.addAndGet(increment);

}


/**

* 根据业务key, 长度, 获取key, 以日期作为前缀

* <p>

* key = order, length = 5 ,当天日期2050年1月1日

* 结果: 2050010100001

* </p>

* @param key

* @param length

* @return

*/

public String generateIdByToday(String key, Integer length) {

RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());


long num = counter.incrementAndGet();

counter.expireAt(getTodayEndTime());

String id = getToday() + String.format("%0" + length + "d", num);

return id;


}



/**

* @Title: getTodayEndTime 获取今日最后的时间

* @Description: Get the cache expire time.

* @return

*/

public static Date getTodayEndTime() {

Calendar todayEnd = Calendar.getInstance();

todayEnd.set(Calendar.HOUR_OF_DAY, 23);

todayEnd.set(Calendar.MINUTE, 59);

todayEnd.set(Calendar.SECOND, 59);

todayEnd.set(Calendar.MILLISECOND, 999);

return todayEnd.getTime();

}


/**

* @Title: getTodayEndTime 今天的日期格式: 20190101

* @Description: Get the cache expire time.

* @return

*/

public static String getToday() {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

return sdf.format(new Date());

}


}

3. 我们来测试下

测试类如下:


// --------------------- 分布式自增Id 测试 ---------------------------

@ApiOperation("根据key生成自增Id")

@RequestMapping(value = "/getId/{key}", method = RequestMethod.GET)

public GenericResponse<Long> getId(@PathVariable("key") String key) {

logger.info("生成Id中");

long generate = idGeneratorFactory.generate(key, IdGeneratorFactory.getTodayEndTime());

return new GenericResponse<>(generate);

}


@ApiOperation("根据key生成自增Id,日期版")

@RequestMapping(value = "/getIdByToday/{key}", method = RequestMethod.GET)

public GenericResponse<String> getIdByToday(@PathVariable("key") String key) {

logger.info("生成Id中");

String id = idGeneratorFactory.generateIdByToday(key, 6);

return new GenericResponse<>(id);

}

测试结果如下: 

 

要使用Redis生成自增ID,可以借助Redis的INCR命令。INCR命令用于将一个键的值增加1,并返回增加后的值。你可以在你的代码中使用INCR命令来生成自增ID。下面是一个示例代码片段: ``` @ApiOperation("根据key生成自增Id") @RequestMapping(value = "/getId/{key}", method = RequestMethod.GET) public GenericResponse<Long> getId(@PathVariable("key") String key) { logger.info("生成Id中"); Long id = redisTemplate.opsForValue().increment(key, 1); return new GenericResponse<>(id); } ``` 在这个示例中,我们使用了Spring Data Redis提供的`redisTemplate`来执行INCR命令,通过操作键的值来实现自增。当调用这个接口时,每次都会生成一个自增ID,并返回给用户。你可以根据实际需求对生成ID进行格式化,比如添加日期前缀或补0等操作。 需要注意的是,为了使用Redis生成自增ID,你需要先确保已经正确地配置了Redis服务器,并在代码中引入相关的依赖。此外,你还可以使用Redis的其他功能,比如设置过期时间等,来满足自己的业务需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [REDIS生成分布式环境自增ID](https://blog.csdn.net/yucaifu1989/article/details/122132257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [毕设项目:基于SpringBoot+MyBatis-Plus 前后端分离的影院选座购票系统.zip](https://download.csdn.net/download/dd_vision/88222307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值