JedisCluster 操作集群Redis

使用Java API 实现

@Test

public void testJedisCluster() {

HashSet nodes = new HashSet<>();

nodes.add(new HostAndPort("192.168.10.131", 7001));

nodes.add(new HostAndPort("192.168.10.131", 7002));

nodes.add(new HostAndPort("192.168.10.131", 7003));

nodes.add(new HostAndPort("192.168.10.131", 7004));

nodes.add(new HostAndPort("192.168.10.131", 7005));

nodes.add(new HostAndPort("192.168.10.131", 7006));

JedisCluster cluster = new JedisCluster(nodes);

cluster.set("key1", "1000");

String string = cluster.get("key1");

System.out.println(string);

cluster.close();

}

 

2 Spring整合方式实现

spring配置文件spring/applicationContext-redis.xml

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<property name="maxTotal" value="30" />

<property name="maxIdle" value="10" />

<property name="numTestsPerEvictionRun" value="1024" />

<property name="timeBetweenEvictionRunsMillis" value="30000" />

<property name="minEvictableIdleTimeMillis" value="1800000" />

<property name="softMinEvictableIdleTimeMillis" value="10000" />

<property name="maxWaitMillis" value="1500" />

<property name="testOnBorrow" value="true" />

<property name="testWhileIdle" value="true" />

<property name="blockWhenExhausted" value="false" />

bean>

 

<bean id="redisClient" class="redis.clients.jedis.JedisCluster">

<constructor-arg name="nodes">

<set>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7001">constructor-arg>

bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7002">constructor-arg>

bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7003">constructor-arg>

bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7004">constructor-arg>

bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7005">constructor-arg>

bean>

<bean class="redis.clients.jedis.HostAndPort">

<constructor-arg name="host" value="192.168.10.131">constructor-arg>

<constructor-arg name="port" value="7006">constructor-arg>

bean>

set>

constructor-arg>

<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>

bean>

 

@Test

public void testSpringJedisCluster() {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");

JedisCluster jedisCluster =  (JedisCluster) applicationContext.getBean("redisClient");

String string = jedisCluster.get("key1");

System.out.println(string);

jedisCluster.close();

}

 

你可能感兴趣的:(Redis)