Java code to operate Redis sentinel and Redis Cluster operations, redissentinel

Source: Internet
Author: User

[Switch] Java code to operate Redis sentinel and Redis Cluster operations, redissentinel

A total of four machines, crxy99 and crxy98 are master nodes and slave nodes. crxy97 and crxy96 are two sentinel nodes that monitor the master-slave architecture.

View the Code directly:

1 import org. junit. test; 2 3 import redis. clients. jedis. hostAndPort; 4 import redis. clients. jedis. jedis; 5 import redis. clients. jedis. jedisPoolConfig; 6 import redis. clients. jedis. jedisSentinelPool; 7 8 public class TestSentinel {9 @ Test10 public void test1 () {11 JedisPoolConfig poolConfig = new JedisPoolConfig (); 12 String masterName = "mymaster "; 13 Set <String> sentinels = new HashSet <String> (); 14 sentinels. add ("192.168.1.97: 26379"); 15 sentinels. add ("192.168.1.96: 26379"); 16 JedisSentinelPool jedisSentinelPool = new JedisSentinelPool (masterName, sentinels, poolConfig); 17 HostAndPort currentHostMaster = jedisSentinelPool. getCurrentHostMaster (); 18 System. out. println (currentHostMaster. getHost () + "--" + currentHostMaster. getPort (); // get the information of the master node 19 Jedis resource = jedisSentinelPool. getResource (); 20 String value = resource. get ("a"); 21 System. out. println (value); // obtain the value 22 resource corresponding to key. close (); 23} 24 25}

Run the following result:

192.168.1.99--63791

Jedis operation cluster ....

Simulate the cluster environment. Start Multiple redis instances on one machine. Each redis instance corresponds to a different port.

Started on crxy99 192.168.1.99... a total of 3 master 3 slave port numbers correspond to 7000 ~ 7005 .....

Check the Code:

1 import java. util. hashSet; 2 import java. util. set; 3 import org. junit. test; 4 import redis. clients. jedis. hostAndPort; 5 import redis. clients. jedis. jedisCluster; 6 import redis. clients. jedis. jedisPoolConfig; 7 8 public class TestCluster {9 @ Test10 public void test1 () throws Exception {11 JedisPoolConfig poolConfig = new JedisPoolConfig (); 12 Set <HostAndPort> nodes = new HashSet <HostAndPort> (); 13 HostAndPort hostAndPort = new HostAndPort ("192.168.1.99", 7000); 14 HostAndPort hostAndPort1 = new HostAndPort ("192.168.1.99 ", 7001); 15 HostAndPort hostAndPort2 = new HostAndPort ("192.168.1.99", 7002); 16 HostAndPort hostAndPort3 = new HostAndPort ("192.168.1.99", 7003 ); 17 HostAndPort hostAndPort4 = new HostAndPort ("192.168.1.99", 7004); 18 HostAndPort hostAndPort5 = new HostAndPort ("192.168.1.99", 7005); 19 nodes. add (hostAndPort); 20 nodes. add (hostAndPort1); 21 nodes. add (hostAndPort2); 22 nodes. add (hostAndPort3); 23 nodes. add (hostAndPort4); 24 nodes. add (hostAndPort5); 25 JedisCluster jedisCluster = new JedisCluster (nodes, poolConfig); // The connection pool is split by default in JedisCluster. 26 // redis creates a connection pool internally, obtains the connection usage from the connection pool, and then returns the connection to the connection pool 27 String string = jedisCluster. get ("a"); 28 System. out. println (string); 29} 30}

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.