IT袋

当前位置:主页 > 经验教程 > 建站编程 >

SpringBoot

SpringBoot Redis分布式锁的正确实现方式(4)

时间:2024-01-24 18:50:11 来源:IT袋 作者:马勇
导读:SpringBoot,把分布式锁接口定义出来,所谓面向接口和对象编程,代码如有神。顺带用英文显摆下什么叫做专业。 /** * 分布式锁 */public interface Lock { /** * Tries to acqu

SpringBoot

把分布式锁接口定义出来,所谓面向接口和对象编程,代码如有神。顺带用英文显摆下什么叫做专业。

/**
 * 分布式锁
 */
public interface Lock {
    /**
     * Tries to acquire the lock with defined <code>leaseTime</code>.
     * Waits up to defined <code>waitTime</code> if necessary until the lock became available.
     * <p>* Lock will be released automatically after defined <code>leaseTime</code> interval.
     *
     * @param waitTime  the maximum time to acquire the lock
     * @param leaseTime lease time
     * @param unit      time unit
     * @return <code>true</code> if lock is successfully acquired,
     * otherwise <code>false</code> if lock is already set.
     * @throws InterruptedException - if the thread is interrupted
     */
    boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
    /**
     * Acquires the lock with defined <code>leaseTime</code>.
     * Waits if necessary until lock became available.
     * <p>* Lock will be released automatically after defined <code>leaseTime</code> interval.
     *
     * @param leaseTime the maximum time to hold the lock after it's acquisition,
     *                  if it hasn't already been released by invoking <code>unlock</code>.
     *                  If leaseTime is -1, hold the lock until explicitly unlocked.
     * @param unit      the time unit
     */
    void lock(long leaseTime, TimeUnit unit);
    /**
     * Releases the lock.
     *
     * <p><b>Implementation Considerations</b>
     *
     * <p>A {@code Lock} implementation will usually impose
     * restrictions on which thread can release a lock (typically only the
     * holder of the lock can release it) and may throw
     * an (unchecked) exception if the restriction is violated.
     * Any restrictions and the exception
     * type must be documented by that {@code Lock} implementation.
     */
    void unlock();
}

青铜分布式锁实战

DistributedLock 实现 Lock 接口,构造方法实现 resourceName 和 StringRedisTemplate 的属性设置。

客户端唯一标识使用uuid:threadId 组成。

DistributedLock

public class DistributedLock implements Lock {
    /**
     * 标识 id
     */
    private final String id = UUID.randomUUID().toString();
    /**
     * 资源名称
     */
    private final String resourceName;
    private final List<String> keys = new ArrayList<>(1);
    /**
     * redis 客户端
     */
    private final StringRedisTemplate redisTemplate;
    public DistributedLock(String resourceName, StringRedisTemplate redisTemplate) {
        this.resourceName = resourceName;
        this.redisTemplate = redisTemplate;
        keys.add(resourceName);
    }
    private String getRequestId(long threadId) {
        return id + ":" + threadId;
    }
}

相关阅读

  • b2b推广网站有哪些 垂直b2b电商平台推荐

    b2b推广网站有哪些 垂直b2b电商平台推荐

    跟大家聊一聊b2b推广网站有哪些和垂直b2b电商平台推荐方面的内容,下面小编为您详细解答 最近,很多小伙伴都反馈给我。我想知道哪些B2B网站在市场上做得很好。以下是IT袋网小编收集的全

  • 前端开发工程师是做什么的 web前端的工作内容

    前端开发工程师是做什么的 web前端的工作内容

    IT袋网为大家说一说前端开发工程师是做什么的和web前端的工作内容方面的讲解,请看下面详细的介绍。 随着Internet的发展和多个终端的普及,前端开发工程师逐渐受到欢迎,但是前端开发工程

  • 什么是Ceph 有什么特点?

    什么是Ceph 有什么特点?

    如果想了解什么是CephIT技巧方面的经验,相关内容具体如下: 概述 Ceph是当前非常流行的开源分布式存储系统,具有高扩展性、高性能、高可靠性等优点,同时提供 块存储服务 (rbd)、 对象存储

  • [网络工程师]-网络规划与设计-通信规范分析

    [网络工程师]-网络规划与设计-通信规范分析

    相对于大多数人[网络工程师]-网络规划与设计-通信规范分析方面的介绍,下面为详细的介绍。 在网络分析和设计过程中,通信规范分析处于第二个阶段,通过分析网络通信流量和通信模式,发