当前位置:   article > 正文

SpringBoot2.x连接自定义前缀的redis_redis自定义前缀 环境变量 springboot

redis自定义前缀 环境变量 springboot

因为项目需要,需通过统一配置中心读取redis的配置信息,而springBoot默认读取的配置信息的前缀为 spring.redis。

步骤如下:

1.需要读取的配置文件 redis.properties

  1. redis.host=192.168.1.10
  2. redis.port=6379

2.程序启动读取配置文件

SpringBoot启动类添加注解   @PropertySource(value = {"classpath:config/redis.properties"})

  1. @SpringBootApplication
  2. @PropertySource(value = {"classpath:config/redis.properties"})
  3. public class Application {
  4. public static void main(String[] args) throws IOException {
  5. SpringApplication.run(KscrmApplication.class, args);
  6. }
  7. }

3.配置RedisConnectionFactory初始化时读取文件中等值

(1)引入相关依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>redis.clients</groupId>
  7. <artifactId>jedis</artifactId>
  8. <version>2.9.0</version>
  9. </dependency>

(2)配置类

  1. @Configuration
  2. @ConditionalOnClass(RedisOperations.class)
  3. @EnableConfigurationProperties(RedisProperties.class)
  4. @Component
  5. public class RedisAutoConfiguration {
  6. @Value("${redis.host}")
  7. private String host;
  8. @Value("${redis.port}")
  9. private int port;
  10. @Bean
  11. public RedisConnectionFactory connectionFactory() {
  12. JedisPoolConfig poolConfig = new JedisPoolConfig();
  13. JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
  14. .usePooling().poolConfig(poolConfig).and().build();
  15. // 单点redis
  16. RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
  17. redisConfig.setHostName(host);
  18. redisConfig.setPort(port);
  19. return new JedisConnectionFactory(redisConfig,clientConfig);
  20. }
  21. @Bean
  22. @ConditionalOnMissingBean(name = "redisTemplate")
  23. public RedisTemplate<Object, Object> redisTemplate() {
  24. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  25. //使用fastjson序列化
  26. FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
  27. // value值的序列化采用fastJsonRedisSerializer
  28. template.setValueSerializer(fastJsonRedisSerializer);
  29. template.setHashValueSerializer(fastJsonRedisSerializer);
  30. // key的序列化采用StringRedisSerializer
  31. template.setKeySerializer(new StringRedisSerializer());
  32. template.setHashKeySerializer(new StringRedisSerializer());
  33. template.setConnectionFactory(connectionFactory());
  34. return template;
  35. }
  36. @Bean
  37. @ConditionalOnMissingBean(StringRedisTemplate.class)
  38. public StringRedisTemplate stringRedisTemplate() {
  39. StringRedisTemplate template = new StringRedisTemplate();
  40. template.setConnectionFactory(connectionFactory());
  41. return template;
  42. }
  43. }

其中FastJsonRedisSerializer类为序列化相关类

  1. public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
  2. public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  3. private Class<T> clazz;
  4. public FastJsonRedisSerializer(Class<T> clazz) {
  5. super();
  6. this.clazz = clazz;
  7. }
  8. @Override
  9. public byte[] serialize(T t) throws SerializationException {
  10. if (null == t) {
  11. return new byte[0];
  12. }
  13. return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
  14. }
  15. @Override
  16. public T deserialize(byte[] bytes) throws SerializationException {
  17. if (null == bytes || bytes.length <= 0) {
  18. return null;
  19. }
  20. String str = new String(bytes, DEFAULT_CHARSET);
  21. return (T) JSON.parseObject(str, clazz);
  22. }
  23. }

 

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

闽ICP备14008679号