当前位置:   article > 正文

使用spring-data-redis执行redis命令

multi is currently not support

首先pom.xml文件,添加以下包:

  1. <dependency>
  2. <groupId>org.springframework.data</groupId>
  3. <artifactId>spring-data-redis</artifactId>
  4. <version>1.8.4.RELEASE</version>
  5. </dependency>

在RedisClustersConfiguration.java配置中添加以下代码:

  1. @Configuration
  2. @PropertySource(value = "classpath:config.properties")
  3. public class RedisClustersConfiguration {
  4. @Value("${redis.pool.maxTotal}")
  5. private int maxTotal;//最大连接数
  6. @Value("${redis.pool.maxIdle}")
  7. private int maxIdle;//最大空闲时间
  8. @Value("${redis.pool.numTestsPerEvictionRun}")
  9. private int numTestsPerEvictionRun;//每次最大连接数
  10. @Value("${redis.pool.timeBetweenEvictionRunsMillis}")
  11. private int timeBetweenEvictionRunsMillis;//释放扫描的扫描间隔
  12. @Value("${redis.pool.minEvictableIdleTimeMillis}")
  13. private int minEvictableIdleTimeMillis;//连接的最小空闲时间
  14. @Value("${redis.pool.softMinEvictableIdleTimeMillis}")
  15. private int softMinEvictableIdleTimeMillis;//连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
  16. @Value("${redis.pool.maxWaitMillis}")
  17. private int maxWaitMillis;//获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
  18. @Value("${redis.pool.testOnBorrow}")
  19. private boolean testOnBorrow;//在获得链接的时候检查有效性,默认false
  20. @Value("${redis.pool.testWhileIdle}")
  21. private boolean testWhileIdle;//在空闲时检查有效性,默认false
  22. @Value("${redis.pool.blockWhenExhausted}")
  23. private boolean blockWhenExhausted;//false报异常,true阻塞超时 默认:true
  24. @Value("${redis.cluster.connectionTimeout}")
  25. private int connectionTimeout;
  26. @Value("${redis.cluster.soTimeout}")
  27. private int soTimeout;
  28. @Value("${redis.cluster.maxRedirects}")
  29. private int maxRedirects;
  30. @Value("${redis.cluster.nodes}")
  31. private String redisNodes;
  32. @Value("${redis.cluster.password}")
  33. private String password;
  34. /**
  35. *
  36. * @return
  37. */
  38. @Bean(name = "poolConfig")
  39. @Profile({ "prod", "dev", "test" })
  40. public JedisPoolConfig jedisPoolConfig() {
  41. JedisPoolConfig bean = new JedisPoolConfig();
  42. bean.setMaxTotal(maxTotal);
  43. bean.setMaxIdle(maxIdle);
  44. bean.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
  45. bean.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  46. bean.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  47. bean.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
  48. bean.setMaxWaitMillis(maxWaitMillis);
  49. bean.setTestOnBorrow(testOnBorrow);
  50. bean.setTestWhileIdle(testWhileIdle);
  51. bean.setBlockWhenExhausted(blockWhenExhausted);
  52. return bean;
  53. }
  54. private Set<RedisNode> getRedisNodes() {
  55. Set<RedisNode> jedisClusterNodes = new HashSet<RedisNode>();
  56. //解析集群配置
  57. String[] clusterNodes = redisNodes.split(";");
  58. for (String clusterNode : clusterNodes) {
  59. String[] node = clusterNode.split(":");
  60. String host = node[0];
  61. int port = Integer.parseInt(node[1]);
  62. jedisClusterNodes.add(new RedisNode(host, port));
  63. }
  64. return jedisClusterNodes;
  65. }
  66. /**
  67. *
  68. * @return
  69. */
  70. @Bean(name = "clusterConfig")
  71. @Profile({ "prod", "dev", "test" })
  72. public RedisClusterConfiguration redisClusterConfiguration() {
  73. RedisClusterConfiguration bean = new RedisClusterConfiguration();
  74. bean.setMaxRedirects(maxRedirects);
  75. bean.setClusterNodes(getRedisNodes());
  76. return bean;
  77. }
  78. /**
  79. *
  80. * @param clusterConfig
  81. * @param poolConfig
  82. * @return
  83. */
  84. @Bean(name = "jedisConnectionFactory")
  85. public JedisConnectionFactory jedisConnectionFactory(RedisClusterConfiguration clusterConfig,
  86. JedisPoolConfig poolConfig) {
  87. JedisConnectionFactory bean = new JedisConnectionFactory(clusterConfig, poolConfig);
  88. bean.setPassword(password);
  89. return bean;
  90. }
  91. /**
  92. *
  93. * @return
  94. */
  95. @Bean(name = "stringRedisSerializer")
  96. public StringRedisSerializer stringRedisSerializer() {
  97. return new StringRedisSerializer();
  98. }
  99. /**
  100. *
  101. * @param jedisConnectionFactory
  102. * @param stringRedisSerializer
  103. * @return
  104. */
  105. @SuppressWarnings({ "rawtypes", "unchecked" })
  106. @Bean(name = "redisTemplateTransactional")
  107. public RedisTemplate redisTemplateTransactional(JedisConnectionFactory jedisConnectionFactory,
  108. StringRedisSerializer stringRedisSerializer) {
  109. RedisTemplate bean = new RedisTemplate();
  110. bean.setConnectionFactory(jedisConnectionFactory);
  111. bean.setDefaultSerializer(stringRedisSerializer);
  112. bean.setEnableTransactionSupport(true);
  113. return bean;
  114. }
  115. /**
  116. *
  117. * @param jedisConnectionFactory
  118. * @param stringRedisSerializer
  119. * @return
  120. */
  121. @SuppressWarnings({ "rawtypes", "unchecked" })
  122. @Bean(name = "redisTemplate")
  123. public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory,
  124. StringRedisSerializer stringRedisSerializer) {
  125. RedisTemplate bean = new RedisTemplate();
  126. bean.setConnectionFactory(jedisConnectionFactory);
  127. bean.setDefaultSerializer(stringRedisSerializer);
  128. bean.setEnableTransactionSupport(false);
  129. return bean;
  130. }
  131. }

