springboot2.0.x调用redis cluster

一、背景
业务背景就是在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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值