spring boot優(yōu)雅集成redisson詳解
集成及注意事項(xiàng)
上一篇文章大白話說了一下redisson的可重入、可續(xù)約、阻塞、時(shí)間輪、紅鎖、聯(lián)鎖、加鎖邏輯和解鎖邏輯,如果大家有興趣先看上一篇,直通車
拔劍起蒿萊????????
redisson支持redis環(huán)境,單機(jī)、集群、哨兵、云等。
這里就講一下集群模式需要注意的地方,redisson啟動(dòng)會(huì)檢測master/slave節(jié)點(diǎn)是否正常,一般來說3分片3主3從是沒有什么問題的,但是如果測試環(huán)境1分片1主1從或者3主都是啟動(dòng)不了的。
除了環(huán)境需要注意,還有注意兼容有無密碼的情況。
手動(dòng)注入redisson配置
一般情況下,生產(chǎn)環(huán)境都是有密碼的。有密碼的話,建議手動(dòng)注入redisson配置,不用spring boot來幫你集成,因?yàn)榭赡躶pring boot識別不了密碼。
@Configuration public class RedissonConfiguration { @Value("${spring.redis.cluster.nodes}") private String node; @Value("${spring.redis.password:}") private String password; @Bean public RedissonClient redissonClient() { Config config = new Config(); String[] nodes = node.split(","); ClusterServersConfig clusterServersConfig = config.useClusterServers(); for (String nodeAddress : nodes) { clusterServersConfig.addNodeAddress(prefixAddress(nodeAddress)); } if (StringUtils.isNotBlank(password)) { clusterServersConfig.setPassword(password); } return Redisson.create(config); } private String prefixAddress(String address) { if (!StringUtils.isBlank(address) && !address.startsWith("redis")) { return "redis://" + address; } return address; } }
上面可以根據(jù)自己實(shí)際情況調(diào)優(yōu)一些配置。見官網(wǎng)
當(dāng)然除了密碼需要注意,還有一點(diǎn)就是是否有ssl,目前所在company是用的亞馬遜云,會(huì)有ssl
spring boot 兼容 redis 可以在yaml配置里天際: Ssl:true,但對于redisson來說添加前綴就可以啦:
rediss:// + ip:端口或者域名
具體yaml配置
spring: redis: cluster: nodes: rediss://clustercfg.xxx password: 'xxx' timeout: 30000 Ssl: true lettuce: pool: max-idle: 100
愿君學(xué)長松 慎勿作桃李????????
既然注意點(diǎn)講完了,那么在講下怎么使用吧
利用鎖的互斥策略,想知道有鎖的那些策略,見上一篇文章。
一開始這樣的
@Scheduled(cron = "${xxx:0 0 */2 * * ?}") public void createProcess() { RLock lock = redisson.getLock(key); try { if (lock.tryLock()) { // 執(zhí)行運(yùn)行程序 } else { log.info("createProcess 獲取鎖失敗"); } } catch (Exception e) { log.error("xxx", e); } finally { // 是否有鎖 && 是否當(dāng)前線程 if (lock != null && lock.isLocked() && lock.isHeldByCurrentThread()) { lock.unlock(); } } }
咋一看是沒有什么問題的,但是本次重構(gòu)的定時(shí)任務(wù)比較多,因此會(huì)涉及到很多try catch相同的代碼。
注解方式
解決重復(fù)代碼方式之一就是封裝,在就是注解切面,想到注解方式更加靈活
于是
/** * Redisson 同步鎖 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface RedissonSyncLock { String pref(); /** * 鎖后綴,一般前綴根據(jù)業(yè)務(wù)來定,后綴是具體的場景 */ String keyEL(); /** * 等待時(shí)長 【需要區(qū)分是互斥還是阻塞,互斥默認(rèn)0就可以】 */ int waitSec() default 0; }
需要一個(gè)切面
@Slf4j @Aspect @Component @RequiredArgsConstructor public class RedissonSyncLockAspect { private final Redisson redisson; @Around(value = "@annotation(xxx.RedissonSyncLock)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); RedissonSyncLock redissonSyncLock = signature.getMethod().getAnnotation(RedissonSyncLock.class); Object[] args = joinPoint.getArgs(); String key = SpelUtil.parseSpel(signature.getMethod(), args, redissonSyncLock.keyEL()); RLock lock = null; try { if (StringUtils.isNotBlank(key) && !StringUtils.equals(key, "null") lock = redisson.getLock(redissonSyncLock.pref().concat(key)); if (lock.tryLock(redissonSyncLock.waitSec(), TimeUnit.SECONDS)) { return joinPoint.proceed(); } } log.info("RedissonSyncLockAspect 上鎖失敗 {}", key); } finally { if (lock != null && lock.isLocked() && lock.isHeldByCurrentThread()) { lock.unlock(); } } } }
使用方法:
@RedissonSyncLock(pref = KeyConstant.xxx, keyEL = "#bean.accountNo") private void xxx(Bean bean){ // 程序執(zhí)行 }
的確使用起來是比較方便的。
以上就是spring boot優(yōu)雅集成redisson詳解的詳細(xì)內(nèi)容,更多關(guān)于spring boot集成redisson的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Cloud中各組件超時(shí)總結(jié)
在大家學(xué)習(xí)spring cloud的時(shí)候組件是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于Spring Cloud中各組件超時(shí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11Java中使用DOM4J生成xml文件并解析xml文件的操作
這篇文章主要介紹了Java中使用DOM4J來生成xml文件和解析xml文件的操作,今天通過代碼給大家展示了解析xml文件和生成xml文件的方法,需要的朋友可以參考下2021-09-09Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行
本文主要介紹了Springboot項(xiàng)目啟動(dòng)成功后可通過五種方式繼續(xù)執(zhí)行,主要包括CommandLineRunner接口,ApplicationRunner接口,ApplicationListener接口,@PostConstruct注解,InitalizingBean接口,感興趣的可以了解一下2023-12-12Java實(shí)現(xiàn)添加文字水印&圖片水印的方法詳解
為圖片添加水印的主要作用是保護(hù)圖片版權(quán),防止圖片被未經(jīng)授權(quán)的人使用或傳播。本文為大家介紹了Java實(shí)現(xiàn)添加文字水印&圖片水印的具體方法,需要的可以參考一下2023-02-02