当前位置:   article > 正文

SpringBoot集成redis使用RedisTemplate_springboot中同时配置redissonclient与redistemplate

springboot中同时配置redissonclient与redistemplate

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.liuyc.redis</groupId>
  7. <artifactId>Redis-Springboot-2022</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-dependencies</artifactId>
  12. <version>2.7.3</version>
  13. </parent>
  14. <packaging>jar</packaging>
  15. <dependencies>
  16. <!-- 引用spring-web开发包 -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. <version>2.2.2.RELEASE</version>
  21. </dependency>
  22. <!-- 配置使用 redis 启动器 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-data-redis</artifactId>
  26. <version>2.7.5</version>
  27. </dependency>
  28. </dependencies>
  29. </project>

application.properties

  1. spring.redis.host=119.23.217.151
  2. #Redis服务器连接端口
  3. spring.redis.port=6379
  4. #Redis服务器连接密码(默认为空)
  5. spring.redis.password=xxxxx
RedisConfig
  1. package com.liuyc.redis.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  4. import com.fasterxml.jackson.annotation.PropertyAccessor;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.redis.connection.RedisConnectionFactory;
  10. import org.springframework.data.redis.core.RedisTemplate;
  11. import org.springframework.data.redis.core.StringRedisTemplate;
  12. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  13. @Configuration
  14. public class RedisConfig {
  15. /**
  16. * 默认是JDK的序列化策略,这里配置redisTemplate采用的是Jackson2JsonRedisSerializer的序列化策略
  17. * @param redisConnectionFactory
  18. * @return
  19. */
  20. @Bean
  21. public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
  22. //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  23. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
  24. ObjectMapper om = new ObjectMapper();
  25. // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
  26. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  27. // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会抛出异常
  28. //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);旧版本
  29. om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
  30. ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
  31. jackson2JsonRedisSerializer.setObjectMapper(om);
  32. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  33. // 配置连接工厂
  34. redisTemplate.setConnectionFactory(redisConnectionFactory);
  35. //使用StringRedisSerializer来序列化和反序列化redis的key值
  36. //redisTemplate.setKeySerializer(new StringRedisSerializer());
  37. redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
  38. // 值采用json序列化
  39. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  40. redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
  41. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  42. redisTemplate.afterPropertiesSet();
  43. return redisTemplate;
  44. }
  45. /**
  46. * 默认采用的是String的序列化策略
  47. * @param redisConnectionFactory
  48. * @return
  49. */
  50. @Bean
  51. public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
  52. StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
  53. stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
  54. return stringRedisTemplate;
  55. }
  56. }

RedisMain

  1. package com.liuyc.redis;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class RedisMain {
  6. public static void main(String[] args) {
  7. SpringApplication.run(RedisMain.class);
  8. }
  9. }
RedisTestController
  1. package com.liuyc.redis.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.data.redis.core.StringRedisTemplate;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class RedisTestController {
  9. @Autowired
  10. private RedisTemplate<String,Object> redisTemplate;
  11. @Autowired
  12. private StringRedisTemplate stringRedisTemplate;
  13. @GetMapping("/getCount")
  14. public Object getCount(){
  15. // Long count = stringRedisTemplate.opsForValue().increment("count");
  16. Long count = redisTemplate.opsForValue().increment("count1");
  17. System.out.println(count);
  18. return count;
  19. }
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/152095
推荐阅读
相关标签
  

闽ICP备14008679号