RedisConfig.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.genersoft.iot.vmp.conf;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.cache.annotation.CachingConfigurerSupport;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.data.redis.connection.RedisConnectionFactory;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  10. import org.springframework.data.redis.serializer.StringRedisSerializer;
  11. import com.alibaba.fastjson.parser.ParserConfig;
  12. import com.genersoft.iot.vmp.utils.redis.FastJsonRedisSerializer;
  13. import redis.clients.jedis.JedisPool;
  14. import redis.clients.jedis.JedisPoolConfig;
  15. /**
  16. * @description:Redis中间件配置类,使用spring-data-redis集成,自动从application.yml中加载redis配置
  17. * @author: swwheihei
  18. * @date: 2019年5月30日 上午10:58:25
  19. *
  20. */
  21. @Configuration
  22. public class RedisConfig extends CachingConfigurerSupport {
  23. @Value("${spring.redis.host}")
  24. private String host;
  25. @Value("${spring.redis.port}")
  26. private int port;
  27. @Value("${spring.redis.database}")
  28. private int database;
  29. @Value("${spring.redis.password}")
  30. private String password;
  31. @Value("${spring.redis.timeout}")
  32. private int timeout;
  33. @Value("${spring.redis.poolMaxTotal:1000}")
  34. private int poolMaxTotal;
  35. @Value("${spring.redis.poolMaxIdle:500}")
  36. private int poolMaxIdle;
  37. @Value("${spring.redis.poolMaxWait:5}")
  38. private int poolMaxWait;
  39. @Bean
  40. public JedisPool jedisPool() {
  41. if (StringUtils.isBlank(password)) {
  42. password = null;
  43. }
  44. JedisPoolConfig poolConfig = new JedisPoolConfig();
  45. poolConfig.setMaxIdle(poolMaxIdle);
  46. poolConfig.setMaxTotal(poolMaxTotal);
  47. // 秒转毫秒
  48. poolConfig.setMaxWaitMillis(poolMaxWait * 1000L);
  49. JedisPool jp = new JedisPool(poolConfig, host, port, timeout * 1000, password, database);
  50. return jp;
  51. }
  52. @Bean("redisTemplate")
  53. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  54. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  55. template.setConnectionFactory(redisConnectionFactory);
  56. // 使用fastjson进行序列化处理,提高解析效率
  57. FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<Object>(Object.class);
  58. // value值的序列化采用fastJsonRedisSerializer
  59. template.setValueSerializer(serializer);
  60. template.setHashValueSerializer(serializer);
  61. // key的序列化采用StringRedisSerializer
  62. template.setKeySerializer(new StringRedisSerializer());
  63. template.setHashKeySerializer(new StringRedisSerializer());
  64. template.setConnectionFactory(redisConnectionFactory);
  65. // 使用fastjson时需设置此项,否则会报异常not support type
  66. ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
  67. return template;
  68. }
  69. /**
  70. * redis消息监听器容器 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
  71. * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
  72. *
  73. * @param connectionFactory
  74. * @return
  75. */
  76. @Bean
  77. RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
  78. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  79. container.setConnectionFactory(connectionFactory);
  80. return container;
  81. }
  82. }