RedisTemplate切库

最近在做项目是用到了Redis,之前了解不多,但因为项目要求是需要亿级别文档的搜索秒级别返回,所以使用了SpringBoot集成redis,根据大神设计好的逻辑进行后端开发。在过程中最主要的问题就是在查询不同redis库的索引时需要切库,但是试了setDataBase后无效,最后只能选择最笨的方法,追着一层层源码重写了redisTemplate,并建立了多个连接。

下面是其中一个redis的Properties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
    private String host;
    private int database;
    private int port;
    private int timeout;
    private Lettuce lettuce;
    private Cache cache;

    @Data
    public static class Lettuce {
        private Pool pool;
    }

    @Data
    public static class Pool {
        private int max_active;
        private int max_wait;
        private int max_idle;
        private int min_idle;

    }

    @Data
    public static class Cache{
        private int livetime;
    }

}

下面是重写的一个RedisTemplateConfiguration

import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;

@Configuration
public class RedisTemplateConfiguration {

    private final RedisProperties redisProperties;

    @Autowired
    public RedisTemplateConfiguration(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Configuration
    public class RedisConfig {
        @Bean(name = "redisConnectionFactory")
        @Primary
        public RedisConnectionFactory redisConnectionFactory1() {
            GenericObjectPoolConfig config = new GenericObjectPoolConfig();
            config.setMaxTotal(redisProperties.getLettuce().getPool().getMax_active());
            config.setMaxIdle(redisProperties.getLettuce().getPool().getMax_idle());
            config.setMinIdle(redisProperties.getLettuce().getPool().getMin_idle());
            config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMax_wait());
            RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
            configuration.setDatabase(redisProperties.getDatabase());
            configuration.setHostName(redisProperties.getHost());
            ClientResources clientResources = DefaultClientResources.builder().build();
            configuration.setPort(redisProperties.getPort());
            LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
                    .poolConfig(config).commandTimeout(Duration.ofSeconds(10)).shutdownTimeout(Duration.ofMillis(100)).clientResources(clientResources).build();
            return new LettuceConnectionFactory(configuration, lettuceClientConfiguration);
        }

        @Bean(name = "redisTemplate")
        @Primary
        public StringRedisTemplate redisTemplate1() {
            StringRedisTemplate redisTemplateObject = new StringRedisTemplate();
            redisTemplateObject.setConnectionFactory(redisConnectionFactory1());
            return redisTemplateObject;
        }

    }
}

用到的三个连接

RedisTemplate切库_第1张图片

application.properties中的数据源设置

RedisTemplate切库_第2张图片

建立好的redisTemplate的使用

RedisTemplate切库_第3张图片

你可能感兴趣的:(java开发,大数据,SpringBoot,Redis,RedisTemplate)