First, what is Redisson’s positioning?

The biggest advantage of Redis’ distributed lock approach is that it is based on Redisson’s API. So, we’ll start with what a Redisson is.

The official documentation

website

Address: redisson.org/

Redis Java Client with features of in-memory Data Grid Redis Java Client with features of in-memory Data Grid

function

  1. Redis configurations
  • Provides the configuration for each Redis scenario
  1. Managed Redis services support
  • Supports the management of redis services
  1. Engine
  • Provides interface engines for all forms of Redis development
  1. Distributed Java objects
  • Provides distributed Java objects
  1. Distributed Java locks and synchronizers
  • Java distributed locks and synchronizers
  1. Distributed Java services
  • Distributed Java services
  1. Distributed Java collections
  • Distributed Java collections
  1. Integration with Java frameworks
  • Integration with Java frameworks
  1. Supported codecs
  • Support multiple encoders

Of course, as Redisson’s Flag states, the goal is to meet the growing need to use Redis as storage

Usage scenarios

  1. Scaling Java applications
  • Extensible applications
  1. Caching
  • The cache
  1. Data source caching
  • Data source cache
  1. Distributed Java tasks scheduling and execution
  • Distributed Java task scheduling and execution
  1. Distributed data processing
  • Distributed data processing
  1. Easy Redis Java client
  • Redis Java Client
  1. Web session clustering
  • Web Session manager
  1. Messaging
  • Publish/subscribe messages

Github

Address: github.com/redisson/re…

Of course, the focus of this article is redisson distributed locks, and the redisson details are not outlined

Redisson redis reentrant distributed lock

The implementation code

public class ReentrantLock { public static void main(String[] args) { Config config = new Config(); The config. UseClusterServers (.) addNodeAddress (" redis: / / 106.12.46.26:7001 "). The addNodeAddress (" redis: / / 106.12.46.26:7002)" AddNodeAddress (" redis: / / 106.12.46.26:7003 "). The addNodeAddress (" redis: / / 106.12.6.73:7001)" AddNodeAddress (" redis: / / 106.12.6.73:7002 "). The addNodeAddress (" redis: / / 106.12.6.73:7003 "); RedissonClient redissonClient = Redisson.create(config); RLock lock = redisson.getLock("anyLock"); lock.lock(); Thread.sleep(30 * 1000); lock.unlock(); }}Copy the code

It mainly involves several points in several documents:

  • How to configure a cluster mode config and obtain redisson client github.com/redisson/re…
  • How to build a RLock github.com/redisson/re…

Simple instructions

A brief explanation of the two documents mentioned above

Config

  1. Clustered mode supports almost any service, including AWS ElastiCache Cluster and Azure Redis Cache
  2. Configurable parameters
    • checkSlotsCoverage

    • nodeAddresses

    • ScanInterval scanInterval, should feel used in the watchdog mechanism, pay attention to later

    • Slots Specifies the number of fragments for distributed objects

    • ReadMode Default value slave, which is the default read/write separation

    • subscriptionMode

    • LoadBalancer loadBalancer parameter

    • … There are many

Build RLock

  1. RLock is built on the basis of Lock locks in Java objects
  2. One problem with redisson’s implementation of locks is that if the instance of the lock being acquired crashes, the lock may hang in the acquired state forever. At the same time, a solution is proposedwatchdogIf the redisson client is alive, it will renew the lock continuously. By default, the lock watchdog timeout is 30 seconds, and it can passConfig.lockWatchdogTimeoutSettings changed.
  1. Redisson also allows you to specify when a lock is acquiredleaseTimeParameter to automatically release locked locks after a specified time interval
  2. RLock objects behave according to the Java Lock specification. This means that only the lock owner thread can unlock it, otherwise you will be thrown IllegalMonitorStateException. Otherwise, consider using RSemaphore objects.

Later, we can focus on the mechanism of this watchdog, including leaseTime and so on