All Projects → lucky8987 → spring-boot-starter-lock

lucky8987 / spring-boot-starter-lock

Licence: other
spring 分布式锁

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to spring-boot-starter-lock

honeymon-spring-boot-starter
Simple Spring Boot Starter and Summary
Stars: ✭ 20 (+66.67%)
Mutual labels:  spring-boot-starter
neo4j-java-driver-spring-boot-starter
Automatic configuration of Neo4j's Java Driver for Spring Boot applications
Stars: ✭ 33 (+175%)
Mutual labels:  spring-boot-starter
spring-boot-starter
Spring Boot support for Watson services
Stars: ✭ 12 (+0%)
Mutual labels:  spring-boot-starter
infinispan-spring-boot
Infinispan Spring Boot starter. Use this starter in your Spring Boot applications to help you use Infinispan+Spring integration in embedded and client/server mode
Stars: ✭ 61 (+408.33%)
Mutual labels:  spring-boot-starter
consul-cluster-manager
Consul - based cluster manager that can be plugged into Vert.x ecosystem.
Stars: ✭ 17 (+41.67%)
Mutual labels:  distributed-lock
hiatus-spring-boot
No description or website provided.
Stars: ✭ 23 (+91.67%)
Mutual labels:  spring-boot-starter
shiro-pac4j-spring-boot-starter
pac4j + shiro
Stars: ✭ 14 (+16.67%)
Mutual labels:  spring-boot-starter
rddlock
redis distribute lock
Stars: ✭ 12 (+0%)
Mutual labels:  distributed-lock
cxf-spring-boot-starter
Enterprise & production ready SOAP webservices powered by Spring Boot & Apache CXF
Stars: ✭ 129 (+975%)
Mutual labels:  spring-boot-starter
distlock
The universal component of distributed locks in golang , support redis and postgresql
Stars: ✭ 60 (+400%)
Mutual labels:  distributed-lock
spindle
A distributed locking library built on top of Cloud Spanner and TrueTime.
Stars: ✭ 47 (+291.67%)
Mutual labels:  distributed-lock
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (+116.67%)
Mutual labels:  distributed-lock
distributed-dev-learning
汇总、整理常用的分布式开发技术,给出demo,方便学习。包括数据分片、共识算法、一致性hash、分布式事务、非侵入的分布式链路追踪实现原理等内容。
Stars: ✭ 39 (+225%)
Mutual labels:  distributed-lock
juice
Java后端开发库,涵盖:常用工具类、SPI扩展、分布式锁、限流、分布式链路追踪等。
Stars: ✭ 32 (+166.67%)
Mutual labels:  distributed-lock
shiro-cas-spring-boot-starter
spring-boot-starter-shiro-cas
Stars: ✭ 16 (+33.33%)
Mutual labels:  spring-boot-starter
memcached-spring-boot
Library that provides support for auto-configuration of Memcached cache in a Spring Boot application.
Stars: ✭ 68 (+466.67%)
Mutual labels:  spring-boot-starter
qynat-spring-boot-starter
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。
Stars: ✭ 65 (+441.67%)
Mutual labels:  spring-boot-starter
grpc-spring-boot-starter
No description or website provided.
Stars: ✭ 16 (+33.33%)
Mutual labels:  spring-boot-starter
sherlock-distributed-lock
Distributed locking library for JVM
Stars: ✭ 17 (+41.67%)
Mutual labels:  distributed-lock
mqtt-spring-boot-starter
MQTT starter for Spring Boot, easier to use.
Stars: ✭ 48 (+300%)
Mutual labels:  spring-boot-starter

分布式锁 - starter

基于redis 的 setnx 实现 和 zk 的分布式锁组件

使用场景:

  • case1: bean注入获取锁

    application.properties(yml) 配置相关的lockName, 例如:

      lock.names = test_lock, demo_lock, temp_lock

    使用方式如下:

    import com.example.component;
    import org.springframework.stereotype.Service;  
      
      @Service
      public class CounterService {
            
          /**
           * lockName 来源 application.properties 中配置的 lock.names
           * 推荐使用 @resource 指定 lockName 的方式获取lockBean
           * 当然你也可以使用 @Autowired + @Qualifier("test_lock") 获取lockBean
           * 注意:如果不指定lockName 则默认获取到的 名称为:global_lock 的 lockBean
           */  
          @Resource(name = "test_lock")
          private Lock testLock;
      
          private Integer count = new Integer(100);
          
          public Integer increment() {
              // 占用锁
              testLock.lock();
              while (count > 0) {
                  try {
                      Thread.sleep(10);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  count--;
              }
              // 释放锁
              testLock.unlock();
              return count;
          }
      }

    或者你可以这样使用锁对象,来实现自动释放(推荐使用,如果程序异常退出也能实现自动释放)

    import com.example.component;
    import org.springframework.stereotype.Service;  
      
      @Service
      public class CounterService {
            
          /**
           * lockName 来源 application.properties 中配置的 redis-lock.names
           * 推荐使用 @resource 指定 lockName 的方式获取lockBean
           * 当然你也可以使用 @Autowired + @Qualifier("test_lock") 获取lockBean
           * 注意:如果不指定lockName 则默认获取到的 名称为:global_lock 的 lockBean
           */  
          @Resource(name = "test_lock")
          private RedisLock testLock;
      
          private Integer count = new Integer(100);
          
          public Integer increment() {
              // 占用锁, java7+ 特性实现自动unlock
              try (redisLock redisLock = testLock.autoLock()){
                  while (count > 0) {
                      try {
                          Thread.sleep(10);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      count--;
                  }
              }
              return count;
          }
      }
  • case2: 使用注解加锁, 不需要在application.properties中配置names; 代码如下:

      import com.example.annotation.DistributedLock;
      import org.springframework.stereotype.Service;
      
      @Service
      public class CounterService {
      
          private Integer count = new Integer(100);
      
          /**
          * aop 实现加锁, 业务方法过粗的情况下不推荐使用
          */
          @DistributedLock(name = "test_lock")
          public Integer increment() {
              while (count > 0) {
                  try {
                      Thread.sleep(10);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  count--;
              }
              return count;
          }
      }

TODO 锁竞争优化、公平锁的实现

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].