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 #連接超時時間 lettuce: pool: #連接池最大連接數(shù) max-active: 8 #最大阻塞等待時間(負數(shù)表示沒限制) max-wait: 1 #連接池最大空閑連接 max-idle: 5 #連接池最小空閑連接 min-idle: 0 #單機模式 # database: 0 # 集群模式該參數(shù)不生效 # host: 127.0.0.1 # port: 6379 #集群模式開啟 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:
測試驗證
調(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); } }
由于此工具類需要放在 common包,所以在調(diào)用系統(tǒng)上新增 MybatisPlusConfig,在項目啟動時候,將bean(stringRedisTemplate)送進到CustomIdGenerator中(我是這樣理解的,可能不能這么解釋)
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ā)電機 * * @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; } /** * 年月日時分秒 +6位 redis返回的自增序列(如000001、000002、000003...) * redis返回的自增序列 規(guī)則: * 根據(jù)傳入的 key(相當(dāng)于字段名)生成自增的序列,2s后重新自增; * 由于redis的incr原子性,也能保證每次返回的結(jié)果不會出現(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; } }
測試驗證
開始10個線程,跑1000次:
結(jié)果如下,2s可以照常跑1000條完無重復(fù)值
改成10000試試, 還是跑到了 10000,原來是 每一條線程進去后,走這個代碼 counter.expire(KEY_EXPIRE_TIME, TimeUnit.SECONDS);將這個key的失效時間重新設(shè)置了一下,不過2s生成100000條已經(jīng)夠用了,大家可以根據(jù)系統(tǒng)業(yè)務(wù),自定義縮短或增加失效時間;
到此這篇關(guān)于springboot集成redis并使用redis生成全局唯一索引ID的文章就介紹到這了,更多相關(guān)springboot redis生成全局唯一索引ID內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot教程之利用ActiveMQ實現(xiàn)延遲消息
這篇文章主要給大家介紹了關(guān)于Spring Boot教程之利用ActiveMQ實現(xiàn)延遲消息的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Springboot之@Async不執(zhí)行原因及分析
這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09java開發(fā)之spring webflow實現(xiàn)上傳單個文件及多個文件功能實例
這篇文章主要介紹了java開發(fā)之spring webflow實現(xiàn)上傳單個文件及多個文件功能,結(jié)合具體實例形式分析了spring webflow文件上傳具體操作技巧,需要的朋友可以參考下2017-11-11Spring中XmlWebApplicationContext的實現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個重要類,本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價值,感興趣的可以了解一下2024-08-08SpringBoot使用thymeleaf實現(xiàn)前端表格
雖然現(xiàn)在流行前后端分離,但是后端模版在一些關(guān)鍵地方還是非常有用的,例如郵件模版、代碼模版等。當(dāng)然也不排除一些古老的項目后端依然使用動態(tài)模版。Thymeleaf 簡潔漂亮、容易理解,并且完美支持 HTML5,可以直接打開靜態(tài)頁面,同時不新增標簽,只需增強屬性2022-10-10