spring-boot-starter-redis配置详解
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.;:
- @Configuration
- @EnableCaching
- public class RedisConfig extends CachingConfigurerSupport{
- @Bean
- public KeyGenerator keyGenerator() {
- return new KeyGenerator() {
- public Object generate(Object target, Method method, Object... params) {
- StringBuilder sb = new StringBuilder();
- sb.append(target.getClass().getName());
- sb.append("_").append(method.getName());
- for (Object obj : params) {
- sb.append("_").append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
- @SuppressWarnings("rawtypes")
- @Bean
- public CacheManager cacheManager(RedisTemplate redisTemplate) {
- RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
- //设置缓存过期时间
- //rcm.setDefaultExpiration(60);//秒
- return rcm;
- }
- @Bean
- public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
- StringRedisTemplate template = new StringRedisTemplate(factory);
- @SuppressWarnings({ "rawtypes", "unchecked" })
- Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
- ObjectMapper om = new ObjectMapper();
- om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
- jackson2JsonRedisSerializer.setObjectMapper(om);
- template.setValueSerializer(jackson2JsonRedisSerializer);
- template.afterPropertiesSet();
- JedisConnectionFactory jc = (JedisConnectionFactory) factory;
- System.out.println(jc.getHostName());
- return template;
- }
- }
如果你还配置了如连接池之类的参数,在上边配置类中加入:
- @Bean
- public JedisConnectionFactory redisConnectionFactory() {
- JedisConnectionFactory factory = new JedisConnectionFactory();
- factory.setHostName(host);
- factory.setPort(port);
- factory.setPassword(password);
- factory.setTimeout(timeout); //设置连接超时时间
- return factory;
- }
使用factory进行set你所配置的值即可。
附带解释一点就是在配置类中注入配置文件中的属性方案有多种,如需了解可参考博客:
StringRedisTemplate与RedisTemplate使用时的注意事项:
1、StringRedisTemplate是RedisTemplate的唯一子类
2、StringRedisTemplate默认采用的key序列化方式为setKeySerializer(stringSerializer);此时在使用Spring的缓存注解如@Cacheable的key属性设置值时,就需
- 要注意如果参数类型为Long那么会出不能进行String类型转换异常。
- 3、RedisTemplate默认使用的序列化方式为JdkSerializationRedisSerializer,它就没有上边的问题。因为它的序列化方法为serialize(Object object)
spring-boot-starter-redis配置详解的更多相关文章
- spring boot application properties配置详解
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- Spring Boot整合Mybatis配置详解
首先,你得有个Spring Boot项目. 平时开发常用的repository包在mybatis里被替换成了mapper. 配置: 1.引入依赖: <dependency> <gro ...
- (转)Spring boot——logback.xml 配置详解(四)<filter>
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 filter的使用 <filter>: Logback的过滤器基于三值逻辑( ...
- (转)Spring boot——logback.xml 配置详解(二)
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 根节点<configuration>包含的属性 scan: 当此属性设置为t ...
- Spring boot——logback.xml 配置详解(四)<filter>
阅读目录 1 filter的使用 2 常用的过滤器 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 filter的使用 < ...
- Spring boot——logback.xml 配置详解(二)
阅读目录 1 根节点包含的属性 2 根节点的子节点 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 根节点<config ...
- (转)Spring boot——logback.xml 配置详解(三)<appender>
文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 1 appender <appender>是<configuration& ...
- Spring boot——logback.xml 配置详解(三)<appender>
阅读目录 1 appender 2 encoder 文章转载自:http://aub.iteye.com/blog/1101260,在此对作者的辛苦表示感谢! 回到顶部 1 appender < ...
- Spring Boot Actuator监控使用详解
在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...
- Spring Boot 之使用 Json 详解
Spring Boot 之使用 Json 详解 简介 Spring Boot 支持的 Json 库 Spring Web 中的序列化.反序列化 指定类的 Json 序列化.反序列化 @JsonTest ...
随机推荐
- [转帖]关于CP936
来源: 知乎:https://www.zhihu.com/question/35609295/answer/63780022 CP936和UTF-8本身和Python是毫无关联的. CP936其实就是 ...
- Vue 中提示报错 handlers[i].call is not a function解决方法
Vue 中提示警告 TypeError: handlers[i].call is not a function at callHook (vue.esm.js?a026:2921) at Object ...
- ssl证书部署问题
问:我现在得到的ssl证书是.crt和.key两个在nginx环境下部署的证书,如果我们改用是tomcat,现在把这两个文件合成了.jks给tomcat使用,合成的时候输入的jks密码是不是就是部署在 ...
- 手把手制作一个简单的IDEA插件(环境搭建Demo篇)
新建IDEA插件File --> new --> Project--> Intellij PlatForm Plugin-->Next-->填好项目名OK 编写插件新建工 ...
- 一、Dev单元格
二.获取表格数据 int selectRow = gridView1.GetSelectedRows()[0]; string id = this.gridView1.GetRowCellValue( ...
- vue-cli: preset预设
preset:预设 vue create demo01 过程中,会保存预设,自动保存着 .vuerc 文件中 .vuerc 文件的位置:C:\Users\Administrator C:\Users\ ...
- mybatis 批量查询参数语句
在mybatis 传入数组在sql语句中进行查询 1.传入一个map集合,已或者的形式拼接数组循环 <select id="selectUserByList" parame ...
- 各个版本spring的jar包以及源码下载地址,目前最高版本到spring4.3.8,留存备用:
http://maven.springframework.org/release/org/springframework/spring/
- python 计算机基础
1.什么是编程语言. 语言是一个事物与另一个事物沟通的介质. 编程语言是程序员与计算机沟通的介质. 2.什么是编程. 编程是人按照某种语法规范设计出计算机能够识别的语言 表达的结果是程序,程序就是一系 ...
- 关于Binder,作为应用开发者你需要知道的全部
作者:rushjs https://www.jianshu.com/p/062a6e4f5cbe github 地址: https://github.com/rushgit/zhongwenjun.g ...