本文分两部分:
(1)jedisCluster
(2)Spring-Data-Redis集群配置和RedisTemplate用法
我这里是在本机上搭建的3主3从6个redis实例。
jar引入:
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.4.2version>
dependency>
连接集群测试:
@Test
public void testJedisCluster() throws Exception {
// 第一步:使用JedisCluster对象。需要一个Set参数。Redis节点的列表。
Set nodes = new HashSet<>();
nodes.add(new HostAndPort("127.0.0.1", 7001));
nodes.add(new HostAndPort("127.0.0.1", 7002));
nodes.add(new HostAndPort("127.0.0.1", 7003));
nodes.add(new HostAndPort("127.0.0.1", 7004));
nodes.add(new HostAndPort("127.0.0.1", 7005));
nodes.add(new HostAndPort("127.0.0.1", 7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
// 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
jedisCluster.set("a", "1");
String result = jedisCluster.get("a");
// 第三步:打印结果
System.out.println(result);
// 第四步:系统关闭前,关闭JedisCluster对象。
jedisCluster.close();
}
基于Spring的配置:
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7001">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7002">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7003">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7004">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7005">constructor-arg>
bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7006">constructor-arg>
bean>
set>
constructor-arg>
bean>
<bean id="jedisClientCluster" class="com.demo.jedis.JedisClientCluster"/>
接口定义:
public interface JedisClient {
String set(String key, String value);
String get(String key);
Boolean exists(String key);
Long expire(String key, int seconds);
Long ttl(String key);
Long incr(String key);
Long hset(String key, String field, String value);
String hget(String key, String field);
Long hdel(String key, String... field);
}
实现类:
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster;
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
}
@Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
}
@Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
}
@Override
public Long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public Long hset(String key, String field, String value) {
return jedisCluster.hset(key, field, value);
}
@Override
public String hget(String key, String field) {
return jedisCluster.hget(key, field);
}
@Override
public Long hdel(String key, String... field) {
return jedisCluster.hdel(key, field);
}
}
最后可以在项目中直接使用接口类。
注意版本与spring版本最好相差不多,不然会报错。
jar:
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.11.RELEASEversion>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.2version>
dependency>
spring-data-redis.xml配置:
<description>Jedis Cluster Configurationdescription>
<context:property-placeholder
ignore-unresolvable="true" location="classpath:redis.cluster.properties" />
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="3">property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7001">constructor-arg>
bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7002">constructor-arg>
bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7003">constructor-arg>
bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7004">constructor-arg>
bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7005">constructor-arg>
bean>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="7006">constructor-arg>
bean>
set>
property>
bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="100" />
<property name="maxTotal" value="600" />
bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory" />
bean>
以上就是全部配置,可以使用模板操作:
@Autowired
private RedisTemplate redisTemplate;