zoukankan      html  css  js  c++  java
  • springboot2.x 与 1.x 配置 redis 区别

    1.X redis  maven 依赖

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

    配置文件

    # Redis 数据库索引(默认为0)
    spring.redis.database = 0
    # Redis 服务器地址
    spring.redis.host = 192.168.110.1
    # Redis 服务器连接端口
    spring.redis.port = 6379
    # Redis 服务器连接密码(默认为空)
    spring.redis.password = 
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active = 8
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait = -1
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle = 8
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle = 0
    # 连接超时时间(毫秒)
    spring.redis.timeout = 0
    #集群节点
    spring.redis.cluster.nodes = 192.168.110.2:6379,192.168.110.3:6380,192.168.110.4:6381,192.168.110.5:6382,191.168.110.6:6384,192.168.110.7:6385
    spring.redis.cluster.max-redirects = 3
    spring.redis.cluster.timeout = 5000

    springboot 2.X redis maven 依赖

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

    配置文件

    # Redis 数据库索引(默认为0)
    spring.redis.database = 0
    # Redis 服务器地址
    spring.redis.host = 192.168.110.1
    # Redis 服务器连接端口
    spring.redis.port = 6379
    # Redis 服务器连接密码(默认为空)
    spring.redis.password = 
    # 连接超时时间(毫秒)
    spring.redis.timeout = 10000ms
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.lettuce.pool.max-active = 8
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.lettuce.pool.max-wait = -1
    # 连接池中的最小空闲连接
    spring.redis.lettuce.pool.min-idle = 0
    # 连接池中的最大空闲连接
    spring.redis.lettuce.pool.max-idle = 8
    #集群节点
    spring.redis.cluster.nodes = 192.168.110.2:6379,192.168.110.3:6380,192.168.110.4:6381,192.168.110.5:6382,191.168.110.6:6384,192.168.110.7:6385

    #获取失败 最大重定向次数 spring.redis.cluster.max-redirects = 3

    SpringBoot 2.X redis 配置

    package com.example.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    
    /**
     * @Description: RedisConfig  redis 配置 使用 Lettuce 连接池
     * @Author: mingtian
     * @CreateDate: 2020/6/9 9:58
     * @Version: 1.0
     */
    @Configuration
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
            //设置序列化
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
            //配置 redisTemplate
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            //设置redis连接工厂
            template.setConnectionFactory(redisConnectionFactory);
            RedisSerializer<?> stringSerializer = new StringRedisSerializer();
            // key 序列化
            template.setKeySerializer(stringSerializer);
            // value 序列化
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.setHashKeySerializer(stringSerializer);
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
    
    }
  • 相关阅读:
    Git提交错了不用慌,这三招帮你修改记录
    codeforces 1443D,解法简单,思维缜密的动态规划问题
    为什么优秀的人总是少数?我从天文学当中获得了一些启示
    本科入行可能吗?做到这3点,斩获BAT offer不是梦
    裸考了一次雅思,我居然学会了数据分析!
    有了Git这个功能,再也不需要依赖IDE了!
    职场中究竟什么是ownership,你是一个有ownership的人吗?
    为何跳槽不考虑腾讯?聊聊我和鹅厂的一点往事
    【Azure DevOps系列】Azure DevOps EFCore命令式脚本部署到SQL数据库
    Linux查看、开启、关闭防火墙操作
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/13071595.html
Copyright © 2011-2022 走看看