spring-boot-starter-redisson

redisson for Spring Boot

License

License

Categories

Categories

Spring Boot Container Microservices Redis Data Databases Redisson
GroupId

GroupId

com.naah69
ArtifactId

ArtifactId

spring-boot-starter-redisson
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

spring-boot-starter-redisson
redisson for Spring Boot
Project URL

Project URL

https://github.com/naah69/spring-boot-starter-redisson
Source Code Management

Source Code Management

https://github.com/naah69/spring-boot-starter-redisson

Download spring-boot-starter-redisson

How to add to project

<!-- https://jarcasting.com/artifacts/com.naah69/spring-boot-starter-redisson/ -->
<dependency>
    <groupId>com.naah69</groupId>
    <artifactId>spring-boot-starter-redisson</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.naah69/spring-boot-starter-redisson/
implementation 'com.naah69:spring-boot-starter-redisson:1.0.0'
// https://jarcasting.com/artifacts/com.naah69/spring-boot-starter-redisson/
implementation ("com.naah69:spring-boot-starter-redisson:1.0.0")
'com.naah69:spring-boot-starter-redisson:jar:1.0.0'
<dependency org="com.naah69" name="spring-boot-starter-redisson" rev="1.0.0">
  <artifact name="spring-boot-starter-redisson" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.naah69', module='spring-boot-starter-redisson', version='1.0.0')
)
libraryDependencies += "com.naah69" % "spring-boot-starter-redisson" % "1.0.0"
[com.naah69/spring-boot-starter-redisson "1.0.0"]

Dependencies

compile (6)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.1.5.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.1.5.RELEASE
org.springframework.boot : spring-boot-starter-data-redis jar 2.1.5.RELEASE
org.apache.commons : commons-pool2 jar 2.6.2
com.alibaba : fastjson jar 1.2.58
org.redisson : redisson-spring-data-21 jar 3.11.0

Project Modules

There are no modules declared in this project.

spring-boot-starter-redisson

[中文] [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();
});

Versions

Version
1.0.0