Example of using Java to connect to Redis

Source: Internet
Author: User
Tags redis server

Using Jedis in multithreaded situations

Strange errors occur when using the same Jedis instance in different threads. But it's not good to create too many implementations because it means there are many sokcet connections that can cause strange errors to occur. A single Jedis instance is not thread-safe. To avoid these problems, you can use Jedispool, Jedispool is a thread-safe network connection pool. You can use Jedispool to create some reliable Jedis instances, and you can get Jedis instances from the pool. This approach solves those problems and allows for efficient performance.

Initialize Jedispool

New Jedispool (new jedispoolconfig (), "localhost");

The above code can be handled statically, and it is thread-safe.

Static {        new jedispool (new Config (), "host", 6379);    }

Jedispoolconfig contains the default parameters for many useful redis-specified connection pools. For example, if a connection does not have any return Jedis within 300 seconds, the connection will be closed.

This can be used:

 Jedis Jedis = Pool.getresource ();  try   {  do something for re    Operation of Dis  jedis.set ("foo", "Bar"  = Jedis.get ("foo" ); Jedis.zadd ( "Sose", 0, "car");     Jedis.zadd ("Sose", 0, "bike"  <String> sose = Jedis.zrange ("Sose", 0, -1);  finally   { //   //  When the program closes, you need to call the Close method  Pool.destroy ();

Set master/Slave distribution

Enable synchronous replication

Redis is primarily built for master/slave distribution. This means that the "write" request must point to "master", and "master" will replicate the changed content to "slave" synchronously. The "read" request can (not be required) be pointed to "slave", easing the read and write pressure of "master".

You can use "master" as the following steps. In order to enable synchronous replication, there are two ways to tell "slave" to "slaveof" to a given "master":


1. The config file (redis.conf) on the Redis server indicates
2. Call the "slaveof" method in the received Jedis instance and specify the IP and port

Jedis.slaveof ("192.168.1.35", 6379);

Note: "Slave" is also a Redis server and can receive "write" requests without error, but changes will not be replicated synchronously, so if you reverse the Jedis instance then some operations will be overwritten.

When you disable synchronous replication/master fails, promote slave
If "Master" is down, you can promote "slave" to become the new "master". First try disabling synchronous replication Offline "Master", if there are several "slave", enable synchronous replication of the remaining "slave" to the new "master".

Slave1jedis.slaveofnoone (); Slave2jedis.slaveof ("192.168.1.36", 6379);

 

Example of using Java to connect to Redis

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.