Spring boot integrates redis (multiple data sources)

Posted by parkie on Sat, 19 Feb 2022 09:46:28 +0100

Official website: https://spring.io/projects/spring-data-redis

Spring Data Redis is a part of the spring data series. Redis can be easily configured and accessed from spring applications, and supports low-level and high-level abstract interfaces. You can see that spring data provides the following technical solutions:

There are already excellent Java Redis tool libraries such as Redis, redison and lattice on the market. Why Spring Data Redis?

  • Spring and Jedis provide a unified operation template library for spring and Jedis.
  • For the upper layer, developers can learn how to use Spring Data Redis without caring about the API operations of Jedis and lettue. Even in the future, if we want to migrate Redis access from Jedis to lattice, we don't need to make any changes.
  • springboot 2. Version x uses Lettce as the default connection pool and can switch to jedis.

Note: at present, Spring Data Redis only supports the internal encapsulation of Jedis and lattice, while Redisson is provided by redisson-spring-data To provide.

Note: for the use of jedis, see https://blog.csdn.net/liuxiao723846/article/details/86724124

Next, we will mainly introduce the use of spring data redis (redis stand-alone), and the redis cluster and redis sentinel will be introduced later.

1. Single data source



            <!--Remove right Lettuce Because Spring Boot priority of use Lettuce As Redis client -->
    <!-- introduce Jedis Dependence, so Spring Boot Implementation pair Jedis Automated configuration of -->

Note: lettuce is used by default in spring boot starter data redis. If jedis library is used, it needs to be exclude d and then manually imported into jedis.


#  Redis database number, which is 0 by default 

#Maximum idle number  
#The maximum number of database connections to the connection pool. If it is set to 0, it means unlimited. If it is jedis 2.4, use redis maxTotal  
#Controls how many jedis instances can be allocated to a pool to replace the above redis Maxactive. Use this attribute after jedis 2.4  
#Maximum connection setup wait time. If this time is exceeded, an exception will be received. Set to - 1 for unlimited.  
#The minimum idle time of a connection defaults to 1800000 milliseconds (30 minutes)  
#The maximum number of connections released each time. The default is 3  
#The time interval of eviction scanning (MS). If it is negative, the eviction thread will not be run. The default is - 1  
#Whether to verify the connection before removing it from the pool. If the verification fails, remove the connection from the pool and try to remove another one  
#Check validity when idle. The default is false  


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

public class RedisConfig {
    private String host;
    private Integer port;
    private String pwd;
    private Integer timeout;
    private Integer maxIdle;
    private Integer maxTotal;
    private Integer maxWaitMillis;
    private Integer minEvictableIdleTimeMillis;
    private Integer numTestsPerEvictionRun;
    private long timeBetweenEvictionRunsMillis;
    private boolean testOnBorrow;
    private boolean testWhileIdle;
     * JedisPoolConfig Connection pool
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);// Maximum idle number
        jedisPoolConfig.setMaxTotal(maxTotal);// Maximum number of database connections to the connection pool
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);// Maximum connection setup wait time
        jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);// The default minimum idle time for evicting connections is 1800000 milliseconds (30 minutes)
        // The maximum number of evictions per eviction check is 1/abs(n) if it is negative. The default is 3
        // The time interval of eviction scanning (MS). If it is negative, the eviction thread will not be run. The default is - 1
        // Whether to verify the connection before removing it from the pool. If the verification fails, remove the connection from the pool and try to remove another one
        jedisPoolConfig.setTestWhileIdle(testWhileIdle);// Check validity when idle. The default is false
        return jedisPoolConfig;
     * Link factory
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
        JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory();
        if (pwd != null && !pwd.equals("")) {
        return JedisConnectionFactory; 

     * RedisTemplate
     * @return
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);// The key adopts the serialization method of String
        template.setHashKeySerializer(stringRedisSerializer);// The key of hash is serialized by String

//        GenericJackson2JsonRedisSerializer jacksonSerializer = new GenericJackson2JsonRedisSerializer();
//        template.setValueSerializer(jacksonSerializer);// value is serialized by jackson
//        template. setHashValueSerializer(jacksonSerializer);//  The value of hash adopts jackson serialization
        template.setEnableTransactionSupport(true);// Open transaction
        template.setConnectionFactory(factory);// Set up link factory
        return template;


  • If you need to set the serialization method of value in your code, you don't need to specify it in RedisConfig. (for example, some business codes are serialized according to json and some are serialized according to pb)
  • In spring data redis, the word is serialized with string, jackson and other methods

Note: if the key serialization method is not specified, it will be found that there will be more keys in redis, such as \ xac\xed\x00\x05t\x00. Therefore, it is best to specify the key serialization method as string.

4) Use:

public class TestService {
    private RedisTemplate<String, Object> redisTemplate;
    public String test1(String value) {
        return (String)redisTemplate.opsForValue().get("test_1");
    public String test2(String value) {
        User user = new User(99,value);
        byte[] serialize = SerializeUtil.serialize(user);
        byte[] bytes = (byte[])redisTemplate.opsForValue().get("test_9");
        User obj = (User) SerializeUtil.unserialize(bytes);

        return obj.toString();

//Serialization tool
public class SerializeUtil {
    public static byte[] serialize(Object object) {//serialize
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
        return null;
    public static Object unserialize(byte[] bytes) {//Deserialization
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
        return null;

2. Single data source 2

The annotation method is used above (springboot adopts config annotation by default to replace the bean declaration in application.xml). Next, we use the original spring application.xml XML configuration mode.



If the primary node is not specified for multiple data sources, the following error will be reported:

Parameter 0 of method redisTemplate in org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration required a single bean, but 2 were found:





Topics: Java