RedisConfig.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.aijia.peixun.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import redis.clients.jedis.HostAndPort;
  6. import redis.clients.jedis.JedisCluster;
  7. import redis.clients.jedis.JedisPoolConfig;
  8. import java.util.HashSet;
  9. @Configuration
  10. public class RedisConfig {
  11. // @Value("${spring.redis.timeout}")
  12. @Value("3000")
  13. private int timeout;
  14. // @Value("${spring.redis.cluster.pool.max-idle}")
  15. @Value("8")
  16. private int maxIdle;
  17. // @Value("${spring.redis.cluster.pool.max-wait}")
  18. @Value("-1")
  19. private long maxWaitMillis;
  20. // @Value("${spring.redis.block-when-exhausted}")
  21. @Value("true")
  22. private boolean blockWhenExhausted;
  23. // @Value("${spring.redis.cluster.nodes}")
  24. // @Value("192.168.2.118:7000,192.168.2.118:7001,192.168.2.118:7002,192.168.2.118:7003,192.168.2.118:7004,192.168.2.118:7005")
  25. @Value("47.119.147.170:7000")
  26. private String clusterNodes;
  27. // @Value("${spring.redis.password}")
  28. // @Value("123")
  29. @Value("aijia@78342")
  30. private String password;
  31. @Bean
  32. public JedisCluster redisPoolFactory() throws Exception {
  33. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  34. jedisPoolConfig.setMaxIdle(maxIdle);
  35. jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
  36. // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
  37. jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
  38. // 是否启用pool的jmx管理功能, 默认true
  39. jedisPoolConfig.setJmxEnabled(true);
  40. String[] cNodes = clusterNodes.split(",");
  41. HashSet<HostAndPort> nodes = new HashSet<>();
  42. // 分割集群节点
  43. for (String node : cNodes) {
  44. String[] hp = node.split(":");
  45. nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
  46. }
  47. // 创建集群对象
  48. // JedisCluster jedisCluster = new JedisCluster(nodes, timeout,
  49. // jedisPoolConfig);
  50. return new JedisCluster(nodes, timeout, timeout, 5, password, jedisPoolConfig);
  51. // return new JedisCluster(nodes, timeout, jedisPoolConfig);
  52. }
  53. }