转:
java操作redis集群配置[可配置密码]和工具类
-
<dependency>
-
<groupId>redis.clients</groupId>
-
<artifactId>jedis</artifactId>
-
<version>2.9.0</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.commons</groupId>
-
<artifactId>commons-pool2</artifactId>
-
<version>2.4.2</version>
-
</dependency>
注意:
版本:jedis2.9.0[此版本可以加入密码配置]+commons-pools2.4.2
配置:
<context:property-placeholder ignore-unresolvable="true" location="classpath*:cache.properties"/>
-
<!-- 连接池配置 -->
-
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
-
<!-- 最大连接数 -->
-
<property name="maxTotal" value="150" />
-
<!-- 最大空闲连接数 -->
-
<property name="maxIdle" value="50" />
-
<!-- 最小空闲连接数 -->
-
<property name="minIdle" value="10" />
-
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
-
<property name="maxWaitMillis" value="3000" />
-
<!-- 每次释放连接的最大数目 -->
-
<property name="numTestsPerEvictionRun" value="100" />
-
<!-- 释放连接的扫描间隔(毫秒) -->
-
<property name="timeBetweenEvictionRunsMillis" value="3000" />
-
<!-- 连接最小空闲时间 -->
-
<property name="minEvictableIdleTimeMillis" value="1800000" />
-
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
-
<property name="softMinEvictableIdleTimeMillis" value="10000" />
-
<!-- 在获取连接的时候检查有效性, 默认false -->
-
<property name="testOnBorrow" value="true" />
-
<!-- 在空闲时检查有效性, 默认false -->
-
<property name="testWhileIdle" value="true" />
-
<!-- 在归还给pool时,是否提前进行validate操作 -->
-
<property name="testOnReturn" value="true" />
-
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
-
<property name="blockWhenExhausted" value="false" />
-
</bean>
-
<!-- jedis集群版配置 -->
-
<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port1}"/>
-
</bean>
-
<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port2}"/>
-
</bean>
-
<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port3}"/>
-
</bean>
-
<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port4}"/>
-
</bean>
-
<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port5}"/>
-
</bean>
-
<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port6}"/>
-
</bean>
-
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
-
<constructor-arg name="jedisClusterNode">
-
<set>
-
<ref bean="hostport1"/>
-
<ref bean="hostport2"/>
-
<ref bean="hostport3"/>
-
<ref bean="hostport4"/>
-
<ref bean="hostport5"/>
-
<ref bean="hostport6"/>
-
</set>
-
</constructor-arg>
-
<constructor-arg name="connectionTimeout" value="2000"/>
-
<constructor-arg name="soTimeout" value="2000"/>
-
<constructor-arg name="maxAttempts" value="3"/>
-
<constructor-arg name="password" value="${redis.clusterpassword}"/>
-
<constructor-arg name="poolConfig">
-
<ref bean="jedisConfig"/>
-
</constructor-arg>
-
</bean>
-
<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:
-
import org.codehaus.jackson.type.TypeReference;
-
import java.util.List;
-
/**
-
* Created by gzy on 2017/11/17 17:16.
-
*/
-
public interface JedisClient {
-
String get(String key);
-
<T> T get(String key, TypeReference<T> clazz);
-
<T> T get(String key, Class<T> clazz);
-
String get(String key, int select);
-
void setAndExpire(String key, Object o, int expire);
-
Long rpush(String key, String string);
-
// Long del(String... keys);
-
Long lpush(String key, String string);
-
void set(String key, Object o);
-
String set(String key, String value);
-
String hget(String hkey, String key);
-
long hset(String hkey, String key, String value);
-
long incr(String key);
-
long expire(String key, int second);
-
long ttl(String key);
-
long del(String key);
-
long hdel(String hkey, String key);
-
Boolean exists(String key);
-
Long decr(String key);
-
List<String> brpop(int timeout, String key);
-
}
jedisClient实现类JedisClientCluster:
-
/**
-
* Created by gzy on 2017/11/17 17:17.
-
*/
-
import org.codehaus.jackson.type.TypeReference;
-
import com.thinkgem.jeesite.common.utils.StringUtils;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import redis.clients.jedis.JedisCluster;
-
import java.util.List;
-
public class JedisClientCluster implements JedisClient {
-
@Autowired
-
private JedisCluster jedisCluster;
-
public <T> T get(String key, TypeReference<T> clazz) {
-
String json = jedisCluster.get(key);
-
if (StringUtils.isNotEmpty(json)) {
-
return JsonUtil.Json2Object(json, clazz);
-
} else {
-
return null;
-
}
-
}
-
public <T> T get(String key, Class<T> clazz) {
-
String json = jedisCluster.get(key);
-
if (StringUtils.isNotEmpty(json)) {
-
return JsonUtil.Json2Object(json, clazz);
-
} else {
-
return null;
-
}
-
}
-
public void set(String key, Object o) {
-
String json = JsonUtil.Object2Json(o);
-
jedisCluster.set(key, json);
-
}
-
public void setAndExpire(String key, Object o, int expire) {
-
String json = JsonUtil.Object2Json(o);
-
jedisCluster.set(key, json);
-
jedisCluster.expire(key, expire);
-
}
-
// public long del(String key) {
-
// return jedisCluster.del(key);
-
// }
-
public String get(String key) {
-
return jedisCluster.get(key);
-
}
-
public String get(String key, int select) {
-
jedisCluster.select(select);
-
return jedisCluster.get(key);
-
}
-
@Override
-
public String set(String key, String value) {
-
return jedisCluster.set(key, value);
-
}
-
@Override
-
public String hget(String hkey, String key) {
-
return jedisCluster.hget(hkey, key);
-
}
-
@Override
-
public long hset(String hkey, String key, String value) {
-
return jedisCluster.hset(hkey, key, value);
-
}
-
@Override
-
public long incr(String key) {
-
return jedisCluster.incr(key);
-
}
-
public Long decr(String key) {
-
return jedisCluster.decr(key);
-
}
-
@Override
-
public long expire(String key, int second) {
-
return jedisCluster.expire(key, second);
-
}
-
@Override
-
public long ttl(String key) {
-
return jedisCluster.ttl(key);
-
}
-
@Override
-
public long del(String key) {
-
return jedisCluster.del(key);
-
}
-
@Override
-
public long hdel(String hkey, String key) {
-
return jedisCluster.hdel(hkey, key);
-
}
-
public Long rpush(String key, String string) {
-
return jedisCluster.rpush(key, string);
-
}
-
public Long lpush(String key, String string) {
-
return jedisCluster.lpush(key, string);
-
}
-
public Boolean exists(String key) {
-
return jedisCluster.exists(key);
-
}
-
public List<String> brpop(int timeout, String key) {
-
return jedisCluster.brpop(timeout, key);
-
}
-
}
创建测试类ClusterTest:
-
import xx.xx.xxx.xxxx.JedisClient;
-
import org.junit.Test;
-
import org.junit.runner.RunWith;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.test.context.ContextConfiguration;
-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
/**
-
* Created by gzy on 2017/11/20 15:22.
-
*/
-
@RunWith(SpringJUnit4ClassRunner.class)
-
@ContextConfiguration("classpath:spring-context-cache.xml")
-
public class ClusterTest {
-
// private static JedisClientCluster redisCluster = SpringContextHolder.getBean("jedisClientCluster");
-
@Autowired
-
private JedisClient jedisClient;
-
@Test
-
public void testJCluster() {
-
jedisClient.set("test:phone:" + "11111111","hhha");
-
String result = jedisClient.get("test:phone:" + "11111111")
-
System.out.println("result==="+result)
-
}}
原文地址: