java操作redis集群配置[可配置密码]和工具类(比较好用)

 

转:

 

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

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.    
  29.    
  30.   <!-- jedis集群版配置 -->
  31.   <bean id="hostport1" class="redis.clients.jedis.HostAndPort">
  32.   <constructor-arg name="host" value="${redis.host}"/>
  33.   <constructor-arg name="port" value="${redis.port1}"/>
  34.   </bean>
  35.    
  36.   <bean id="hostport2" class="redis.clients.jedis.HostAndPort">
  37.   <constructor-arg name="host" value="${redis.host}"/>
  38.   <constructor-arg name="port" value="${redis.port2}"/>
  39.   </bean>
  40.    
  41.   <bean id="hostport3" class="redis.clients.jedis.HostAndPort">
  42.   <constructor-arg name="host" value="${redis.host}"/>
  43.   <constructor-arg name="port" value="${redis.port3}"/>
  44.   </bean>
  45.    
  46.   <bean id="hostport4" class="redis.clients.jedis.HostAndPort">
  47.   <constructor-arg name="host" value="${redis.host}"/>
  48.   <constructor-arg name="port" value="${redis.port4}"/>
  49.   </bean>
  50.    
  51.   <bean id="hostport5" class="redis.clients.jedis.HostAndPort">
  52.   <constructor-arg name="host" value="${redis.host}"/>
  53.   <constructor-arg name="port" value="${redis.port5}"/>
  54.   </bean>
  55.    
  56.   <bean id="hostport6" class="redis.clients.jedis.HostAndPort">
  57.   <constructor-arg name="host" value="${redis.host}"/>
  58.   <constructor-arg name="port" value="${redis.port6}"/>
  59.   </bean>
  60.    
  61.   <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
  62.   <constructor-arg name="jedisClusterNode">
  63.   <set>
  64.   <ref bean="hostport1"/>
  65.   <ref bean="hostport2"/>
  66.   <ref bean="hostport3"/>
  67.   <ref bean="hostport4"/>
  68.   <ref bean="hostport5"/>
  69.   <ref bean="hostport6"/>
  70.   </set>
  71.   </constructor-arg>
  72.   <constructor-arg name="connectionTimeout" value="2000"/>
  73.   <constructor-arg name="soTimeout" value="2000"/>
  74.   <constructor-arg name="maxAttempts" value="3"/>
  75.   <constructor-arg name="password" value="${redis.clusterpassword}"/>
  76.   <constructor-arg name="poolConfig">
  77.   <ref bean="jedisConfig"/>
  78.   </constructor-arg>
  79.   </bean>
  80.   <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.    
  3.   import java.util.List;
  4.    
  5.   /**
  6.   * Created by gzy on 2017/11/17 17:16.
  7.   */
  8.    
  9.   public interface JedisClient {
  10.   String get(String key);
  11.    
  12.   <T> T get(String key, TypeReference<T> clazz);
  13.    
  14.   <T> T get(String key, Class<T> clazz);
  15.    
  16.   String get(String key, int select);
  17.    
  18.   void setAndExpire(String key, Object o, int expire);
  19.    
  20.   Long rpush(String key, String string);
  21.    
  22.   // Long del(String... keys);
  23.   Long lpush(String key, String string);
  24.    
  25.   void set(String key, Object o);
  26.    
  27.   String set(String key, String value);
  28.    
  29.   String hget(String hkey, String key);
  30.    
  31.   long hset(String hkey, String key, String value);
  32.    
  33.   long incr(String key);
  34.    
  35.   long expire(String key, int second);
  36.    
  37.   long ttl(String key);
  38.    
  39.   long del(String key);
  40.    
  41.   long hdel(String hkey, String key);
  42.    
  43.   Boolean exists(String key);
  44.    
  45.   Long decr(String key);
  46.    
  47.   List<String> brpop(int timeout, String key);
  48.    
  49.   }

 

 

jedisClient实现类JedisClientCluster:

 

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

 

 

创建测试类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.   /**
  9.   * Created by gzy on 2017/11/20 15:22.
  10.   */
  11.   @RunWith(SpringJUnit4ClassRunner.class)
  12.   @ContextConfiguration("classpath:spring-context-cache.xml")
  13.   public class ClusterTest {
  14.   // private static JedisClientCluster redisCluster = SpringContextHolder.getBean("jedisClientCluster");
  15.   @Autowired
  16.   private JedisClient jedisClient;
  17.    
  18.   @Test
  19.   public void testJCluster() {
  20.   jedisClient.set("test:phone:" + "11111111","hhha");
  21.   String result = jedisClient.get("test:phone:" + "11111111")
  22.   System.out.println("result==="+result)
  23.    
  24.   }}



 原文地址:

上一篇:115资源分享


下一篇:bloom-server 基于 rust 编写的 rest api cache 中间件