SpringBoot集成Redis
Jar依賴
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 進行redisTemplate配置的時候需要此Jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
application.properties
# Redis數據庫索引(默認爲0)
spring.redis.database=0
#Redis服務器地址
spring.redis.host=127.0.0.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=1000
核心配置類
package com.qc.springboot.config;
import java.time.Duration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Redis核心配置類,這個類提供了兩個方法(其實是提供了兩個bean,這兩個bean會加載到spring的context裏面.供使用者調用)
* @author Mr.qian
*
*/
@Configuration //這個註解通常和@bean結合使用,當@bean使用到該類的方法上,代表將該方法作爲一個bean交給spring的context管理.\
@EnableCaching //這個註解是允許我們的緩存cache
public class RedisConfig extends CachingConfigurerSupport {
@Bean //此時將redisTeplate加載到spring的context中. applicationContext
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
//創建redisTemplate對象
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
//序列化
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//引入Json串的轉換類
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
//設置objectMapper的訪問權限
om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setConnectionFactory(factory);
//redis key的序列化
redisTemplate.setKeySerializer(redisSerializer);
//redis value的序列化 使用的是jackson2JsonRedisSerializer
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//value的序列化 hashmap的序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
//序列化
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//引入Json串的轉換類
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
//設置objectMapper的訪問權限
om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//序列化配置 亂碼問題解決及緩存時效性
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().
entryTtl(Duration.ofSeconds(30)).
serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).
serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).
disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();
return cacheManager;
}
}
報錯1
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'spring.redis.timeout' to java.time.Duration:
Property: spring.redis.timeout
Value: 1000
Origin: class path resource [application.properties]:22:22
Reason: failed to convert java.lang.String to java.time.Duration
Action:
Update your application's configuration
原因
已經用了連接池,可以不用設置timeout,springboot 裏timeout默認爲0
##########redis#########
# Redis數據庫索引(默認爲0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=127.0.0.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=1000
共同學習,不對的地方歡迎指出!