Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SpringBoot project is connected to the Redis cluster


May 31, 2021 Article blog


Table of contents


This article comes from the public number: Java Geek Technology Author: Duck Blood Fans

Presumably you're no stranger to Redis, and you'll use it more or less at work, whether it's for storing login information or caching hotspot data, which is helpful to us. However, Redis cluster estimates are not for everyone, because many business scenarios or systems are simple management systems and do not require Redis cluster environments.

The same was true before, the Redis used in the project was a stand-alone environment, but recently with the increase in the number of terminals, slowly found that stand-alone has been fast to support, so think again and again decided to upgrade the Redis environment into a cluster. The following powder to introduce you in the process of upgrading the project needs to adjust the place, this article does not involve the cluster construction and configuration, interested students to search for their own.

Configure the parameters

Since this article does not cover the construction of Redis clusters, here we assume that we already have a Redis cluster environment, and we need to adjust the following sections in our project

  1. Modify the configuration parameters, the node and password configuration of the cluster;
  2. Make sure that the introduced version of Jedis supports password setting, spring-data-redis 1.8 and SpringBoot 1.5 or more to support password setting;
  3. Inject RedisTemplate
  4. Writing tool classes;

Modify the configuration parameters

############### Redis 集群配置 #########################
spring.custome.redis.cluster.nodes=172.20.0.1:7001,172.20.0.2:7002,172.20.0.3:7003
spring.custome.redis.cluster.max-redirects=3
spring.custome.redis.cluster.max-active=500
spring.custome.redis.cluster.max-wait=-1
spring.custome.redis.cluster.max-idle=500
spring.custome.redis.cluster.min-idle=20
spring.custome.redis.cluster.timeout=3000
spring.custome.redis.cluster.password=redis.cluster.password

Introduce dependencies (if needed)

Make sure that the version of SpringBoot is greater than 1.4.x If not, use the following configuration to exclude older Jedis spring-data-redis from SpringBoot and then rely on higher versions of Jedis and spring-data-redis


       
            org.springframework.boot
            spring-boot-starter-data-redis

            

            

                
                    redis.clients
                    jedis

                

                
                    org.springframework.data
                    spring-data-redis

                

            

        

        

        
            redis.clients
            jedis
            2.9.0

        

        
            org.springframework.data
            spring-data-redis
            1.8.0.RELEASE

        

Inject RedisTemplate

To inject RedisTemplate we need three components: JedisConnectionFactory RedisClusterConfiguration JedisPoolConfig and here's the code to inject RedisTempalte First create JedisConnectFactory based on configuration, you need to configure RedisClusterConfiguration JedisPoolConfig and finally JedisConnectionFactory to create RedisTemplate

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import java.time.Duration;
import java.util.ArrayList;
import java.util.List;


public class RedisClusterConfig {


    @Bean(name = "redisTemplate")
    @Primary
    public RedisTemplate redisClusterTemplate(@Value("${spring.custome.redis.cluster.nodes}") String host,
                                     @Value("${spring.custome.redis.cluster.password}") String password,
                                     @Value("${spring.custome.redis.cluster.timeout}") long timeout,
                                     @Value("${spring.custome.redis.cluster.max-redirects}") int maxRedirect,
                                     @Value("${spring.custome.redis.cluster.max-active}") int maxActive,
                                     @Value("${spring.custome.redis.cluster.max-wait}") int maxWait,
                                     @Value("${spring.custome.redis.cluster.max-idle}") int maxIdle,
                                     @Value("${spring.custome.redis.cluster.min-idle}") int minIdle) {


        JedisConnectionFactory connectionFactory =  jedisClusterConnectionFactory(host, password,
                timeout, maxRedirect, maxActive, maxWait, maxIdle, minIdle);
        return createRedisClusterTemplate(connectionFactory);
    }


