从Resources 下 创建一个 cluster.xml 文件
配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- JedisPoolConfig
配置连接池 连接数最大30
-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="30"/>
</bean>
<!-- 配置6个 RedisNode
一个 node 就是一个主机
构造器的参数 分别为 host主机 和 post端口
-->
<bean id="node8001" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8001"/>
</bean>
<bean id="node8002" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8002"/>
</bean>
<bean id="node8003" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8003"/>
</bean>
<bean id="node8004" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8004"/>
</bean>
<bean id="node8005" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8005"/>
</bean>
<bean id="node8006" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.132.139"/>
<constructor-arg name="port" value="8006"/>
</bean>
<!-- 配置 RedisClusterConfigguration
配置集群 引用构造出来的主机
-->
<bean id="clusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<ref bean="node8001"/>
<ref bean="node8002"/>
<ref bean="node8003"/>
<ref bean="node8004"/>
<ref bean="node8005"/>
<ref bean="node8006"/>
</set>
</property>
</bean>
<!-- 配置JedisConnctionFactory
创建jedis 的链接工厂
构造器的参数分别引用 集群 和 连接池
-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="clusterConfiguration"/>
<constructor-arg name="poolConfig" ref="poolConfig"/>
</bean>
<!-- 配置StringRedisTemplate
创建模板引用 链接工厂
-->
<bean class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
package com.etoak;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
public class ClusterTest {
public static void main(String[] args) {
/*
* ApplicationContext spring 容器 为应用程序提供的中央接口,引用 cluster.xml中的bean配置
* */
ApplicationContext ioc = new ClassPathXmlApplicationContext("cluster.xml");
/* 得到spring 容器中的 Redis 模板 */
StringRedisTemplate template = ioc.getBean(StringRedisTemplate.class);
//zset
template.opsForZSet().add("set3","数学",100);
template.opsForZSet().add("set3","语文",110);
template.opsForZSet().add("set3","英语",90);
//zrange
template.opsForZSet().range("set3",0,-1).forEach(x -> System.out.println(x));
System.out.println("=======================");
template.opsForZSet().reverseRangeWithScores("set3",0,-1).forEach(tuple ->{
System.out.println(tuple.getValue() + ":"+ tuple.getScore());
});
}
}