Java Connect Redis Cluster

Source: Internet
Author: User
Tags redis cluster

http://m.blog.csdn.net/koushr/article/details/50956870

Connect to a Redis database (non-clustered mode)

 Public Static voidMain (string[] args) {jedispoolconfig poolconfig=NewJedispoolconfig (); //Maximum number of connectionsPoolconfig.setmaxtotal (2); //Maximum idle numberPoolconfig.setmaxidle (2); //The maximum allowable wait time, if the connection is not obtained at this time, the jedisexception exception is reported://Could not get a resource from the poolPoolconfig.setmaxwaitmillis (1000); Jedispool Pool=NewJedispool (Poolconfig, "192.168.83.128", 6379, 0, "123"); Jedis Jedis=NULL; Try {         for(inti = 0; I < 5; i++) {Jedis=Pool.getresource (); Jedis.set ("Foo" + I, "bar" +i); System.out.println ("First" + (i + 1) + "Connect, get the value" + Jedis.get ("foo" +i)); //Be sure to release the connection when you're doneJedis.close (); }    } finally{pool.close (); }}

Connection Rediscluster (cluster mode)

 Public Static voidMain (string[] args) {jedispoolconfig poolconfig=NewJedispoolconfig (); //Maximum number of connectionsPoolconfig.setmaxtotal (1); //Maximum idle numberPoolconfig.setmaxidle (1); //The maximum allowable wait time, if the connection is not obtained at this time, the jedisexception exception is reported://Could not get a resource from the poolPoolconfig.setmaxwaitmillis (1000); Set<HostAndPort> nodes =NewLinkedhashset(); Nodes.Add (NewHostandport ("192.168.83.128", 6379)); Nodes.Add (NewHostandport ("192.168.83.128", 6380)); Nodes.Add (NewHostandport ("192.168.83.128", 6381)); Nodes.Add (NewHostandport ("192.168.83.128", 6382)); Nodes.Add (NewHostandport ("192.168.83.128", 6383)); Nodes.Add (NewHostandport ("192.168.83.128", 6384)); Jediscluster Cluster=Newjediscluster (nodes, poolconfig); String name= Cluster.get ("name");    SYSTEM.OUT.PRINTLN (name); Cluster.set ("Age", "18"); System.out.println (Cluster.get ("Age")); Try{cluster.close (); } Catch(IOException e) {e.printstacktrace (); }}

Java Connect Redis Cluster

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.