最简单的spring-boot-starter-redisson发布啦!

Posted by Naah on Sunday, Jul 28,2019 10:41:51

spring-boot-starter-redisson

前言

最近忽然发现springboot2spring-boot-starter-data-redis底层默认使用了lettuce作为连接池,经过一番试用后,我的评价是辣鸡!

lettuce对比jedis的优化主要是使用了netty作为底层通信,实现了对redis非阻塞通信,对并发场景更加友好。

 然后在redis非阻塞通信这种场景下,我们为何不用我们大天朝码农和沙皇俄国码农共同开发的redisson

性能方面可是甩了lettuce一条街,而且在封装上,给你提供分布式锁分布式事务等友好的接口,最让我中意的点是集群下的mget也是通过计算key的槽,来对节点进行pipline计算,这对集群执行mgetmset简直就是福利,不用自己再去实现pipline

下面就让我们开始吧!

而且经过昨天的战斗,我已经成功的将这个依赖上传到中央仓库啦!你可以直接引用偶!

Github

[中文] [English]

1 介绍

RedisTemplateStringRedisTemplate整合Redisson,开箱即用,提供更友好更完善的API,更方便的调用!

最大的优点是省略redisson.yaml配置文件

本项目是基于xsxgit/redis-spring-boot-starter改写的(站在巨人的肩膀上)

2 软件依赖

3 当前版本

<dependency>
    <groupId>com.naah69</groupId>
    <artifactId>spring-boot-starter-redisson</artifactId>
    <version>1.0.0</version>
</dependency>

4 快速开始

4.1 准备工作

当前demo源码在本项目的spring-boot-starter-redisson-demo

  1. 添加依赖:
<dependency>
    <groupId>com.naah69</groupId>
    <artifactId>spring-boot-starter-redisson</artifactId>
    <version>1.0.0</version>
</dependency>
  1. redis配置:

yml方式:

server:
  port: 6969
spring:
  profiles:
    active: local
---
spring:
  profiles: local
  redis:
    redisson:
      enable: true
      singleServerConfig:
        idleConnectionTimeout: 10000
        pingTimeout: 1000
        connectTimeout: 10000
        timeout: 3000
        retryAttempts: 3
        retryInterval: 1500
        failedSlaveReconnectionInterval: 3000
        failedSlaveCheckInterval: 3
        #        password: null
        subscriptionsPerConnection: 5
        clientName: "redisson-demo"
        address: "redis://127.0.0.1:6379"
        subscriptionConnectionMinimumIdleSize: 1
        subscriptionConnectionPoolSize: 50
        connectionMinimumIdleSize: 32
        connectionPoolSize: 64
        database: 0
      threads: 0
      nettyThreads: 0
      codec:
        class: "org.redisson.codec.JsonJacksonCodec"
      transportMode: "NIO"
---
spring:
  profiles: cluster
  redis:
    redisson:
      enable: true
      clusterServersConfig:
        idleConnectionTimeout: 10000
        pingTimeout: 1000
        connectTimeout: 10000
        timeout: 3000
        retryAttempts: 3
        retryInterval: 1500
        failedSlaveReconnectionInterval: 3000
        failedSlaveCheckInterval: 3
        password: 'password'
        subscriptionsPerConnection: 5
        clientName: "redisson-demo"
        loadBalancer:
          class: "org.redisson.connection.balancer.RoundRobinLoadBalancer"
        slaveSubscriptionConnectionMinimumIdleSize: 1
        slaveSubscriptionConnectionPoolSize: 50
        slaveConnectionMinimumIdleSize: 32
        slaveConnectionPoolSize: 64
        masterConnectionMinimumIdleSize: 32
        masterConnectionPoolSize: 64
        readMode: "SLAVE"
        nodeAddresses:
        - "redis://127.0.0.1:7110"
        - "redis://127.0.0.1:7111"
        - "redis://127.0.0.1:7112"
        - "redis://127.0.0.1:7113"
        scanInterval: 1000
      threads: 0
      nettyThreads: 0
      codec:
        class: "org.redisson.codec.JsonJacksonCodec"
      transportMode: "NIO"

4.2 开始使用

下面的顺序为推荐使用的顺序

  1. RedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;

@Test
public void test() {
    //获取客户端
    ValueOperations<String, String> redisClient = redisTemplate.opsForValue();

    //key
    String key = "RedisTemplateTest";

    //删除
    redisTemplate.delete(key);

    //value
    long current = System.currentTimeMillis();
    String value = current + "";

    //set
    redisClient.set(key, value);

    //校验
    Assert.assertEquals(redisClient.get(key), value);
}
  1. RedisUtils
