Springboot 2.X与redis整合
2019-01-17 11:18
561 查看
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>Redis数据库索引(默认为0)
spring.redis.database=0
Redis服务器地址spring.redis.host=127.0.0.1
Redis服务器连接端口spring.redis.port=6379
Redis服务器连接密码(默认为空)spring.redis.password=
连接池最大连接数(使用负值表示没有限制)spring.redis.jedis.pool.max-active=8
连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.jedis.pool.max-wait=-1ms
连接池中的最大空闲连接spring.redis.jedis.pool.max-idle=8
连接池中的最小空闲连接spring.redis.jedis.pool.min-idle=0
连接超时时间(毫秒)spring.redis.timeout=0ms
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); //afterPropertiesSet (初始化操作)加载配置后执行 template.afterPropertiesSet(); return template; }
参考:
https://blog.csdn.net/zhangbeizhen18/article/details/85272094
https://www.geek-share.com/detail/2733657125.html
https://www.cnblogs.com/superfj/p/9232482.html
相关文章推荐
- Springboot 2.X中Spring-cache与redis整合
- SpringBoot2.X 整合RedisTemplate 简单实现消息队列
- Spring Boot 2.x整合Redis
- springboot同时整合redis和ehcache
- Spring Boot整合Redis
- springboot整合redis
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- spring boot 整合mybatis、redis、 spring mvc基本配置
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- springboot整合redis
- SpringBoot(二十七)整合Redis之分布式锁
- Springboot中Spring-cache与redis整合
- Spring Boot 整合 Redis 实现缓存操作
- SpringBoot2.x 整合Druid数据源(详解及配置配置)
- SpringBoot整合Redis及Redis工具类撰写
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
- Spring Boot 整合 Redis 实现缓存操作
- Redis学习(3.1)spring boot整合Redis速成
- springboot与redis的注解式整合
- (七)Spring Boot 整合Redis