java工具箱-Redisson

122 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天,点击查看活动详情

Redisson官网

Redisson是java访问Redis的Java客户端,同类比较流行的工具包还有Lettuce,Jedis。与这两个工具包相比Redisson提供了很多工具方法(如RAtomicLong,Bloom Filter,RateLimiter等)以及集群环境下的解决方案,例如集群环境下的分布式锁联锁,红锁……等特性,项目wiki上甚至有专门的中文文档,十分友善。

Redisson除了开源版之外还有企业版,提供了本地缓存等特性详情请参考官网

Pom依赖

    <!-- Spring Boot 依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
    </parent>
    <!--springboot-redisson启动器-->
    <dependencies>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.14.0</version>
        </dependency>
    </dependencies>
  • 启动器中包含了spring-boot-starter-data-redis,spring-session-core,spring-boot-starter-web,等常用的jar包,无需重复依赖。
  • redisson-spring-boot-starter对应Pom配置文件中描述了对应的springboot版本,需保持一致。
  • 可使用spring-redis默认配置方式,也可以使用redisson配置,配置更多特性。

使用

增加启动项后除了RedisTemplate,还可以注入RedissonClient

在最新的版本中还注入了以下javaBean。

  • RedissonRxClient:基于RxJava2的函数式编码风格访问模式
  • RedissonReactiveClient:异步模式访问redis
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/redisson")
public class RedissonController {
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/ratomicLong")
    public long ratomicLong() {
        RAtomicLong temp = redissonClient.getAtomicLong("temp");
        return temp.getAndIncrement();
    }

    @GetMapping("/rkeys")
    public String rkeys(){
        RKeys keys = redissonClient.getKeys();
        Iterable<String> foundedKeys = keys.getKeysByPattern("*b");
        return StreamSupport.stream(foundedKeys.spliterator(),false)
                .collect(Collectors.joining(","));
    }

    @GetMapping("/rlock")
    public String rlock(){
        RLock lock = redissonClient.getLock("myLock");
        String result = "";
        try {
            boolean res = lock.tryLock(1, 10, TimeUnit.SECONDS);
            if (res) {
                try {
                    Thread.sleep(2000);
                    result = "计算完成";
                } finally {
                    lock.unlock();
                }
            }else{
                result = "未参与计算-未获取到锁";
            }
        } catch (InterruptedException e) {
            result = "未参与计算-创建锁异常";
        }
        return result;
    }

}
  • RAtomicLong:基于redis的原子长整型,可用于生成编号
  • RKeys:通过redis的SCAN命令查找redis中的key
  • RLock:基于redis的锁,符合java锁使用模式,

springboot-redis提供的RedisTemplate是对redis核心功能的基本操作,RedissonClient则是redis与java结合之后的一些常用扩展操作封装。

参考资料

github.com/redisson/re…

redisson.org/

github.com/redisson/re…