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 ...
随机推荐
- [转帖]KMS 是什么 以及 优缺点
产品激活 比如Windows激活 , office激活 等激活的原理是什么? KMS等激活工具安全吗? http://www.cnblogs.com/flowerslip/p/8370832.html ...
- CMake--变量
1.一般变量 1)CMake变量引用的方式 使用${}进行变量的引用.例如: ${PROJECT_NAME} #返回项目名称 在 IF 等语句中,是直接使用变量名而不通过${}取值. 2)cmake自 ...
- 【学亮IT手记】mysql创建/查看/切换数据库
--创建数据库 create database web_test1 CHARACTER set utf8; --切换数据库 use web_test1; --查看当前使用的数据库 select DAT ...
- Linux安装mysql5.6
安装mysql5.6https://www.cnblogs.com/wangdaijun/p/6132632.html
- maven+springmvc项目启动时,request mapping not found……
springmvc项目跑的好好的,跑着跑着,出现request mapping not found的问题. 第一波,网上查问题,stackoverflow上面的各种配置说明,但是我本地就是没查出问题 ...
- 10分钟让你的代码更加pythonic
参考: https://blog.csdn.net/g8433373/article/details/80709116
- Centso7 简单优化(阿里云服务器)
##.下载常用包 # yum -y install wget net-tools screen lsof tcpdump nc mtr openssl-devel vim bash-completio ...
- linux下使用sha256sum生成sha256校验文件,并校验其一致性
[root@localhost ]# " >test.zip 生成sha256文件校验文件 [root@localhost ]# sha256sum test.zip >test ...
- PHP爬虫框架Beanbun使用
第一 下载安装Beanbun框架 例如: mkdir -p BeanbunTest composer require kiddyu/beanbun 如图所示: 第二 简单下载网页的例子 <?ph ...
- shiro使用ajax登陆实现,success但页面无法跳转的问题
首先:简述一下登陆的后台流程 页面提交——>对应controller中的方法——>对应Realm认证——>controller返回 json 这样,无论成功与否,都有返回值,可以用 ...