java操作redis集群配置[可配置密码]和工具类

时间:2022-12-29 22:21:56

java操作redis集群配置[可配置密码]和工具类

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. <version>2.4.2</version>
  10. </dependency>

版本:jedis2.9.0[此版本可以加入密码配置]+commons-pools2.4.2

配置:

  <context:property-placeholder ignore-unresolvable="true" location="classpath*:cache.properties"/>
  1. <!-- 连接池配置 -->
  2. <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
  3. <!-- 最大连接数 -->
  4. <property name="maxTotal" value="150" />
  5. <!-- 最大空闲连接数 -->
  6. <property name="maxIdle" value="50" />
  7. <!-- 最小空闲连接数 -->
  8. <property name="minIdle" value="10" />
  9. <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
  10. <property name="maxWaitMillis" value="3000" />
  11. <!-- 每次释放连接的最大数目 -->
  12. <property name="numTestsPerEvictionRun" value="100" />
  13. <!-- 释放连接的扫描间隔(毫秒) -->
  14. <property name="timeBetweenEvictionRunsMillis" value="3000" />
  15. <!-- 连接最小空闲时间 -->
  16. <property name="minEvictableIdleTimeMillis" value="1800000" />
  17. <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
  18. <property name="softMinEvictableIdleTimeMillis" value="10000" />
  19. <!-- 在获取连接的时候检查有效性, 默认false -->
  20. <property name="testOnBorrow" value="true" />
  21. <!-- 在空闲时检查有效性, 默认false -->
  22. <property name="testWhileIdle" value="true" />
  23. <!-- 在归还给pool时,是否提前进行validate操作 -->
  24. <property name="testOnReturn" value="true" />
  25. <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
  26. <property name="blockWhenExhausted" value="false" />
  27. </bean>
  28. <!-- jedis集群版配置 -->
  29. <bean id="hostport1" class="redis.clients.jedis.HostAndPort">
  30. <constructor-arg name="host" value="${redis.host}"/>
  31. <constructor-arg name="port" value="${redis.port1}"/>
  32. </bean>
  33. <bean id="hostport2" class="redis.clients.jedis.HostAndPort">
  34. <constructor-arg name="host" value="${redis.host}"/>
  35. <constructor-arg name="port" value="${redis.port2}"/>
  36. </bean>
  37. <bean id="hostport3" class="redis.clients.jedis.HostAndPort">
  38. <constructor-arg name="host" value="${redis.host}"/>
  39. <constructor-arg name="port" value="${redis.port3}"/>
  40. </bean>
  41. <bean id="hostport4" class="redis.clients.jedis.HostAndPort">
  42. <constructor-arg name="host" value="${redis.host}"/>
  43. <constructor-arg name="port" value="${redis.port4}"/>
  44. </bean>
  45. <bean id="hostport5" class="redis.clients.jedis.HostAndPort">
  46. <constructor-arg name="host" value="${redis.host}"/>
  47. <constructor-arg name="port" value="${redis.port5}"/>
  48. </bean>
  49. <bean id="hostport6" class="redis.clients.jedis.HostAndPort">
  50. <constructor-arg name="host" value="${redis.host}"/>
  51. <constructor-arg name="port" value="${redis.port6}"/>
  52. </bean>
  53. <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
  54. <constructor-arg name="jedisClusterNode">
  55. <set>
  56. <ref bean="hostport1"/>
  57. <ref bean="hostport2"/>
  58. <ref bean="hostport3"/>
  59. <ref bean="hostport4"/>
  60. <ref bean="hostport5"/>
  61. <ref bean="hostport6"/>
  62. </set>
  63. </constructor-arg>
  64. <constructor-arg name="connectionTimeout" value="2000"/>
  65. <constructor-arg name="soTimeout" value="2000"/>
  66. <constructor-arg name="maxAttempts" value="3"/>
  67. <constructor-arg name="password" value="${redis.clusterpassword}"/>
  68. <constructor-arg name="poolConfig">
  69. <ref bean="jedisConfig"/>
  70. </constructor-arg>
  71. </bean>
  72. <bean id="jedisClientCluster" class="xx.xxx.xxxxx.xxxx.xxxx.JedisClientCluster"></bean>

创建cache.properties:

redis.host =192.168.xx.xxx
redis.port1=7001 redis.port2=7002 redis.port3=7003 redis.port4=7004 redis.port5=7005 redis.port6=7006 redis.clusterpassword=xxxxxxx

