[Java]Redission入门使用

一、什么是Redission

Redission is a Java-based distributed cache and object storage framework. It provides a variety of distributed data structures and services, such as lists, sets, queues, maps, bitmaps, HyperLogLog, geospatial indexing, etc.
It supports Redis protocol and JCache API, and provides many features such as real-time statistics, Pub/Sub, Lua script writing, etc., making it more convenient for applications to use and manage distributed data.
It also has advantages such as high performance, high reliability, and scalability.

Redission是一个Java实现的分布式缓存和分布式对象存储框架,它提供了许多分布式数据结构和服务,如列表、集合、队列、映射、位图、HyperLogLog、Geospatial索引等。
它支持Redis协议和JCache API,并提供了许多功能,如实时统计、Pub/Sub、Lua脚本编写等,使得应用程序可以更加方便地使用和管理分布式数据。
同时也具有高性能、高可靠性和可扩展性等优势。

Based on high-performance async and lock-free Java Redis client and Netty framework.

基于高性能异步和无锁的Java Redis Client 和Netty。


二、入门使用

1、Maven依赖

<dependency>
   <groupId>org.redissongroupId>
   <artifactId>redissonartifactId>
   <version>3.24.1version>
dependency>  

2、创建配置实例—— Create config object

Config config = new Config();
config.useClusterServers()
	// 集群状态扫描间隔时间,单位是毫秒
	.setScanInterval(2000)
	//cluster方式至少6个节点
	.addNodeAddress("redis://127.0.0.1:6379" )
	.addNodeAddress("redis://127.0.0.1:6380")
	.addNodeAddress("redis://127.0.0.1:6381")
	.addNodeAddress("redis://127.0.0.1:6382")
	.addNodeAddress("redis://127.0.0.1:6383")
	.addNodeAddress("redis://127.0.0.1:6384");

or read config from file

或者读取配置文件

Config config = Config.fromYAML(new File("config-file.yaml")); 

3、创建Redission实例—— Create Redisson instance

// Sync and Async API
RedissonClient redisson = Redisson.create(config);

4. Get Redis based implementation of java.util.concurrent.ConcurrentMap

RMap<Object, Object> map = redisson.getMap("map_key");
RLock rLock = map.getLock("key");

try {
	// lock 上锁
	rLock.lock();
    Object v = map.get(k);
    // todo 业务逻辑
} finally {
    rLock.unlock();
}

5. Get Redis based implementation of java.util.concurrent.locks.Lock

Redission加锁

设置过期时间为3秒的锁。

try {
	//获取锁对象
	RLock lock = redisson.getLock("my_key");
	//加锁
	lock.lock(3, TimeUnit.SECONDS);
	//todo 业务
} finally {
    lock.unlock();
}

若不设置过期时间,默认过期时间30秒,并触发Watchdog进行续期

try {
	//获取锁对象
	RLock lock = redisson.getLock("my_key");
	//加锁,默认过期30秒,并触发Watchdog进行续期
	lock.lock();
	//todo 业务
} finally {
    lock.unlock();
}

Watch Dog 的自动延期机制

如果一个拿到锁的线程正常执行任务但还没完成,那么看门狗会延长锁超时时间,锁不会因为超时而被释放。

看门狗默认续期时间是30s,可以通过修改Config.lockWatchdogTimeout指定时间。

如果使用lock方法传入过期时间leaseTime,那么超时自动解锁,不会触发Watch Dog延长时间。


6. Get Redis based implementation of java.util.concurrent.ExecutorService

RScheduledExecutorService executorService = redisson.getExecutorService("my_key");
//3秒时间后执行任务
executorService.schedule(()->{
    //todo 业务逻辑
},3,TimeUnit.SECONDS);

//2秒后执行,每3秒执行一次
executorService.scheduleAtFixedRate(()->{
   //业务逻辑
}, 2,3,TimeUnit.SECONDS);

//2秒后执行,每3秒执行一次
executorService.scheduleWithFixedDelay(()->{
   //业务逻辑
}, 2,3,TimeUnit.SECONDS);


7、等等…

你可能感兴趣的:(JAVA,java,开发语言,redis,redisson)