• Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)


    环境

    1. springmvc
    2. jdk1.8
    3. maven

    redis.properties配置文件

    #redis setting  
    redis.host=localhost
    redis.port=6379
    redis.password=
    redis.maxIdle=200
    redis.minIdle=0
    redis.maxActive=50
    redis.maxWait=10000
    redis.testOnBorrow=true
    redis.timeout=100000
    
    #定义需要使用的db
    //#sessionCode DB
    sessionCodeDb = 0
    //#车辆基本信息 DB
    bicycleInfoDb = 15
    //#当前位置信息 DB
    currentLocationDb = 14
    //#锁车/解锁 DB
    lockDb = 13
    //#根据车牌获取电子车牌 DB
    ebikeNoDb = 12
    //#根据电子车牌获取车牌 DB
    bikeNoDb = 11

    pom.xml依赖

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring.version>5.1.2.RELEASE</spring.version>
        </properties>
        <!-- spring4 start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>2.0.14.RELEASE</version>
            </dependency>
            <!-- fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.11</version>
            </dependency>
            <!-- json-lib -->
            <!-- <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> 
                </dependency> -->
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>

    RedisConfig.java 配置类 初始化redis连接池

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisPassword;
    import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
    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.core.ValueOperations;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.stereotype.Component;
    import redis.clients.jedis.JedisPoolConfig;
    
    import javax.annotation.PostConstruct;
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    @Component
    @Slf4j
    public class RedisConfig {
    
        @Value("${redis.host}")
        private String hostName;
        @Value("${redis.port}")
        private int port;
        @Value("${redis.password}")
        private String passWord;
        @Value("${redis.maxIdle}")
        private int maxIdl;
        @Value("${redis.minIdle}")
        private int minIdl;
        @Value("${redis.timeout}")
        private int timeout;
    
        @Value("${sessionCodeDb}")
        private int sessionCodeDb;
        @Value("${bicycleInfoDb}")
        private int bicycleInfoDb;
        @Value("${currentLocationDb}")
        private int currentLocationDb;
        @Value("${lockDb}")
        private int lockDb;
        @Value("${ebikeNoDb}")
        private int ebikeNoDb;
        @Value("${bikeNoDb}")
        private int bikeNoDb;
    
        public static Map<Integer,RedisTemplate<Serializable, Object>> redisTemplateMap = new HashMap<>();
    
        @PostConstruct
        public void initRedisTemp() throws Exception{
            log.info("###### START 初始化 Redis 连接池 START ######");
            redisTemplateMap.put(sessionCodeDb,redisTemplateObject(sessionCodeDb));
            redisTemplateMap.put(bicycleInfoDb,redisTemplateObject(bicycleInfoDb));
            redisTemplateMap.put(currentLocationDb,redisTemplateObject(currentLocationDb));
            redisTemplateMap.put(lockDb,redisTemplateObject(lockDb));
            redisTemplateMap.put(ebikeNoDb,redisTemplateObject(ebikeNoDb));
            redisTemplateMap.put(bikeNoDb,redisTemplateObject(bikeNoDb));
            log.info("###### END 初始化 Redis 连接池 END ######");
        }
    
        public RedisTemplate<Serializable, Object> redisTemplateObject(Integer dbIndex) throws Exception {
            RedisTemplate<Serializable, Object> redisTemplateObject = new RedisTemplate<Serializable, Object>();
            redisTemplateObject.setConnectionFactory(redisConnectionFactory(jedisPoolConfig(),dbIndex));
            setSerializer(redisTemplateObject);
            redisTemplateObject.afterPropertiesSet();
            return redisTemplateObject;
        }
    
        /**
         * 连接池配置信息
         * @return
         */
        public JedisPoolConfig jedisPoolConfig() {
            JedisPoolConfig poolConfig=new JedisPoolConfig();
            //最大连接数
            poolConfig.setMaxIdle(maxIdl);
            //最小空闲连接数
            poolConfig.setMinIdle(minIdl);
            poolConfig.setTestOnBorrow(true);
            poolConfig.setTestOnReturn(true);
            poolConfig.setTestWhileIdle(true);
            poolConfig.setNumTestsPerEvictionRun(10);
            poolConfig.setTimeBetweenEvictionRunsMillis(60000);
            //当池内没有可用的连接时,最大等待时间
            poolConfig.setMaxWaitMillis(10000);
            //------其他属性根据需要自行添加-------------
            return poolConfig;
        }
        /**
         * jedis连接工厂
         * @param jedisPoolConfig
         * @return
         */
        public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig,int db) {
            //单机版jedis
            RedisStandaloneConfiguration redisStandaloneConfiguration =
                    new RedisStandaloneConfiguration();
            //设置redis服务器的host或者ip地址
            redisStandaloneConfiguration.setHostName(hostName);
            //设置默认使用的数据库
            redisStandaloneConfiguration.setDatabase(db);
            //设置密码
            redisStandaloneConfiguration.setPassword(RedisPassword.of(passWord));
            //设置redis的服务的端口号
            redisStandaloneConfiguration.setPort(port);
            //获得默认的连接池构造器(怎么设计的,为什么不抽象出单独类,供用户使用呢)
            JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
                    (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
            //指定jedisPoolConifig来修改默认的连接池构造器(真麻烦,滥用设计模式!)
            jpcb.poolConfig(jedisPoolConfig);
            //通过构造器来构造jedis客户端配置
            JedisClientConfiguration jedisClientConfiguration = jpcb.build();
            //单机配置 + 客户端配置 = jedis连接工厂
            return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
        }
    
        private void setSerializer(RedisTemplate<Serializable, Object> template) {
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
                    Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setKeySerializer(template.getStringSerializer());
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            //在使用String的数据结构的时候使用这个来更改序列化方式
            RedisSerializer<String> stringSerializer = new StringRedisSerializer();
            template.setKeySerializer(stringSerializer );
            template.setValueSerializer(stringSerializer );
            template.setHashKeySerializer(stringSerializer );
            template.setHashValueSerializer(stringSerializer );
        }
    
        /**
         * 删除对应的value
         * @param key
         */
        public void remove(final String key,int db) {
            RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
            if (exists(key,redisTemplate)) {
                redisTemplate.delete(key);
            }
        }
    
        /**
         * 判断缓存中是否有对应的value
         * @param key
         * @return
         */
        public boolean exists(final String key,RedisTemplate<Serializable, Object> redisTemplate) {
            return redisTemplate.hasKey(key);
        }
    
        /**
         * 读取缓存
         * @param key
         * @return
         */
        public Object get(final String key,int db) {
            RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
            Object result = null;
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            result = operations.get(key);
            return result;
        }
    
        /**
         * 写入缓存
         * @param key
         * @param value
         * @return
         */
        public boolean set(final String key, Object value,int db) {
            RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
            boolean result = false;
            try {
                ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                result = true;
            } catch (Exception e) {
                log.error("set cache error", e);
            }
            return result;
        }
    
        /**
         * 根据key 获取过期时间
         * @param key 键 不能为null
         * @return 返回0代表为永久有效
         */
        public long getExpire(String key,TimeUnit unit,int db) {
            RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
            return redisTemplate.getExpire(key, unit);
        }
    
    
     
    /**
         * 根据db 获取对应的redisTemplate实例
         * @param db
         * @return
         */
        public RedisTemplate<Serializable, Object> getRedisTemplateByDb(int db){
            return redisTemplateMap.get(db);
        }
    
    }

    在其他类中的使用

        @Autowired
        private RedisConfig redisUtil;
        
        //#获取db0 数据库的数据
        public static Integer sessionCodeDb = 0;
    /**
         * 根据sessionCode获取userId
         * @param sessionCode
         * @return
         */
        public String getUserIdBySessionCode(String sessionCode){
            try {
                Object obj = redisUtil.get(sessionCode,sessionCodeDb);
                if(obj!=null) {
                    return obj.toString();
                }else{
                    return null;
                }
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
  • 相关阅读:
    Linux下安装mysql(1)(CentOS)
    shell 运算符
    shell $*与$@的区别
    XMind破解
    在CentOS系统上将deb包转换为rpm包
    一次与流氓软件的斗争,浏览器主页被锁定
    dropzone上传文件
    KindEditor4.1.10,支持粘贴图片
    bootstrap paginator使用简述
    sendkeys && appactivate
  • 原文地址:https://www.cnblogs.com/muxi0407/p/11845016.html
Copyright © 2020-2023  润新知