redis多数据库配置

 
package com.vwmin.dockercli.conf;


import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
@ConfigurationProperties("docker-cli.redis-database")
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {


    private final RedisProperties redisProperties;

    public RedisConfig(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }


    @Bean(name = "jwtTokenFactory", destroyMethod = "destroy")
    public LettuceConnectionFactory getJwtTokenFactory(){
        return getFactory(redisDatabase.getJwtToken());
    }

    @Bean(name = "serviceTokenFactory", destroyMethod = "destroy")
    public LettuceConnectionFactory getServiceTokenFactory(){
        return getFactory(redisDatabase.getServiceToken());
    }


    @Bean(name = "jwtTokenTemplate")
    public RedisTemplate<String, Object> jwtTokenTemplate(@Qualifier("jwtTokenFactory") LettuceConnectionFactory jwtTokenFactory){
        return commonTemplateSetting(jwtTokenFactory);
    }

    @Bean(name = "serviceTokenTemplate")
    public RedisTemplate<String, Object> serviceTokenTemplate(@Qualifier("serviceTokenFactory") LettuceConnectionFactory serviceTokenFactory){
        return commonTemplateSetting(serviceTokenFactory);
    }


    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(@Qualifier("serviceTokenFactory") LettuceConnectionFactory serviceTokenFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(serviceTokenFactory);
        return container;
    }

    /**
     * 自定义Lettuce连接配置:根据database的选择创建连接
     * @param database database index
     * @return 一个连接自定义database index的那啥
     */
    private LettuceConnectionFactory getFactory(int database){
        /* ========= 基本配置 ========= */
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(redisProperties.getHost());
        configuration.setPort(redisProperties.getPort());
        configuration.setPassword(redisProperties.getPassword());
        configuration.setDatabase(database);
        /* ========= 连接池通用配置 ========= */
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
        genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
        genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
        /* ========= lettuce pool ========= */
        LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
        builder.poolConfig(genericObjectPoolConfig);
//        builder.commandTimeout(redisProperties.getTimeout());

        
        // 该函数在后续调用 现在调用的话与后续冲突 会发生垃圾回收
        //        connectionFactory.afterPropertiesSet();

        return new LettuceConnectionFactory(configuration, builder.build());
    }

    private RedisTemplate<String, Object> commonTemplateSetting(LettuceConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // 设置value的序列化规则和key的序列化规则
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.setDefaultSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


    private final RedisDatabase redisDatabase = new RedisDatabase();

    public static class RedisDatabase {
        private int jwtToken = 0;
        private int serviceToken = 1;

        public int getJwtToken() {
            return jwtToken;
        }

        public void setJwtToken(int jwtToken) {
            this.jwtToken = jwtToken;
        }

        public int getServiceToken() {
            return serviceToken;
        }

        public void setServiceToken(int serviceToken) {
            this.serviceToken = serviceToken;
        }
    }

    public RedisDatabase getRedisDatabase() {
        return redisDatabase;
    }



}