这篇文章主要介绍了spring boot如何集成redisson的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇spring boot如何集成redisson文章都会有所收获,下面我们一起来看看吧。
redisson支持redis环境,单机、集群、哨兵、云等。
这里就讲一下集群模式需要注意的地方,redisson启动会检测master/slave节点是否正常,一般来说3分片3主3从是没有什么问题的,但是如果测试环境1分片1主1从或者3主都是启动不了的。
除了环境需要注意,还有注意兼容有无密码的情况。
一般情况下,生产环境都是有密码的。有密码的话,建议手动注入redisson配置,不用spring boot来帮你集成,因为可能spring 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;
}
}
上面可以根据自己实际情况调优一些配置。
当然除了密码需要注意,还有一点就是是否有ssl,目前所在company是用的亚马逊云,会有ssl
spring boot 兼容 redis 可以在yaml配置里天际: Ssl:true,但对于redisson来说添加前缀就可以啦:
rediss:// + ip:端口或者域名
spring:
redis:
cluster:
nodes: rediss://clustercfg.xxx
password: 'xxx'
timeout: 30000
Ssl: true
lettuce:
pool:
max-idle: 100
利用锁的互斥策略,一开始这样的
@Scheduled(cron = "${xxx:0 0 */2 * * ?}")
public void createProcess() {
RLock lock = redisson.getLock(key);
try {
if (lock.tryLock()) {
// 执行运行程序
} else {
log.info("createProcess 获取锁失败");
}
} catch (Exception e) {
log.error("xxx", e);
} finally {
// 是否有锁 && 是否当前线程
if (lock != null && lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
咋一看是没有什么问题的,但是本次重构的定时任务比较多,因此会涉及到很多try catch相同的代码。
解决重复代码方式之一就是封装,在就是注解切面,想到注解方式更加灵活
于是
/**
* Redisson 同步锁
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RedissonSyncLock {
String pref();
/**
* 锁后缀,一般前缀根据业务来定,后缀是具体的场景
*/
String keyEL();
/**
* 等待时长 【需要区分是互斥还是阻塞,互斥默认0就可以】
*/
int waitSec() default 0;
}
@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){
// 程序执行
}
的确使用起来是比较方便的。
关于“spring boot如何集成redisson”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“spring boot如何集成redisson”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。