一、背景
业务背景就是在springcloud中增加缓存层,只是简单用redis cluster作为缓存组件,设置不同的缓存容器和缓存过期时间,没有涉及缓存的自动刷新。
二、工具
spring boot 2.0.0; spring cloud Finchley.RELEASE; redis cluster 6.2
三、流程
1.pom文件
主项目涉及的依赖
<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
服务项目涉及的依赖
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Camden.SR4</version>-->
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2.服务项目的配置文件信息
redis:
cluster:
nodes: 192.168.110.128:7000,192.168.110.128:7001,192.168.110.128:70002,192.168.110.128:7003,192.168.110.128:7004,192.168.110.128:7005
# 执行命令超时时间 springboot2.x需要指定单位
timeout: 15000ms
# 执行命令超时时间 springboot1.x不需要指定单位
# timeout: 15000
# 跨集群执行命令时要遵循的最大重定向数量
max-redirects: 5
cache:
#缓存的名称集合,多个采用逗号分割
cache-names:
#缓存的类型,官方提供了很多,这里我们填写redis
type: redis
redis:
#是否缓存null数据,默认是false
cache-null-values: false
#redis中缓存超时的时间,默认60000ms
time-to-live: 60000ms
#缓存数据key是否使用前缀,默认是true
use-key-prefix: false
#缓存数据key的前缀,在上面的配置为true时有效
key-prefix:
massage:
data:
name: mydefine
#结构化信息缓存配置,默认超时时间为2m
struct-live-to-time: 2
#对象化信息缓存配置,默认超时时间为6m
object-live-to-time: 6
#默认信息缓存配置,默认超时时间为30s
default-live-to-time: 30
3.缓存配置类
@Configuration
@EnableCaching
public class RedisClusterConfigSpringboot2x extends CachingConfigurerSupport {
@Autowired
private Environment environment;
@Bean
CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//结构化信息缓存配置,默认超时时间为2m
RedisCacheConfiguration structServiceCache = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(environment.getProperty("massage.data.struct-live-to-time",Long.TYPE))).disableCachingNullValues().prefixKeysWith("StructServiceCache");
//对象化信息缓存配置,默认超时时间为6m
RedisCacheConfiguration objectServiceCache = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(environment.getProperty("massage.data.object-live-to-time",Long.TYPE))).disableCachingNullValues().prefixKeysWith("ObjectServiceCache");
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("StructServiceCache", structServiceCache);
redisCacheConfigurationMap.put("ObjectServiceCache", objectServiceCache);
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
//设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现
//ClassLoader loader = this.getClass().getClassLoader();
//JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader);
//RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer);
//RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
//设置默认超过期时间是30秒,这是设置的除了StructServiceCache和ObjectServiceCache的缓存容器的默认超时时间
defaultCacheConfig.entryTtl(Duration.ofSeconds(environment.getProperty("massage.data.default-live-to-time",Long.TYPE)));
//初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
return cacheManager;
}
4.接口调用缓存层
@Cacheable(value = "StructServiceCache",key="#root.methodName+#p0+#p1")
public List<Map<String, Object>> getStructsByItems(String type, String item);
四、总结
springboot 1.x 和2.x,在redis连接上有较大变动。1.x是用的jedis连接池,而2.x默认是用的lettuce.