The easiest Redisson tutorial in history

Continue to create, accelerate growth! This is the 20th day of my participation in the "Nuggets Daily New Plan · October Update Challenge", click to view the details of the event

introduce

Redisson is a Java In-Memory Data Grid based on Redis . It makes full use of a series of advantages provided by the Redis key-value database, and provides users with a series of common tool classes with distributed characteristics based on the common interfaces in the Java utility toolkit. The toolkit originally used to coordinate single-machine multi-threaded concurrent programs has acquired the ability to coordinate distributed multi-machine and multi-threaded concurrent systems, which greatly reduces the difficulty of designing and developing large-scale distributed systems. At the same time, combining various distributed services with rich characteristics further simplifies the cooperation between programs in a distributed environment.

Redisson adopts the Netty framework based on NIO, which can not only serve as the underlying driver client of Redis, but also provide the connection function of various configuration forms of Redis. Redis commands can be sent synchronously, asynchronously , asynchronously stream or pipeline. The sending function, the LUA script execution processing, and the function of processing the returned result are also integrated with more advanced application solutions . , , , , , , etc. structure, on this basis, it also provides distributed , , , , , , , , , , , , , , , , etc. Distributed data structures that Redis did not have originally. Not only that, Redisson also implements distributed locks as mentioned in the Redis documentationHashListSetStringGeoHyperLogLog映射(Map)列表(List)集(Set)通用对象桶(Object Bucket)地理空间对象桶(Geospatial Bucket)基数估计算法(HyperLogLog)多值映射(Multimap)本地缓存映射(LocalCachedMap)有序集(SortedSet)计分排序集(ScoredSortedSet)字典排序集(LexSortedSet)列队(Queue)阻塞队列(Blocking Queue)有界阻塞列队(Bounded Blocking Queue)双端队列(Deque)阻塞双端列队(Blocking Deque)阻塞公平列队(Blocking Fair Queue)延迟列队(Delayed Queue)布隆过滤器(Bloom Filter)原子整长形(AtomicLong)原子双精度浮点数(AtomicDouble)BitSetLockSuch higher-level application scenarios. In fact, Redisson does not stop there. On the basis of distributed locks, it also provides 联锁(MultiLock), 读写锁(ReadWriteLock), 公平锁(Fair Lock), 红锁(RedLock), 信号量(Semaphore), 可过期性信号量(PermitExpirableSemaphore)and 闭锁(CountDownLatch)these basic components that are essential to multi-threaded high-concurrency applications in practice. It is through the realization of high-level application solutions based on Redis that Redisson becomes an important tool for building distributed systems.

Steps for usage

1. Introduce jar package

<dependency>
  <groupId>org.redisson</groupId>
  <artifactId>redisson</artifactId>
  <version>2.7.0</version>
</dependency> 
复制代码

2. Configuration

public class RedissonManager {
    private static Config config = new Config();
    //声明redisso对象
    private static Redisson redisson = null;
   //实例化redisson
 static{
​
     config.useSingleServer().setAddress("127.0.0.1:6379");
          //得到redisson对象
        redisson = (Redisson) Redisson.create(config);
​
}
​
 //获取redisson对象的方法
    public static Redisson getRedisson(){
        return redisson;
    }
}
复制代码

3. Lock acquisition and release

public class DistributedRedisLock {
   //从配置类中获取redisson对象
    private static Redisson redisson = RedissonManager.getRedisson();
    private static final String LOCK_TITLE = "redisLock_";
   //加锁
    public static boolean acquire(String lockName){
       //声明key对象
        String key = LOCK_TITLE + lockName;
       //获取锁对象
        RLock mylock = redisson.getLock(key);
       //加锁,并且设置锁过期时间,防止死锁的产生
        mylock.lock(2, TimeUnit.MINUTES); 
        System.err.println("======lock======"+Thread.currentThread().getName());
       //加锁成功
        return  true;
    }
  //锁的释放
    public static void release(String lockName){
       //必须是和加锁时的同一个key
        String key = LOCK_TITLE + lockName;
       //获取所对象
        RLock mylock = redisson.getLock(key);
      //释放锁(解锁)
        mylock.unlock();
        System.err.println("======unlock======"+Thread.currentThread().getName());
    }
}
复制代码

Welcome to like and comment, thank you guys ヾ(◍°∇°◍)ノ゙

Guess you like

Origin juejin.im/post/7158256053350744095