spring-boot-starter-redis主要是通过配置RedisConnectionFactory中的相关参数去实现连接redis service。RedisConnectionFactory是一个接口,有如下4个具体的实现类,我们通常使用的是JedisConnectionFactory。

在spring boot的配置文件中redis的基本配置如下:

# Redis服务器地址
spring.redis.host=192.168.0.58
# Redis服务器连接端口
spring.redis.port=6379  
# Redis服务器连接密码(默认为空,如果redis服务端配置文件开启了requirepass 密码,此处就应该填写相应的配置密码)
spring.redis.password=  
# 连接超时时间(毫秒)
spring.redis.timeout=0

上边这4项是在JedisConnectionFactory类中的基本配置项,里边其实还包含了一些比如连接池,集群,主从,哨兵等的配置,这里先简单介绍下连接池(JedisPoolConfig),需要了解其它配置了可以看下源码。GenericObjectPoolConfig是JedisPoolConfig的父类,主要提供了maxTotal、maxIdle、maxIdle共三个参数的配置,其中还设置了默认的参数。

# 连接池最大连接数(使用负值表示没有限制,对应maxTotal)

spring.redis.pool.max-active=8

# 连接池中的最大空闲连接

spring.redis.pool.max-idle=8

# 连接池中的最小空闲连接

spring.redis.pool.min-idle=0

配置文件配置好后,还需要建立一个redis的配置类,主要用来配置key和value的序列化及加载配置文件中的相关参数

如果你只需要使用基本的redis配置,那么使用如下配置类即可,spring boot会自动扫描redis的基本配置,但是有一项要注意那就是password,如果你在配置文件中设置了password,那么就必须在配置类中手工注入JedisConnectionFactory中,否则会在启动过程中报NOAUTH Authentication required.;:

  1.  
    @Configuration
  2.  
    @EnableCaching
  3.  
    public class RedisConfig extends CachingConfigurerSupport{
  4.  
     
  5.  
    @Bean
  6.  
    public KeyGenerator keyGenerator() {
  7.  
    return new KeyGenerator() {
  8.  
     
  9.  
    public Object generate(Object target, Method method, Object... params) {
  10.  
    StringBuilder sb = new StringBuilder();
  11.  
                    sb.append(target.getClass().getName());
  12.  
                    sb.append("_").append(method.getName());
  13.  
                    for (Object obj : params) {
  14.  
                        sb.append("_").append(obj.toString());
  15.  
                    }
  16.  
                    return sb.toString();
  17.  
    }
  18.  
    };
  19.  
    }
  20.  
     
  21.  
    @SuppressWarnings("rawtypes")
  22.  
    @Bean
  23.  
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
  24.  
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
  25.  
    //设置缓存过期时间
  26.  
    //rcm.setDefaultExpiration(60);//秒
  27.  
    return rcm;
  28.  
    }
  29.  
     
  30.  
     
  31.  
    @Bean
  32.  
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  33.  
    StringRedisTemplate template = new StringRedisTemplate(factory);
  34.  
    @SuppressWarnings({ "rawtypes", "unchecked" })
  35.  
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  36.  
    ObjectMapper om = new ObjectMapper();
  37.  
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  38.  
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  39.  
    jackson2JsonRedisSerializer.setObjectMapper(om);
  40.  
    template.setValueSerializer(jackson2JsonRedisSerializer);
  41.  
    template.afterPropertiesSet();
  42.  
    JedisConnectionFactory jc = (JedisConnectionFactory) factory;
  43.  
    System.out.println(jc.getHostName());
  44.  
    return template;
  45.  
    }
  46.  
     
  47.  
    }

如果你还配置了如连接池之类的参数,在上边配置类中加入:

  1.  
    @Bean
  2.  
    public JedisConnectionFactory redisConnectionFactory() {
  3.  
    JedisConnectionFactory factory = new JedisConnectionFactory();
  4.  
    factory.setHostName(host);
  5.  
    factory.setPort(port);
  6.  
    factory.setPassword(password);
  7.  
    factory.setTimeout(timeout); //设置连接超时时间
  8.  
    return factory;
  9.  
    }
使用factory进行set你所配置的值即可。

