-
-
<groupId>redis.clients</groupId>
-
<artifactId>jedis</artifactId>
-
-
-
-
<groupId>org.apache.commons</groupId>
-
<artifactId>commons-pool2</artifactId>
-
-
版本: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" />
-
-
<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" />
-
-
<property name="testOnBorrow" value="true" />
-
-
<property name="testWhileIdle" value="true" />
-
-
<property name="testOnReturn" value="true" />
-
-
<property name="blockWhenExhausted" value="false" />
-
-
-
-
-
<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port1}"/>
-
-
-
<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port2}"/>
-
-
-
<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port3}"/>
-
-
-
<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port4}"/>
-
-
-
<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port5}"/>
-
-
-
<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
-
<constructor-arg name="host" value="${redis.host}"/>
-
<constructor-arg name="port" value="${redis.port6}"/>
-
-
-
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
-
<constructor-arg name="jedisClusterNode">
-
-
-
-
-
-
-
-
-
-
<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"/>
-
-
-
<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;
-
-
-
-
-
-
-
-
public interface JedisClient {
-
-
-
<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 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 expire(String key, int second);
-
-
-
-
-
-
long hdel(String hkey, String key);
-
-
Boolean exists(String key);
-
-
-
-
List<String> brpop(int timeout, String key);
-
-
jedisClient实现类JedisClientCluster:
-
-
-
-
-
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;
-
-
-
-
public class JedisClientCluster implements JedisClient {
-
-
-
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);
-
-
-
-
-
-
public <T> T get(String key, Class<T> clazz) {
-
String json = jedisCluster.get(key);
-
if (StringUtils.isNotEmpty(json)) {
-
return JsonUtil.Json2Object(json, clazz);
-
-
-
-
-
-
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 String get(String key) {
-
return jedisCluster.get(key);
-
-
-
public String get(String key, int select) {
-
jedisCluster.select(select);
-
return jedisCluster.get(key);
-
-
-
-
public String set(String key, String value) {
-
return jedisCluster.set(key, value);
-
-
-
-
public String hget(String hkey, String key) {
-
return jedisCluster.hget(hkey, key);
-
-
-
-
public long hset(String hkey, String key, String value) {
-
return jedisCluster.hset(hkey, key, value);
-
-
-
-
public long incr(String key) {
-
return jedisCluster.incr(key);
-
-
-
public Long decr(String key) {
-
return jedisCluster.decr(key);
-
-
-
-
public long expire(String key, int second) {
-
return jedisCluster.expire(key, second);
-
-
-
-
public long ttl(String key) {
-
return jedisCluster.ttl(key);
-
-
-
-
public long del(String key) {
-
return jedisCluster.del(key);
-
-
-
-
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.runner.RunWith;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.test.context.ContextConfiguration;
-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-
-
-
-
@RunWith(SpringJUnit4ClassRunner.class)
-
@ContextConfiguration("classpath:spring-context-cache.xml")
-
public class ClusterTest {
-
-
-
private JedisClient jedisClient;
-
-
-
public void testJCluster() {
-
jedisClient.set("test:phone:" + "11111111","hhha");
-
String result = jedisClient.get("test:phone:" + "11111111")
-
System.out.println("result==="+result)
-
-
原文地址: https://blog.csdn.net/gzy_0922/article/details/78602716
注意:Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.