springboot2.4 整合 redis 配置详解

70阅读 0评论2021-04-29 fhadmin
分类:Java

1. 引入pom

点击(此处)折叠或打开

  1. <!-- Spring Data Redis的启动器 -->
  2.      <dependency>
  3.      <groupId>org.springframework.boot</groupId>
  4.      <artifactId>spring-boot-starter-data-redis</artifactId>
  5.      <exclusions>
  6.          <exclusion>
  7.          <groupId>io.lettuce</groupId>
  8.          <artifactId>lettuce-core</artifactId>
  9.          </exclusion>
  10.          </exclusions>
  11.      </dependency>
  12.      <dependency>
  13.          <groupId>redis.clients</groupId>
  14.          <artifactId>jedis</artifactId>
  15.         </dependency>
2. application.properties 配置文件

点击(此处)折叠或打开

  1. #===========Redis配置===========
  2. # Redis数据库索引(默认为0)
  3. spring.redis.database=0
  4. # Redis服务器地址
  5. spring.redis.host=127.0.0.1
  6. # Redis服务器连接端口
  7. spring.redis.port=6379
  8. # Redis服务器连接密码(默认为空)
  9. spring.redis.password=root
  10. # 连接池最大连接数(使用负值表示没有限制)
  11. spring.redis.pool.max-active=200
  12. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  13. spring.redis.pool.max-wait=-1
  14. # 连接池中的最大空闲连接
  15. spring.redis.pool.max-idle=10
  16. # 连接池中的最小空闲连接
  17. spring.redis.pool.min-idle=0
  18. # 连接超时时间(毫秒)
  19. spring.redis.timeout=2000ms
  20. spring.redis.jedis.pool.max-wait=-1ms
  21. #===========Redis配置===========
3.  config 配置类

点击(此处)折叠或打开

  1. package org.fh.config;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;

  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
  10. import com.fasterxml.jackson.annotation.PropertyAccessor;
  11. import com.fasterxml.jackson.annotation.JsonAutoDetect;

  12. /**
  13.  * 说明:Redis
  14.  * 作者:FH Admin
  15.  * java:www fhadmin org
  16.  */
  17. @Configuration
  18. public class RedisConfig {

  19.     @Bean
  20.     @SuppressWarnings("all")
  21.     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  22.         RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  23.         template.setConnectionFactory(factory);
  24.         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  25.         ObjectMapper om = new ObjectMapper();
  26.         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  27.         om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL);
  28.         jackson2JsonRedisSerializer.setObjectMapper(om);
  29.         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  30.         // key采用String的序列化方式
  31.         template.setKeySerializer(stringRedisSerializer);
  32.         // hash的key也采用String的序列化方式
  33.         template.setHashKeySerializer(stringRedisSerializer);
  34.         // value序列化方式采用jackson
  35.         template.setValueSerializer(jackson2JsonRedisSerializer);
  36.         // hash的value序列化方式采用jackson
  37.         template.setHashValueSerializer(jackson2JsonRedisSerializer);
  38.         template.afterPropertiesSet();
  39.         return template;
  40.     }

  41. }




上一篇:Oracle创建表空间和用户
下一篇:zuul 替代者 微服务网关gateway