What is Redisson

The officially recommended Java clients for Redis include Jedis, lettuce and Redisson. Each of the three clients has its own advantages and disadvantages. Choosing the appropriate client in our business implementation will help improve the performance of Redis. To choose the appropriate client, we should have a rough understanding of the communication methods of the Redis client and the advantages and disadvantages of various clients.

Redis overview

Redis supports multiple language clients. The supported clients can be viewed from the official website. Here is a list of common clients:
Redis uses a single-threaded method to handle multiple client connections. As a program developer, you should understand the relationship between the Redis server and the client. Communication protocols, and how to use the Redis client in mainstream programming languages. You also need to understand the corresponding APIs for client management and the problems that may be encountered in development and operation.

Redis client communication protocol

Redis has formulated the RESP (Redis Serializable Protocol, RESP) protocol to achieve normal interaction between the client and the server. This protocol is simple and efficient, and can be parsed by machines and easily recognized by humans.
RESP can serialize data of different data types (integer, string, array, and special type Error). The Redis command that needs to be executed will be encapsulated into a string array-like request and sent to the server through the client. Redis will select the corresponding data type to reply based on the specified command type.

Java client

jedi

The traditional and old Java client has been updated and has relatively comprehensive support for redis commands.

advantage

  • Supports comprehensive Redis commands and has a comprehensive API.

shortcoming

  • For blocking I/O, its methods are all synchronous calls. The program flow needs to wait until the sockets have processed the I/O before it can be executed. Asynchronous is not supported;
  • Jedis client instances are not thread-safe, so you need to use Jedis through a connection pool.

lettuce

lettuce is a scalable thread-safe client that supports asynchronous mode. Multiple threads can share a connection if blocking and transactional operations such as BLPOP and MULTI/EXEC are avoided. The bottom layer of lettuce is based on Netty and supports advanced Redis features, such as sentinels, clusters, pipelines, automatic reconnection and Redis data model.

advantage:

  • Support synchronous and asynchronous communication modes;
  • Lettuce's API is thread-safe, and multiple threads can share a connection if blocking and transactional operations such as BLPOP and MULTI/EXEC are not performed.
  • SpringBoot 2.x default Redis client provides perfect support for Lettuce

Redisson

Redisson is a Java In-Memory Data Grid implemented on the basis of Redis. It not only provides a series of distributed common Java objects, but also provides many distributed services. These include (BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service , Scheduler service) Redisson provides the simplest and most convenient way to use Redis. The purpose of Redisson is to promote users' separation of concerns (Separation of Concern) from Redis, so that users can focus more on processing business logic.
advantage:

  • The user's separation of concerns about Redis can be compared to the Spring framework. These frameworks build the basic framework and functions of the application, improve development efficiency, and allow developers to have more time to focus on business logic;
  • Provides many distributed related operation services, such as distributed locks, distributed collections, delay queue support through Redis, etc.
    shortcoming:
  • Redisson has poor support for string operations.

Redis Java client technology selection

The article is based on SpringBoot 2.x for selection

  • Only use Redis to cache string data, and you can use the default lettuce.
  • If you need to use Redis for distributed transactions, you can choose lettuce + Redisson.
  • If you need to cache distributed collection data and perform frequent read and write operations on the collection, you can choose lettuce + Redisson.
  • To use Redis as a message queue, you can choose lettuce + Redisson.

redission usage example

maven dependency

  <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.5.7</version>
        </dependency>

Configuration file

spring:
  redis:
    database: 0
    host: 192.168.159.100
    port: 6379

Configuration class

package com.xishan.store.usercenter.userweb;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {
    
    
    @Value("${spring.redis.host}")
    private String addr;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redisson() {
    
    
        Config config = new Config();
        config.useSingleServer()
                .setAddress(String.format("%s%s%s", "redis://", addr,":"+port))
                .setPassword(password)
                .setConnectionPoolSize(64)              // 连接池大小
                .setConnectionMinimumIdleSize(8)        // 保持最小连接数
                .setConnectTimeout(1500)                // 建立连接超时时间
                .setTimeout(2000)                       // 执行命令的超时时间, 从命令发送成功时开始计时
                .setRetryAttempts(2)                    // 命令执行失败重试次数
                .setRetryInterval(1000);                // 命令重试发送时间间隔

        return Redisson.create(config);
    }
}

Test class, use redission to conveniently obtain distributed locks

 public void contextLoads() {
    
    
        RLock sss = redissonClient.getLock("sss");
        sss.lock;
        sss.unlock();

    }

Guess you like

Origin blog.csdn.net/qq_37436172/article/details/130544599