redission 分布式锁

<dependency>

<groupId>org.redisson</groupId>

<artifactId>redisson</artifactId>

<version>3.18.0</version> <!-- 与Spring Boot 2.7.18兼容的版本 -->

</dependency>

<!-- 如果使用redisson-spring-boot-starter -->

<dependency>

<groupId>org.redisson</groupId>

<artifactId>redisson-spring-boot-starter</artifactId>

<version>3.18.0</version>

</dependency>

虽然这2个依赖都可以,但是推荐使用第二个redisson-spring-boot-starter, 因为可以不用写如下配置类, 直接使用 RedissonClient redissonClient; 如下是定义自定义注解 和 aop切面编程,实现分布式定时任务, 只需要给 @

java 复制代码
@Slf4j
@Component
@Aspect
public class DistributdLockAspect {

    @Autowired
    private RedissonClient redissonClient;

    public static final String LOCK_PREFIX = "lock:";

    /**
     * distributedLock注解,之前抢锁, 注解方法执行之后释放锁
     * 因为 @Before通知的方法参数只能是 JoinPoint
     *  @Around方法才有 org.aspectj.lang.ProceedingJoinPoint 是 JoinPoint的子类
     *
     * @param joinPoint joinPoint
     * @param distributedLock distributedLock
     */
    @Around("@annotation(distributedLock)")
    public void action(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable {
        String lockKey = LOCK_PREFIX + distributedLock.value();
        RLock lock = redissonClient.getLock(lockKey);
        boolean isLocked = false;
        try {
            isLocked = lock.tryLock(0, 30,TimeUnit.SECONDS);
            if (isLocked) {
                log.info("获取分布式锁成功,开始执行定时任务: {}", lockKey);
                joinPoint.proceed();
            } else {
                log.info("未获取到分布式锁,跳过执行: {}", lockKey);
            }
        } finally {
            if (isLocked && lock.isHeldByCurrentThread()) {
                lock.unlock();
                log.info("释放分布式锁: {}", lockKey);
            }
        }
    }

}


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @Author wangxinle5
 * @since 2025-12-01
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {
    String value() default "";
}
java 复制代码
import java.util.List;
import java.util.stream.Collectors;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @Author wangxinle5
 * @since 2025-11-18
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.sentinel.master}")
    private String master;

    @Value("${spring.redis.database}")
    private Integer database;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.sentinel.nodes}")
    private List<String> sentinelNodes;

    @Bean
    public RedissonClient redissonClient() {
        List<String> nodes = sentinelNodes.stream().map(node -> "redis://" + node).collect(Collectors.toList());
        // 创建 Redis 哨兵配置
        Config config = new Config();
        // 配置哨兵集群模式
        config.useSentinelServers().setMasterName(master).addSentinelAddress(nodes.toArray(new String[0]))
            .setDatabase(database);
        // 只有密码不为空时才设置密码
        if (StringUtils.hasText(password)) {
            config.useSentinelServers().setPassword(password);
        }
        return Redisson.create(config);
    }

}
相关推荐
爱喝白开水a1 分钟前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
董世昌412 分钟前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
吃杠碰小鸡1 小时前
高中数学-数列-导数证明
前端·数学·算法
kingwebo'sZone1 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
独断万古他化1 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密
xjt_09011 小时前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js
我是伪码农2 小时前
Vue 2.3
前端·javascript·vue.js
夜郎king2 小时前
HTML5 SVG 实现日出日落动画与实时天气可视化
前端·html5·svg 日出日落
夏幻灵3 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_3 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js