config.properties配置文件参考如下:

  1. redis.pool.maxTotal=30
  2. redis.pool.maxIdle=10
  3. redis.pool.numTestsPerEvictionRun=1024
  4. redis.pool.timeBetweenEvictionRunsMillis=30000
  5. redis.pool.minEvictableIdleTimeMillis=1800000
  6. redis.pool.softMinEvictableIdleTimeMillis=10000
  7. redis.pool.maxWaitMillis=1500
  8. redis.pool.testOnBorrow=true
  9. redis.pool.testWhileIdle=true
  10. redis.pool.blockWhenExhausted=false
  11. redis.cluster.connectionTimeout=60000
  12. redis.cluster.soTimeout=300000
  13. redis.cluster.maxRedirects=8
  14. redis.cluster.nodes=172.23.25.142:6379;172.23.25.142:6380;172.23.25.142:6381;172.23.25.142:6382;172.23.25.143:6379;172.23.25.143:6380;172.23.25.143:6381;172.23.25.143:6382;172.23.25.144:6379;172.23.25.144:6380;172.23.25.144:6381;172.23.25.144:6382
  15. redis.cluster.password=

Service代码实现如下:

  1. @Service
  2. public class RedisTemplateServiceImpl implements RedisTemplateService{
  3. /*
  4. * redis集群使用事务时,会报MUTLI is currently not supported in cluster mode.
  5. * Redis cluster对多key操作有限,要求命令中所有的key都属于一个slot,才可以被执行。
  6. * 客户端可以对multi-key命令进行拆分,再发给redis。 目前还未找到比较好的解决方法,
  7. * 所以在出现该错误的地方都用了非事务的redisTemplate。由于multi等命令不支持集群,
  8. * 所以弃用,使用redis的lua脚本来完成事务,脚本功能被设计成可以与集群功能保持兼容。
  9. * 但是脚本无法保证操作多key的原子性。
  10. */
  11. @Autowired
  12. private RedisTemplate<String, String> redisTemplateTransactional;
  13. @Autowired
  14. private RedisTemplate<String, String> redisTemplate;
  15. @Override
  16. public boolean hexists(final String key) {
  17. return redisTemplate.execute(new RedisCallback<Boolean>() {
  18. @Override
  19. public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
  20. return connection.exists(key.getBytes());
  21. }
  22. });
  23. }
  24. @Override
  25. public void del(String key) {
  26. redisTemplate.delete(key);
  27. }
  28. @Override
  29. public Long hincrby(String key, String hashkey, long value) {
  30. return redisTemplate.opsForHash().increment(key, hashkey, value);
  31. }
  32. @Override
  33. public void hset(String key, String hashkey, Object value) {
  34. redisTemplate.opsForHash().put(key, hashkey, value);
  35. }
  36. @Override
  37. public void hmset(String key, Map<? extends Object, ? extends Object> map) {
  38. redisTemplate.opsForHash().putAll(key, map);
  39. }
  40. @Override
  41. public String hget(String key, String hashkey) {
  42. return (String) redisTemplate.opsForHash().get(key, hashkey);
  43. }
  44. @Override
  45. public Long lpush(String key, String value) {
  46. return redisTemplate.opsForList().leftPush(key, value);
  47. }
  48. @Override
  49. public String rpop(String key) {
  50. return redisTemplate.opsForList().rightPop(key);
  51. }
  52. }

 

转载于:https://my.oschina.net/u/1445585/blog/1489132

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

闽ICP备14008679号