Springboot 集成 redis单点和redis集群

系统管理员 2023-06-27 11:30 74阅读 0赞

Redis 现在是我们最常用的 Key-Value数据库之一,所以在写springboot微服务的时候,redis的集成当然也是必须的。

首先新建一个springboot项目,

一、引入Redis依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

二、boot yml 配置

1、如果是redis单点的话,配置如下,

  1. redis:
  2. jedis:
  3. pool:
  4. max-active: 8
  5. max-wait: -1ms
  6. min-idle: 0
  7. max-idle: 8
  8. host: 127.0.0.1
  9. port: 6379

2、如果是redis集群(三节点),配置如下,

  1. redis:
  2. cluster:
  3. nodes: 101.2.50.101:6379,101.2.50.102:6379,101.2.50.103:6379
  4. jedis:
  5. pool:
  6. max-active: 8
  7. max-wait: -1ms
  8. min-idle: 0
  9. max-idle: 8

3、相关配置参数

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

三、Redis 配置类

原生的 RedisTemplate 不好用,推荐如下重写,重新使用 fastjson 序列化。

  1. package com.zszq.bigdata.hbaseservice.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  8. import org.springframework.data.redis.cache.RedisCacheManager;
  9. import org.springframework.data.redis.connection.RedisConnectionFactory;
  10. import org.springframework.data.redis.core.RedisTemplate;
  11. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  12. import org.springframework.data.redis.serializer.RedisSerializer;
  13. import org.springframework.data.redis.serializer.StringRedisSerializer;
  14. import java.time.Duration;
  15. /*
  16. * created by shipfei
  17. * on 2019/5/23, 16:49
  18. * motto: Saying and doing are two different things.
  19. */
  20. @Configuration
  21. public class RedisConfig {
  22. @Bean
  23. public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory){
  24. return RedisCacheManager.builder(connectionFactory)
  25. .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(15))) // 缓存过期时间
  26. .transactionAware()
  27. .build();
  28. }
  29. @Bean
  30. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
  31. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  32. ObjectMapper om = new ObjectMapper();
  33. RedisSerializer stringSerializer = new StringRedisSerializer();
  34. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  35. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  36. jackson2JsonRedisSerializer.setObjectMapper(om);
  37. RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  38. template.setConnectionFactory(redisConnectionFactory);
  39. template.setKeySerializer(stringSerializer);
  40. template.setValueSerializer(jackson2JsonRedisSerializer);
  41. template.setHashKeySerializer(stringSerializer);
  42. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  43. template.afterPropertiesSet();
  44. return template;
  45. }
  46. }

四、使用

完成以上三步后,就可以在使用redis来做缓存了,使用时注入RedisTemplate即可。

  1. @RestController
  2. public class DayClickController {
  3. @Autowired
  4. private DayClickService dayClickService;
  5. @Autowired
  6. private RedisTemplate redisTemplate;
  7. @GetMapping("/getDayData")
  8. public ResponseData getDayData(@RequestParam(value = "custNo") String custNo,
  9. @RequestParam(value = "page") String page){
  10. String rowKey = custNo + "_" + page;
  11. Map<String, String> param = new HashMap<>();
  12. param.put("rowKey", rowKey);
  13. redisTemplate.opsForValue().increment("request-getDayData", 1); // 点击量
  14. ResponseData responseData = dayClickService.getDayClickByRowKey(param);
  15. return responseData;
  16. }
  17. }

发表评论

表情:
评论列表 (有 0 条评论,74人围观)

还没有评论,来说两句吧...

相关阅读