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 ...
随机推荐
- CentOS7安装k8s
借鉴博客:https://www.cnblogs.com/xkops/p/6169034.html 此博客里面有每个k8s配置文件的注释:https://blog.csdn.net/qq_359048 ...
- C# Note23: 如何自定义类型使用foreach循环
前言 在foreach语句代码中,我们经常是对List,Collection,Dictionary等类型的数据进行操作,不过C#允许用户自定义自己的类型来使用foreach语句.那么自定义类型能够使用 ...
- python语法糖/装饰器
1.python高阶函数和嵌套函数 1.1高阶函数 def func1(x): return x**2 def func2(x): return x**3 def func(x,y): return ...
- 【纪录】Hash about
backup a easy implement # coding: utf-8 def add(k, v): pass def get(target): pass class LinearMap(ob ...
- 从Oracle数据库中查询前几个月数据时需要注意的一些问题
在最近的一个项目中,有一个需求就是要查询数据库中前几个月的历史数据,但是由于自己考虑不全面造成了程序的bug,现在将这一块好好作一个总结,希望以后不再犯这种很低级的错误,首先贴出查询中用到的一个子函数 ...
- indexOf刚开始写成IndexOf出错
{{# if(d.fronturlmin ==null||d.fronturlmin ==""){ }} <img src="@System.Configurati ...
- linux下ssh无法连接的原因
在虚拟机上安装了ubuntu16.04 server,用本机 ssh 连接的时候 无法连接上: 忽然想起在安装的时候有个openssh好像没有勾选,所以在虚拟机上 apt install openss ...
- 另一个ado工具类
using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using Sy ...
- maven项目 报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
ssm的项目如果在mapper.xml mapper接口 配置没问题的情况下 项目依然报org.apache.ibatis.binding.BindingException: Invalid bo ...
- 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 ...