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);
    }

}
相关推荐
子兮曰5 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰5 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万5 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝5 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋5 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁5 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95275 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大5 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师5 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学5 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端