附带解释一点就是在配置类中注入配置文件中的属性方案有多种,如需了解可参考博客:

点击打开链接

StringRedisTemplate与RedisTemplate使用时的注意事项:
1、StringRedisTemplate是RedisTemplate的唯一子类
2、StringRedisTemplate默认采用的key序列化方式为setKeySerializer(stringSerializer);此时在使用Spring的缓存注解如@Cacheable的key属性设置值时,就需
  1.  
    要注意如果参数类型为Long那么会出不能进行String类型转换异常。
  2.  
    3、RedisTemplate默认使用的序列化方式为JdkSerializationRedisSerializer,它就没有上边的问题。因为它的序列化方法为serialize(Object object)

spring-boot-starter-redis配置详解的更多相关文章

  1. spring boot application properties配置详解

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  2. Spring Boot整合Mybatis配置详解

    首先,你得有个Spring Boot项目. 平时开发常用的repository包在mybatis里被替换成了mapper. 配置: 1.引入依赖: <dependency> <gro ...

  3. (转)Spring boot——logback.xml 配置详解(四)<filter>

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...

  4. (转)Spring boot——logback.xml 配置详解(二)

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 根节点<configuration>包含的属性 scan: 当此属性设置为t ...

  5. Spring boot——logback.xml 配置详解(四)<filter>

    阅读目录 1 filter的使用 2 常用的过滤器 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 filter的使用 < ...

  6. Spring boot——logback.xml 配置详解(二)

    阅读目录 1 根节点包含的属性 2 根节点的子节点 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 根节点<config ...

  7. (转)Spring boot——logback.xml 配置详解(三)<appender>

    文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 appender <appender>是<configuration& ...

  8. Spring boot——logback.xml 配置详解(三)<appender>

    阅读目录 1 appender 2  encoder 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 appender < ...

  9. Spring Boot Actuator监控使用详解

    在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...

  10. Spring Boot 之使用 Json 详解

    Spring Boot 之使用 Json 详解 简介 Spring Boot 支持的 Json 库 Spring Web 中的序列化.反序列化 指定类的 Json 序列化.反序列化 @JsonTest ...

随机推荐

  1. CentOS7安装k8s

    借鉴博客:https://www.cnblogs.com/xkops/p/6169034.html 此博客里面有每个k8s配置文件的注释:https://blog.csdn.net/qq_359048 ...

  2. C# Note23: 如何自定义类型使用foreach循环

    前言 在foreach语句代码中,我们经常是对List,Collection,Dictionary等类型的数据进行操作,不过C#允许用户自定义自己的类型来使用foreach语句.那么自定义类型能够使用 ...

  3. python语法糖/装饰器

    1.python高阶函数和嵌套函数 1.1高阶函数 def func1(x): return x**2 def func2(x): return x**3 def func(x,y): return ...

  4. 【纪录】Hash about

    backup a easy implement # coding: utf-8 def add(k, v): pass def get(target): pass class LinearMap(ob ...

  5. 从Oracle数据库中查询前几个月数据时需要注意的一些问题

    在最近的一个项目中,有一个需求就是要查询数据库中前几个月的历史数据,但是由于自己考虑不全面造成了程序的bug,现在将这一块好好作一个总结,希望以后不再犯这种很低级的错误,首先贴出查询中用到的一个子函数 ...

  6. indexOf刚开始写成IndexOf出错

    {{# if(d.fronturlmin ==null||d.fronturlmin ==""){ }} <img src="@System.Configurati ...

  7. linux下ssh无法连接的原因

    在虚拟机上安装了ubuntu16.04 server,用本机 ssh 连接的时候 无法连接上: 忽然想起在安装的时候有个openssh好像没有勾选,所以在虚拟机上 apt install openss ...

  8. 另一个ado工具类

    using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using Sy ...

  9. maven项目 报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    ssm的项目如果在mapper.xml  mapper接口 配置没问题的情况下  项目依然报org.apache.ibatis.binding.BindingException: Invalid bo ...

  10. Build 2017 Revisited: .NET, XAML, Visual Studio

    For the next couple months we're going to revisit Build 2017, each post focusing on different aspect ...