Skip to content

pearstack/lock-spring-boot-starter

Repository files navigation

lock-spring-boot-starter

A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use

一款基于 Redis 和 Zookeeper 的分布式锁, 开箱即用,快速且易于使用

GitHub issues GitHub stars GitHub license GitHub forks Lines of code GitHub code size in bytes

工程简介

点击链接加入群聊【Pear Stack】讨论交流:293972109

国内镜像: 【Gitee】:lock-spring-boot-starter

  1. 简单描述

    lock-spring-boot-starter 一款分布式锁, 支持使用 RedisZooKeeper, 开箱即用, 快捷方便

  2. 特点

    1. spring-boot-starter组件开箱即用, 快捷方便
    2. 支持自定义key, 以及spring el表达式
    3. 将生成key的方法和上锁失败的异常方法作为接口, 可以自定义实现层, 更加自由
  3. 依赖版本

    框架名称 版本号 官网
    spring-boot-starter-aop 2.6.2 https://spring.io/projects/spring-boot
    spring-boot-starter-validation 2.6.2 https://spring.io/projects/spring-boot
    spring-integration-redis 2.6.2 https://spring.io/projects/spring-boot
    spring-boot-starter-data-redis 2.6.2 https://spring.io/projects/spring-boot
    redisson-spring-boot-starter 3.16.7 http://redisson.org
    spring-integration-zookeeper 2.6.2 https://spring.io/projects/spring-boot
    spring-boot-configuration-processor 2.6.2 https://spring.io/projects/spring-boot
    hutool-core 5.7.17 https://github.com/dromara/hutool
    lombok 1.18.22 https://projectlombok.org

安装

  1. Redisson 模式 Maven Central

    1. Apache Maven

      <dependency>
        <groupId>io.github.pearstack</groupId>
        <artifactId>lock-redisson-spring-boot-starter</artifactId>
        <version>${last.version}</version>
      </dependency>
    2. Gradle Groovy DSL

      implementation 'io.github.pearstack:lock-redisson-spring-boot-starter:${last.version}'
  2. Redis Template模式 Maven Central

    1. Apache Maven

      <dependency>
        <groupId>io.github.pearstack</groupId>
        <artifactId>lock-redis-template-spring-boot-starter</artifactId>
        <version>${last.version}</version>
      </dependency>
    2. Gradle Groovy DSL

      implementation 'io.github.pearstack:lock-redis-template-spring-boot-starter:${last.version}'
  3. ZooKeeper 模式 Maven Central

    1. Apache Maven

      <dependency>
        <groupId>io.github.pearstack</groupId>
        <artifactId>lock-zookeeper-spring-boot-starter</artifactId>
        <version>${last.version}</version>
      </dependency>
    2. Gradle Groovy DSL

      implementation 'io.github.pearstack:lock-zookeeper-spring-boot-starter:${last.version}'

快速开始

PS: 更多细节请看帮助文档

package com.lihao.lock.controller;

import io.github.pearstack.lock.annotation.Locked;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestController {

  public static Integer apple = 200;
  public static Integer pear = 200;

  /**
   * 带分布式锁, 不会出现超卖
   *
   * @param appleId
   * @return
   */
  @Locked(keys = "#appleId")
  @GetMapping("/get/apple")
  public String getApple(Long appleId) {
    if (apple <= 0) {
      return "对不起, 已经没货了!";
    } else {
      apple = apple - 1;
      log.info("购买成功, 现在apple数量为:{}", apple);
      return "购买成功!";
    }
  }

  /**
   * 不带分布式锁, 会出现超卖
   *
   * @param pearId
   */
  @GetMapping("/get/pear")
  public String getPear(Long pearId) {
    if (pear <= 0) {
      return "对不起, 已经没货了!";
    } else {
      pear = pear - 1;
      log.info("购买成功, 现在pear数量为:{}", pear);
      return "购买成功!";
    }
  }
}

特别鸣谢

  1. 茶猫云赞助服务器 : https://www.cmy.cn

使用lock-spring-boot-starter的开源项目

暂无~(欢迎留言)

友情链接

暂无~ (欢迎交换链接)

趋势图

Stargazers over time

About

A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages