springboot集成redis并使用redis生成全局唯一索引ID
部署redis
Windows下搭建Reids本地集群,可參考http://chabaoo.cn/article/242520.htm
springboot集成 redis
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>application.yaml文件
spring:
#redis 緩存
redis:
connect-timeout: 180000 #連接超時(shí)時(shí)間
lettuce:
pool:
#連接池最大連接數(shù)
max-active: 8
#最大阻塞等待時(shí)間(負(fù)數(shù)表示沒(méi)限制)
max-wait: 1
#連接池最大空閑連接
max-idle: 5
#連接池最小空閑連接
min-idle: 0
#單機(jī)模式
# database: 0 # 集群模式該參數(shù)不生效
# host: 127.0.0.1
# port: 6379
#集群模式開(kāi)啟
cluster:
nodes: 127.0.0.1:6379,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005
max-redirects: 3
password:測(cè)試驗(yàn)證
調(diào)用該接口,返回 22,則集成redis成功;

redis生成全局唯一索引ID
使用redis的RedisAtomicLong可以生成分布式自增的ID值;直接上代碼:
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.wd.basic.common.support.component.CustomIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
/**
* mybatis 配置
*
* @author 上官婉兒
* @date 2022/03/21
*/
@Slf4j
@Configuration
public class MybatisPlusConfig {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Bean
public IdentifierGenerator idGenerator() {
return new CustomIdGenerator(stringRedisTemplate);
}
}由于此工具類(lèi)需要放在 common包,所以在調(diào)用系統(tǒng)上新增 MybatisPlusConfig,在項(xiàng)目啟動(dòng)時(shí)候,將bean(stringRedisTemplate)送進(jìn)到CustomIdGenerator中(我是這樣理解的,可能不能這么解釋?zhuān)?/p>
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.wd.basic.common.exception.BasicException;
import com.wd.basic.common.exception.enums.IDGeneratorExceptionEnum;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 自定義id發(fā)電機(jī)
*
* @author 上官婉兒
* @date 2022/03/21
*/
public class CustomIdGenerator {
private static final String I_KEY_PREFIX = "IKey:generator";
public static final int KEY_EXPIRE_TIME = 2;
private final StringRedisTemplate stringRedisTemplate;
public CustomIdGenerator(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
/**
* 年月日時(shí)分秒 +6位 redis返回的自增序列(如000001、000002、000003...)
* redis返回的自增序列 規(guī)則:
* 根據(jù)傳入的 key(相當(dāng)于字段名)生成自增的序列,2s后重新自增;
* 由于redis的incr原子性,也能保證每次返回的結(jié)果不會(huì)出現(xiàn)相同的值,
*/
@Override
public String nextUUID(Object entity) {
String bizKey = entity.getClass().getName();
String dateStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_FORMATTER);
RedisAtomicLong counter = new RedisAtomicLong(I_KEY_PREFIX + bizKey, Objects.requireNonNull(stringRedisTemplate.getConnectionFactory()));
counter.expire(KEY_EXPIRE_TIME, TimeUnit.SECONDS);
long redisId = counter.incrementAndGet();
String redisIdStr = StrUtil.fillBefore(String.valueOf(redisId), '0',6);
return dateStr + redisIdStr;
}
}測(cè)試驗(yàn)證
開(kāi)始10個(gè)線(xiàn)程,跑1000次:

結(jié)果如下,2s可以照常跑1000條完無(wú)重復(fù)值

改成10000試試, 還是跑到了 10000,原來(lái)是 每一條線(xiàn)程進(jìn)去后,走這個(gè)代碼 counter.expire(KEY_EXPIRE_TIME, TimeUnit.SECONDS);將這個(gè)key的失效時(shí)間重新設(shè)置了一下,不過(guò)2s生成100000條已經(jīng)夠用了,大家可以根據(jù)系統(tǒng)業(yè)務(wù),自定義縮短或增加失效時(shí)間;
到此這篇關(guān)于springboot集成redis并使用redis生成全局唯一索引ID的文章就介紹到這了,更多相關(guān)springboot redis生成全局唯一索引ID內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中匿名類(lèi)的兩種實(shí)現(xiàn)方式
本文主要介紹了Java中匿名類(lèi)的兩種實(shí)現(xiàn)方式。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
Spring Boot教程之利用ActiveMQ實(shí)現(xiàn)延遲消息
這篇文章主要給大家介紹了關(guān)于Spring Boot教程之利用ActiveMQ實(shí)現(xiàn)延遲消息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Springboot之@Async不執(zhí)行原因及分析
這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
java開(kāi)發(fā)之spring webflow實(shí)現(xiàn)上傳單個(gè)文件及多個(gè)文件功能實(shí)例
這篇文章主要介紹了java開(kāi)發(fā)之spring webflow實(shí)現(xiàn)上傳單個(gè)文件及多個(gè)文件功能,結(jié)合具體實(shí)例形式分析了spring webflow文件上傳具體操作技巧,需要的朋友可以參考下2017-11-11
java代碼實(shí)現(xiàn)斗地主發(fā)牌功能
這篇文章主要介紹了java實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Spring中XmlWebApplicationContext的實(shí)現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個(gè)重要類(lèi),本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
SpringBoot使用thymeleaf實(shí)現(xiàn)前端表格
雖然現(xiàn)在流行前后端分離,但是后端模版在一些關(guān)鍵地方還是非常有用的,例如郵件模版、代碼模版等。當(dāng)然也不排除一些古老的項(xiàng)目后端依然使用動(dòng)態(tài)模版。Thymeleaf 簡(jiǎn)潔漂亮、容易理解,并且完美支持 HTML5,可以直接打開(kāi)靜態(tài)頁(yè)面,同時(shí)不新增標(biāo)簽,只需增強(qiáng)屬性2022-10-10
java使用反射創(chuàng)建并操作對(duì)象的方法
這篇文章主要介紹了java使用反射創(chuàng)建并操作對(duì)象的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
SpringCloud Bus如何實(shí)現(xiàn)配置刷新
這篇文章主要介紹了SpringCloud Bus如何實(shí)現(xiàn)配置刷新,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

