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