码迷,mamicode.com
首页 > 编程语言 > 详细

springboot2.x 与 1.x 配置 redis 区别

时间:2020-06-09 12:47:12      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:osi   ali   tor   配置   最大   maven   script   cti   final   

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;
    }

}

 

springboot2.x 与 1.x 配置 redis 区别

标签:osi   ali   tor   配置   最大   maven   script   cti   final   

原文地址:https://www.cnblogs.com/ming-blogs/p/13071595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!