@Test
public void test() {
    //获取客户端
    StringHandler redisClient = RedisUtil.getStringHandler();

    //key
    String key = "RedisUtilsTest";

    //删除
    redisClient.remove(key);

    //value
    long current = System.currentTimeMillis();
    String value = current + "";

    //set
    redisClient.set(key, value);

    //校验
    Assert.assertEquals(redisClient.get(key), value);
}
  1. RedissonClient
@Autowired
private RedissonClient redissonClient;

@Test
public void test() {
    //key
    String key = "RedissonClientTest";

    //获取客户端
    RBucket<String> redisClient = redissonClient.getBucket(key);

    //删除
    redisClient.delete();

    //value
    long current = System.currentTimeMillis();
    String value = current + "";

    //set
    redisClient.set(value);

    //校验
    Assert.assertEquals(redisClient.get(), value);
}

4.3 自定义组件RedisUtils说明

获取操作实例:

// 获取默认数据库实例(DB)
DBHandler dbHandler = RedisUtil.getDBHandler();
...

// 获取数据库索引为2的数据库实例(DB)
DBHandler dbHandler = RedisUtil.getDBHandler(2);
...
实例 数据类型 获取方式
NumberHandler 数字(Number) RedisUtil.getNumberHandler()
RedisUtil.getNumberHandler(dbIndex)
StringHandler 字符串(String) RedisUtil.getStringHandler()
RedisUtil.getStringHandler(dbIndex)
HashHandler 哈希(Hash) RedisUtil.getHashHandler()
RedisUtil.getHashHandler(dbIndex)
SetHandler 无序集合(Set) RedisUtil.getSetHandler()
RedisUtil.getSetHandler(dbIndex)
ZsetHandler 有序集合(Zset) RedisUtil.getZsetHandler()
RedisUtil.getZsetHandler(dbIndex)
HyperLogLogHandler 基数(HyperLogLog) RedisUtil.getHyperLogLogHandler()
RedisUtil.getHyperLogLogHandler(dbIndex)
BitmapHandler 位图(Bitmap) RedisUtil.getBitmapHandler()
RedisUtil.getBitmapHandler(dbIndex)
GeoHandler 地理位置(Geo) RedisUtil.getGeoHandler()
RedisUtil.getGeoHandler(dbIndex)
KeyHandler 键(Key) RedisUtil.getKeyHandler()
RedisUtil.getKeyHandler(dbIndex)
ScriptHandler 脚本(Lua Script) RedisUtil.getScriptHandler()
RedisUtil.getScriptHandler(dbIndex)
PubSubHandler 发布订阅(Pubsub) RedisUtil.getPubSubHandler()
RedisUtil.getPubSubHandler(dbIndex)
StreamHandler 流(Stream) RedisUtil.getStreamHandler()
RedisUtil.getStreamHandler(dbIndex)
RedisUtil.getStreamHandler(dbIndex, mapper)
DBHandler 数据库(DB) RedisUtil.getDBHandler()
RedisUtil.getDBHandler(dbIndex)
SentinelHandler 哨兵(Sentinel) RedisUtil.getSentinelHandler()
RedisUtil.getSentinelHandler(dbIndex)
ClusterHandler 集群(Cluster) RedisUtil.getClusterHandler()
CustomCommandHandler 自定义命令(CustomCommand) RedisUtil.getCustomCommandHandler()
RedisUtil.getCustomCommandHandler(dbIndex)
RedisLockHandler 分布式锁(Lock) RedisUtil.getRedisLockHandler()
RedisUtil.getRedisLockHandler(dbIndex)
TransactionHandler 事务(Transaction) RedisUtil.getTransactionHandler()
RedisUtil.getTransactionHandler(dbIndex)

4.4 事务使用示例

List execute = RedisUtil.getTransactionHandler(2).execute(handler -> {
    // 开启监控
    handler.watch("xx", "test");
    // 开启事务
    handler.beginTransaction();
    // 获取对应事务字符串助手
    StringHandler stringHandler = handler.getStringHandler();
    // 执行操作
    stringHandler.set("xx", "hello");
    stringHandler.append("xx", "world");
    stringHandler.append("xx", "!");
    // 获取对应事务数字助手
    NumberHandler numberHandler = handler.getNumberHandler();
    numberHandler.addLong("test", 100);
    numberHandler.incrementLong("test");
    numberHandler.incrementLong("test");
    numberHandler.incrementLong("test");
    // 提交事务返回结果
    return handler.commit();
});