Redis cluster, click Version and Spring integration steps

Source: Internet
Author: User
Tags redis cluster

1. Build the cluster, shut down the virtual machine firewall, or iptables parameters to allow the Redis cluster port to pass

Service iptables stop or Vim/etc/sysconfig/iptables

2. Test cluster (does not integrate spring)
@Test Public voidTestjediscluster () {HashSet<HostAndPort> nodes =NewHashset(); Nodes.Add (NewHostandport ("192.168.31.100", 7001)); Nodes.Add (NewHostandport ("192.168.31.100", 7002)); Nodes.Add (NewHostandport ("192.168.31.100", 7003)); Nodes.Add (NewHostandport ("192.168.31.100", 7004)); Nodes.Add (NewHostandport ("192.168.31.100", 7005)); Nodes.Add (NewHostandport ("192.168.31.100", 7006)); Jediscluster Cluster=Newjediscluster (nodes); Cluster.set ("Key1", "1000"); System.out.println (Cluster.get ("Key1"));        Cluster.close (); }

3. Configure the Spring file

  

<bean id= "Redisclient"class= "Redis.clients.jedis.JedisCluster" > <constructor-arg name= "Nodes" > <set> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7001" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7002" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7003" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7004" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7005" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.31.100" ></c                onstructor-arg> <constructor-arg name= "Port" value= "7006" ></constructor-arg> </bean> </set> </constructor-arg>
      <!--can be omitted --<constructor-arg name= "Poolconfig" ref= "Jedispoolconfig" ></constructor-arg> </bean>
<!--connection Pool configuration-<bean id= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig" > <!--Maximum number of connections--<property name= "maxtotal" value= ""/&gt        ; <!--maximum idle connections--<property name= "Maxidle" value= "/> <!--maximum number of connections per release--&LT;PR Operty name= "Numtestsperevictionrun" value= "1024x768"/> <!--release connection scanning interval (milliseconds)--<property name= "time Betweenevictionrunsmillis "value=" 30000 "/> <!--connection min idle time-<property name=" Minevictableidleti Memillis "value=" 1800000 "/> <!--how long the connection is idle and released when idle time > This value and idle connections > maximum idle Connections is released directly--<property n Ame= "Softminevictableidletimemillis" value= "10000"/> <!--maximum wait milliseconds to get a connection, less than 0: blocking indeterminate time, Default-1--< Property Name= "Maxwaitmillis" value= "/> <!--check validity when getting the connection, default false---<property name=" Te Stonborrow "value=" true "/> <!--check validity on idle, default false--<property name=" Testwhileidle "value=" tr      UE "/>  < If!--connection is exhausted, false to report an exception, ture block until timeout, default true--<property name= "blockwhenexhausted" value= "false"/> </bean>
4. Test the cluster (integrate spring)
@Test      Public void Testspringjediscluster () {        new classpathxmlapplicationcontext ("classpath:spring/ Applicationcontext-*.xml ");         =  (Jediscluster) Applicationcontext.getbean ("Redisclient");         = Jediscluster.get ("Key1");        System.out.println (string);        Jediscluster.close ();    }
5. Click Version Test (do not integrate spring)
@Test      Public void Testjedissingle () {        // Create a Jedis object        new Jedis ("192.168.31.100", 6379 );         // call the method of the Jedis object, the method name is consistent        with the Redis command Jedis.set ("Key1", "test01");        System.out.println (Jedis.get ("Key1"));         // Close Jedis                 jedis.close ();    }

6. Click Version Spring Configuration
<!--Jedis Client standalone version--    class= "Redis.clients.jedis.JedisPool" >        <constructor-arg name= " Host "value=" 192.168.31.100 "></constructor-arg>        <constructor-arg name=" Port "value=" 6379 ">< /constructor-arg>        <constructor-arg name= "Poolconfig" ref= "Jedispoolconfig" ></constructor-arg>    </bean>

7. Click Version Spring (Integration test)
@Test      Public void Testspringjedissingle () {        new classpathxmlapplicationcontext ("classpath:spring/ Applicationcontext-*.xml ");         = (Jedispool) applicationcontext.getbean ("Redisclient");         = Pool.getresource ();         = Jedis.get ("Key1");        System.out.println (string);        Jedis.close ();        Pool.close ();    }

  

Redis cluster, click Version and Spring integration steps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.