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;
}
} 相关推荐
王道革 2020-11-25
wangdonghello 2020-11-03
chenhualong0 2020-11-16
聚合室 2020-11-16
koushr 2020-11-12
guoyanga 2020-11-10
fackyou00 2020-11-10
Orangesss 2020-11-03
dongCSDN 2020-10-31
Quietboy 2020-10-30
liuyulong 2020-10-29
fansili 2020-10-29
温攀峰 2020-10-23
jackbon 2020-10-19
kaixinfelix 2020-10-04