Spring boot integrates redis

Posted by PyraX on Sat, 18 Dec 2021 16:13:38 +0100

1, Spring boot introduces redis dependency

Spring boot supports redis very well, just in POM XML file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Go to spring boot starter data redis. At the bottom, you can see that the dependency referenced by redis has changed from jedis to lettuce.

The main differences between jedis and lettuce are as follows:

jedis and lettuce are both redis clients

Jedis is directly connected to Redis server. In a multi-threaded environment, it is non thread safe. Each thread takes its own jedis instance. When the number of connections increases, the resource consumption is relatively large.

lettuce's connection is based on Netty. Connection instances can be shared among multiple threads. When multiple threads use the same connection instance, it is thread safe.

Therefore, in the new version of spring boot, the integrated redis client has been modified to lettuce.

2, Profile configuration

I wrote this section to learn how to view the spring boot autoconfigure package to understand how the imported toolkit configures properties in the file, rather than through Baidu.

As shown in the figure above, click spring.com of spring's automatic configuration package Factories file, search redis, and you can see the classes automatically configured by redis. Click open.

Click the RedisProperties class according to the screenshot above. As shown in the screenshot below, configure the prefix and property name of redis connection properties in the application file in the definition.

All the following configurations in the application file of spring boot can be recognized by redisTemplate in the source code.

#Configure rediszuowe
spring.redis.host=127.0.0.1
spring.redis.port=6379

3, Custom encapsulated redisTemplate

Using the above method, the native redisTemplate is automatically injected to operate the storage of redis data. The data stored in the database will be inconvenient to view directly with the redis client due to the problem of serialization, The redisTemplate in the code can only be queried (deserialized in the same way). The main reason is that the native redisTemplate selects the jdk serialization method, as shown in the following code screenshot.

Therefore, you need to customize the redisTemplate and re specify the sequence method to facilitate data storage in redis and viewing by redis clients. Generally, the key is serialized by String, and the value is serialized by jackson's json structure.

@Configuration
public class RedisConfig {

    /**
     * Create redisTemplate, select String serialization for key and json serialization for value
     * @param factory
     * @return
     */

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(factory);

        //Set json serialization
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance);
        //Serialization of String
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();

        //key select the serialization of String
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //value select jackson serialization
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //hashKey adopts String serialization
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        //hashValue is serialized with jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;

    }
}

 

Topics: Redis Spring Boot