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識(shí)別不了密碼。
@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)文章
詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
使用c3p0連接數(shù)據(jù)庫實(shí)現(xiàn)增刪改查
這篇文章主要為大家詳細(xì)介紹了使用c3p0連接數(shù)據(jù)庫實(shí)現(xiàn)增刪改查,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Java socket通信模擬QQ實(shí)現(xiàn)多人聊天室
Socket在Java實(shí)戰(zhàn)網(wǎng)絡(luò)通信編程應(yīng)用中有非常重要的作用,你想要跟別人聯(lián)系都得通過socket占據(jù)端口來實(shí)現(xiàn),掌握Socket技術(shù)不僅在聊天應(yīng)用程序中需要用到(比如QQ什么的都都是用socket來寫的),而且對于學(xué)習(xí) Asp.net 也非常有幫助2022-07-07
idea中自動(dòng)生成Java類圖和時(shí)序圖的圖文教程
本文主要介紹了idea中自動(dòng)生成Java類圖和時(shí)序圖的圖文教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
spring整合JMS實(shí)現(xiàn)同步收發(fā)消息(基于ActiveMQ的實(shí)現(xiàn))
本篇文章主要介紹了spring整合JMS實(shí)現(xiàn)同步收發(fā)消息(基于ActiveMQ的實(shí)現(xiàn)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

