Java operating redis client Jedis connection to the cluster (Cluster)

Creating JedisCluster class connection redis clusters.

@Test 

public  void testJedisCluster () throws Exception { 

     // create a connection, JedisCluster objects, in the present embodiment is a single system, 

     the Set <HostAndPort> Nodes = new new HashSet <> (); 

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

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

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

     nodes.add ( new new HostAndPort (" 127.0. 0.1 ", 7004 )); 

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

     Nodes.add(new new HostAndPort ( "127.0.0.1", 7006 )); 

     JedisCluster Cluster = new new JedisCluster (Nodes); 

     // perform a method JedisCluster objects, methods, and redis-one correspondence. 

     cluster.set ( "Cluster-Test", "My Test jedis Cluster" ); 

     String Result = cluster.get ( "Cluster-Test" ); 

     System.out.println (Result); 

     // needs to close the end of the program objects JedisCluster 

     cluster.close (); 

}

 

Use spring

  • Configuration applicationContext.xml
<! - connection pool configuration -> 

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

     <! - the maximum number of connections -> 

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

     <! - the maximum number of idle connections -> 

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

     <! - the maximum number of connections per release -> 

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

     <! - scanning interval release of the connection (ms) -> 

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

     <-! connection minimum idle time -> 

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

     <-! connection release long idle, when the idle time> value and the immediate release when an idle connection> the maximum number of idle connections -> 

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

     <-! maximum number of milliseconds to wait to get a connection, less than zero: no blocking determined time, the default -1 -> 

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

     <! - check the validity of the connection at the time of acquisition, default false-> 

     < Property name = "testOnBorrow" value = "to true"  /> 

     <-! Check the validity when idle, default false -> 

     < Property name = "testWhileIdle" value = "to true"  /> 

     <! - - whether the obstruction connection exhaustion, false report abnormal, ture blocked until the timeout, default to true -> 

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

</ bean > 

<-! Redis cluster -> 

< bean the above mentioned id = "jedisCluster" class = "redis.clients.jedis.JedisCluster">

     <constructor-arg index="0">

         <set>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7001"></constructor-arg>

              </bean>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7002"></constructor-arg>

              </bean>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7003"></constructor-arg>

              </bean>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7004"></constructor-arg>

              </bean>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7005"></constructor-arg>

              </bean>

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

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7006"></constructor-arg>

              </bean>

         </set>

     </constructor-arg>

     <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>

</bean>
View Code

 

  • Test code
private ApplicationContext applicationContext;

     @Before

     public void init() {

         applicationContext = new ClassPathXmlApplicationContext(

                   "classpath:applicationContext.xml");

     }

 

     // redis集群

     @Test

     public void testJedisCluster() {

         JedisCluster jedisCluster = (JedisCluster) applicationContext

                   .getBean("jedisCluster");

         jedisCluster.set("name", "zhangsan");

         String value = jedisCluster.get("name");

         System.out.println(value);

     }

Guess you like

Origin www.cnblogs.com/royal6/p/12149651.html