创建接口JedisClient:

  1. import org.codehaus.jackson.type.TypeReference;
  2. import java.util.List;
  3. /**
  4. * Created by gzy on 2017/11/17 17:16.
  5. */
  6. public interface JedisClient {
  7. String get(String key);
  8. <T> T get(String key, TypeReference<T> clazz);
  9. <T> T get(String key, Class<T> clazz);
  10. String get(String key, int select);
  11. void setAndExpire(String key, Object o, int expire);
  12. Long rpush(String key, String string);
  13. // Long del(String... keys);
  14. Long lpush(String key, String string);
  15. void set(String key, Object o);
  16. String set(String key, String value);
  17. String hget(String hkey, String key);
  18. long hset(String hkey, String key, String value);
  19. long incr(String key);
  20. long expire(String key, int second);
  21. long ttl(String key);
  22. long del(String key);
  23. long hdel(String hkey, String key);
  24. Boolean exists(String key);
  25. Long decr(String key);
  26. List<String> brpop(int timeout, String key);
  27. }

jedisClient实现类JedisClientCluster:

  1. /**
  2. * Created by gzy on 2017/11/17 17:17.
  3. */
  4. import org.codehaus.jackson.type.TypeReference;
  5. import com.thinkgem.jeesite.common.utils.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import redis.clients.jedis.JedisCluster;
  8. import java.util.List;
  9. public class JedisClientCluster implements JedisClient {
  10. @Autowired
  11. private JedisCluster jedisCluster;
  12. public <T> T get(String key, TypeReference<T> clazz) {
  13. String json = jedisCluster.get(key);
  14. if (StringUtils.isNotEmpty(json)) {
  15. return JsonUtil.Json2Object(json, clazz);
  16. } else {
  17. return null;
  18. }
  19. }
  20. public <T> T get(String key, Class<T> clazz) {
  21. String json = jedisCluster.get(key);
  22. if (StringUtils.isNotEmpty(json)) {
  23. return JsonUtil.Json2Object(json, clazz);
  24. } else {
  25. return null;
  26. }
  27. }
  28. public void set(String key, Object o) {
  29. String json = JsonUtil.Object2Json(o);
  30. jedisCluster.set(key, json);
  31. }
  32. public void setAndExpire(String key, Object o, int expire) {
  33. String json = JsonUtil.Object2Json(o);
  34. jedisCluster.set(key, json);
  35. jedisCluster.expire(key, expire);
  36. }
  37. // public long del(String key) {
  38. // return jedisCluster.del(key);
  39. // }
  40. public String get(String key) {
  41. return jedisCluster.get(key);
  42. }
  43. public String get(String key, int select) {
  44. jedisCluster.select(select);
  45. return jedisCluster.get(key);
  46. }
  47. @Override
  48. public String set(String key, String value) {
  49. return jedisCluster.set(key, value);
  50. }
  51. @Override
  52. public String hget(String hkey, String key) {
  53. return jedisCluster.hget(hkey, key);
  54. }
  55. @Override
  56. public long hset(String hkey, String key, String value) {
  57. return jedisCluster.hset(hkey, key, value);
  58. }
  59. @Override
  60. public long incr(String key) {
  61. return jedisCluster.incr(key);
  62. }
  63. public Long decr(String key) {
  64. return jedisCluster.decr(key);
  65. }
  66. @Override
  67. public long expire(String key, int second) {
  68. return jedisCluster.expire(key, second);
  69. }
  70. @Override
  71. public long ttl(String key) {
  72. return jedisCluster.ttl(key);
  73. }
  74. @Override
  75. public long del(String key) {
  76. return jedisCluster.del(key);
  77. }
  78. @Override
  79. public long hdel(String hkey, String key) {
  80. return jedisCluster.hdel(hkey, key);
  81. }
  82. public Long rpush(String key, String string) {
  83. return jedisCluster.rpush(key, string);
  84. }
  85. public Long lpush(String key, String string) {
  86. return jedisCluster.lpush(key, string);
  87. }
  88. public Boolean exists(String key) {
  89. return jedisCluster.exists(key);
  90. }
  91. public List<String> brpop(int timeout, String key) {
  92. return jedisCluster.brpop(timeout, key);
  93. }
  94. }

创建测试类ClusterTest:

  1. import xx.xx.xxx.xxxx.JedisClient;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. /**
  8. * Created by gzy on 2017/11/20 15:22.
  9. */
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration("classpath:spring-context-cache.xml")
  12. public class ClusterTest {
  13. // private static JedisClientCluster redisCluster = SpringContextHolder.getBean("jedisClientCluster");
  14. @Autowired
  15. private JedisClient jedisClient;
  16. @Test
  17. public void testJCluster() {
  18. jedisClient.set("test:phone:" + "11111111","hhha");
  19. String result = jedisClient.get("test:phone:" + "11111111")
  20. System.out.println("result==="+result)
  21. }}

原文地址: https://blog.csdn.net/gzy_0922/article/details/78602716

注意:Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.