    private JedisConnectionFactory jedisClusterConnectionFactory(String host, String password,
                                                                   long timeout, int maxRedirect, int maxActive, int maxWait, int maxIdle, int minIdle) {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        List nodeList = new ArrayList();
        String[] cNodes = host.split(",");
        //分割出集群节点
        for (String node : cNodes) {
            String[] hp = node.split(":");
            nodeList.add(new RedisNode(hp[0], Integer.parseInt(hp[1])));
        }
        redisClusterConfiguration.setClusterNodes(nodeList);
        redisClusterConfiguration.setPassword(password);
        redisClusterConfiguration.setMaxRedirects(maxRedirect);


        // 连接池通用配置
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(maxIdle);
        genericObjectPoolConfig.setMaxTotal(maxActive);
        genericObjectPoolConfig.setMinIdle(minIdle);
        genericObjectPoolConfig.setMaxWaitMillis(maxWait);
        genericObjectPoolConfig.setTestWhileIdle(true);
        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(300000);


        JedisClientConfiguration.DefaultJedisClientConfigurationBuilder builder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration
                .builder();
        builder.connectTimeout(Duration.ofSeconds(timeout));
        builder.usePooling();
        builder.poolConfig(genericObjectPoolConfig);
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisClusterConfiguration, builder.build());
        // 连接池初始化
        connectionFactory.afterPropertiesSet();


        return connectionFactory;
    }


    private RedisTemplate createRedisClusterTemplate(JedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);


        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);


        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();


        return redisTemplate;
    }
}

The code has been submitted to GitHub and is available by replying to The Source Warehouse. It is important to note here that the spring-data-redis version of Jedis supports password setting, after all, the production environment must configure the password.

Write a tool class

In fact, this is basically done here, we can see that SpringBoot project access Redis cluster is relatively simple, and if the previous stand-alone environment is to adopt RedisTemplate now there is no need to write tool classes, the previous operation is still valid. However, as a intimate powder, I still prepared a tool class for everyone, the code is too long, I only paste part, need to complete the code can go to the public number reply to the "source warehouse" to get.

/**
     *  删除KEY
     * @param key
     * @return
     */
    public boolean delete(String key) {
        try {
            return getTemplate().delete(key);
        } catch (Exception e) {
            log.error("redis hasKey() is error");
            return false;
        }
    }


    /**
     * 普通缓存获取
     *
     * @param key 键
     * @return 值
     */
    public Object get(String key) {


        return key == null ? null : getTemplate().opsForValue().get(key);
    }


    /**
     * 普通缓存放入
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key, Object value) {


        try {
            getTemplate().opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("redis set() is error");
            return false;
        }


    }


    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                getTemplate().opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("redis set() is error");
            return false;
        }
    }


    /**
     * 计数器
     *
     * @param key 键
     * @return 值
     */
    public Long incr(String key) {


        return getTemplate().opsForValue().increment(key);
    }


    public Long incrBy(String key, long step) {


        return getTemplate().opsForValue().increment(key, step);
    }


    /**
     * HashGet
     *
     * @param key  键 不能为null
     * @param item 项 不能为null
     * @return 值
     */
    public Object hget(String key, String item) {


        return getTemplate().opsForHash().get(key, item);
    }


    /**
     * 获取hashKey对应的所有键值
     *
     * @param key 键
     * @return 对应的多个键值
     */
    public Map hmget(String key) {


        return getTemplate().opsForHash().entries(key);
    }


    /**
     * 获取hashKey对应的批量键值
     * @param key
     * @param values
     * @return
     */
    public List<Object> hmget(String key, List values) {


        return getTemplate().opsForHash().multiGet(key, values);
    }

Several methods are randomly listed above, and more scenarios await your exploration.

summary

Today, A powder to introduce to you how the SpringBoot project access to The Redis cluster, need friends can refer to, but A powder still want to say that the system design can not be too redundant, if in the short term can also support the development of the business, then do not consider too complex, after all, the system architecture is the need to constantly improve, it is not possible to design a very perfect system framework at the beginning. As your business continues to evolve, it's not too late to really discover that stand-alone Redis can no longer meet your business needs!

That's what W3Cschool编程狮 has to say about the SpringBoot project's access to the Redis cluster, and I hope it will help you.