Summary of frequently asked questions about the use of redisson


Insert image description here

Common errors

1. Wrong configuration method

Redisson provides two configuration methods
1. The first uses the configuration json file in application.properties spring.redis.redisson.file=classpath:redisson.json
2. The secondUse yaml format in spring.redis.redisson.config=as follows

spring:
  redis:
   redisson: 
      config: |
        clusterServersConfig:
          idleConnectionTimeout: 10000
          connectTimeout: 10000
          timeout: 3000
          retryAttempts: 3
          retryInterval: 1500
          failedSlaveReconnectionInterval: 3000
          failedSlaveCheckInterval: 60000
          password: null
          subscriptionsPerConnection: 5
          clientName: null
          loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {
    
    }
          subscriptionConnectionMinimumIdleSize: 1
          subscriptionConnectionPoolSize: 50
          slaveConnectionMinimumIdleSize: 24
          slaveConnectionPoolSize: 64
          masterConnectionMinimumIdleSize: 24
          masterConnectionPoolSize: 64
          readMode: "SLAVE"
          subscriptionMode: "SLAVE"
          nodeAddresses:
          - "redis://127.0.0.1:7004"
          - "redis://127.0.0.1:7001"
          - "redis://127.0.0.1:7000"
          scanInterval: 1000
          pingConnectionInterval: 0
          keepAlive: false
          tcpNoDelay: false
        threads: 16
        nettyThreads: 32
        codec: !<org.redisson.codec.Kryo5Codec> {
    
    }
        transportMode: "NIO"

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘singleServerConfig’: was expecting (JSON String, Number, Array, Object or token ‘null’, ‘true’ or ‘false’)

2. Version difference error reporting

As a result, the variable names of some configurations have been changed, and the old configuration item names cannot be mapped to configuration objects, such as pingTimeoutanduseLinuxNativeEpoll
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “pingTimeout”

Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel: [id: 0xadf834eb, L:/20.0.2.11:65046 - R:epaas.e6gpshk.com/172.20.6.37:6379] command: (AUTH), params: (password masked)

3. The password is configured in the configuration file or the configuration is incorrect.

If the redis server does not have a password configured, the password configuration cannot appear in the configuration file.
If the server has a password configured, the password needs to be configured in the configuration file, otherwise the error message is as follows
Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel: [id: 0xadf834eb, L:/20.0.2.11:65046 - R:epaas.e6gpshk.com/172.20.6.37:6379] command: (AUTH), params: (password masked)

4. Character set and serialization method configuration issues

If Jackson is used to serialize the string, the string will contain two double quotes, causing it to be empty when retrieved.
Workaround Configuring string serialization
can be configured by following these steps:

  1. Create a configuration object for the Redisson client:
Config config = new Config();
  1. Create a StringCodec object and set its serialization method:
StringCodec stringCodec = new StringCodec(Charset.forName("UTF-8"));
// 或者使用其他支持的编码方式,如ISO-8859-1
  1. Set a StringCodec object as Redisson’s default encoder:
config.setCodec(stringCodec);
  1. Create Redisson client:
RedissonClient redissonClient = Redisson.create(config);

You can configure the serialization method of Redisson's string type data to the specified encoding method. Generally, UTF-8 encoding is used, and you can also choose other encoding methods according to actual needs.

5. Redisson serialization problem

  • Problem: Using the default JDK serialization method, you may encounter compatibility issues when serializing and deserializing objects.
  • Solution: You can use other serialization solutions such as FastJson or Jackson to solve compatibility issues by setting a custom encoder.

Sample code:

Config config = new Config();
config.setCodec(new JsonJacksonCodec()); // 使用Jackson序列化
RedissonClient redisson = Redisson.create(config);

6. Connection pool problem:

  • Problem: There are not enough connections in the connection pool, causing the request to be blocked.
  • Solution: Increase the maximum number of connections in the connection pool, or adjust the configuration parameters of the connection pool, such as the minimum number of idle connections and connection timeout, to adapt to the concurrency needs of the system.

Example configuration:

Config config = new Config();
config.useSingleServer()
        .setAddress("redis://127.0.0.1:6379")
        .setConnectionPoolSize(100) // 设置连接池大小
        .setConnectionMinimumIdleSize(10) // 设置最小空闲连接数
        .setConnectTimeout(3000); // 设置连接超时时间
RedissonClient redisson = Redisson.create(config);

7. Redisson high availability issues:

  • Problem: When the Redis master node is down, the application cannot connect to the Redis database.
  • Solution: Use Redisson's sentinel mode or cluster mode to configure multiple Redis nodes and automatically switch to the backup node when the main node goes down.

Example configuration (sentinel mode):

Config config = new Config();
config.useSentinelServers()
        .addSentinelAddress("redis://127.0.0.1:26379")
        .addSentinelAddress("redis://127.0.0.1:26380")
        .setMasterName("mymaster");
RedissonClient redisson = Redisson.create(config);

8. Concurrency issues with Redisson

  • Problem: Simultaneous access to a shared resource by multiple threads can lead to data inconsistencies or race conditions.
  • Solution: Use Redisson's distributed lock to ensure that only one thread can access shared resources at the same time, or use atomic operations to ensure atomic operations on data.

Sample code (distributed lock):

RLock lock = redisson.getLock("myLock");
lock.lock();
try {
    
    
    // 执行需要互斥的操作
} finally {
    
    
    lock.unlock();
}

9. Redisson performance issues

  • Problem: When the system concurrency is large, the performance of Redisson decreases.
  • Solution: Increase the size of the connection pool, use asynchronous operations to improve throughput, and use clustered deployment and data sharding to improve concurrency performance.

Example configuration (asynchronous operation):

Config config = new Config();
config.useSingleServer()
        .setAddress("redis://127.0.0.1:6379")
        .setConnectionPoolSize(100)
        .setNettyThreads(0)
        .setThreads(0)
        .setTransportMode(TransportMode.EPOLL)
        .setUseLinuxNativeEpoll(true); // 使用异步操作提高性能
RedissonClient redisson = Redisson.create(config);

10. Redisson version compatibility issues:

  • Problem: Using an incompatible version of Redisson may cause runtime errors.
  • Solution: Before using Redisson, confirm that the version of Redisson is compatible with the version of the Redis server. You can check Redisson's official documentation or consult the community to obtain relevant information.

Sample code (Maven dependency):

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.15.2</version> <!-- 根据实际需求选择合适的版本 -->
</dependency>

2. Reference documents

Reference documentation

Guess you like

Origin blog.csdn.net/wangshuai6707/article/details/132802295
Recommended