一、利用Jedis来实现
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("10.96.5.183",9001));
jedisClusterNodes.add(new HostAndPort("10.96.5.183",9002));
jedisClusterNodes.add(new HostAndPort("10.96.5.183",9003));
JedisCluster jc = new JedisCluster(jedisClusterNodes);
二、利用spring-data-redis来实现
<!--通过构造方法注入RedisNode-->
<bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="10.96.5.183" />
<constructor-arg value="9002" type="int" />
</bean>
....
<!--setter方式注入-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<ref bean="clusterRedisNodes1"/>
<ref bean="clusterRedisNodes2"/>
<ref bean="clusterRedisNodes3"/>
</set>
</property>
</bean>
<!--setter方式注入,对应的属性需存在setterXXX方法-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxToal" value="1000" />
<property name="maxIdle" value="1000" />
<property name="maxWaitMillis" value="1000" />
</bean>
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory" />
<!--setter方式注入PersonRepoImpl-->
<bean id="personRepo" class="com.example.repo.impl.PersonRepoImpl">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
三、简单集成Spring
自己编写jedisCluster的工厂类JedisClusterFactory,然后通过Spring注入的方式获取jedisCluster,实现客户端使用Redis3.0版本的集群特性。