Introduction of Jedis and analysis of common problems

Source: Internet
Author: User
Tags connection pooling redis cluster aliyun
Click to open the link
Jedis Introduction

Jedis is an open source Redis database client, compatible Redis 2.8.x and 3.xx,jedis provide the following features: Sort links manage different types of value command handle string type command handling hashes type command handling Lists type of command handling sets type of command handling Sorted sets type of command processing Transaction Batch command processing subscription/Publish persistent Control command Remote control command fragment (Md5,murmurhash) cluster's Key-tags function cluster batch command processing script Batch command processing the Redis cluster supports how to use the stable version code that downloads the official latest compilation: Http://github.com/xetorthio/jedis/releases directly in the MAVEN project the configuration dependencies are as follows:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactid>jedis</artifactid >
    <version>2.9.0</version>
    <type>jar</type>
    <scope>compile</ Scope>
</dependency>
Direct-attached mode
Import Redis.clients.jedis.Jedis;
public class Jedistest {public
static void Main (string[] args) {
    try {
        String host = ' xx.kvstore.aliyuncs.com "//console display access address
        int port = 6379;
        Jedis Jedis = new Jedis (host, port);
        Authentication information
        Jedis.auth ("password");//password
        String key = "Redis";
        String value = "Aliyun-redis";
        Select DB defaults to 0
        jedis.select (1);
        Set a key
        Jedis.set (key, value);
        System.out.println ("Set key" + key + "value:" + value);
        Get set in key
        String GetValue = Jedis.get (key);
        SYSTEM.OUT.PRINTLN ("Get key" + key + "ReturnValue:" + getvalue);
        Jedis.quit ();
        Jedis.close ();
    } catch (Exception e) {
        e.printstacktrace ();}}}

Connection pooling Mode
Jedispoolconfig config = new Jedispoolconfig ();
Maximum number of idle connections, apply your own evaluation, do not exceed the maximum number of connections for each instance of apsaradb for Redis
config.setmaxidle (MB);
Maximum number of connections, apply your own evaluation, do not exceed the maximum number of connections for each instance of apsaradb for Redis
config.setmaxtotal (+);
Config.settestonborrow (false);
Config.settestonreturn (false);
String host = "*.aliyuncs.com";
String password = "password";
Jedispool pool = new Jedispool (config, host, 6379, 3000, password);
Jedis Jedis = null;
try {
    Jedis = Pool.getresource ();
    ... do stuff ...
    for example jedis.set ("foo", "Bar");
    String foobar = jedis.get ("foo");
    Jedis.zadd ("Sose", 0, "car");
    Jedis.zadd ("Sose", 0, "bike");
    Set<string> sose = Jedis.zrange ("Sose", 0,-1);
} Finally {
    if (Jedis!= null) {
        jedis.close ();
    }
}
... when closing your application:
Pool.destroy ();
Common error issues troubleshooting connection pooling errors

The error that occurs when you use the Jedis connection pool mode is that you cannot get the connection pool

Redis.clients.jedis.exceptions.JedisConnectionException:Could not get a resource from the pool

For a number of reasons for this type of problem, you can troubleshoot network checks based on the following one by one

First check for network problems, you can use Telnet host 6379 for a simple test, auth password return to see if the +ok\r\n returned, if the correct return to continue to check the ping request or whether the read and write request returned normally, the operation to troubleshoot network problems. Jedispool Connection Number Setup check

Jedispool need to use the connection pool settings, users over the number of maxtotal connections will also appear to get the connection pool, this time can be accessed on the client side through Netstat-an | grep 6379 | grep EST | Wc-l View the number of linked client links and compare this number with the Maxtotal value of the Jedispool configuration, excluding the effect of Jedispool connection pool configuration if it is not significantly exceeded or approximated. Jedispool Connection Pool Code check

For Jedispool connection pooling operation, after each getresource call Returnresource or close to return, you can see if the code is properly used, code sample can refer to the above check whether the occurrence of nf_ Conntrack lost the bag

Check the client for exceptions by DMESG

Nf_conntrack:table full, dropping packet

If a nf_conntract packet loss occurs, you can sysctl-w net.netfilter.nf_conntrack_max=120000 check to see if the problem is time_wait by modifying settings

See if there are too many links by Ss-s

If time_wait too much, you can modify the following parameters

Sysctl-w net.ipv4.tcp_max_tw_buckets=180000
sysctl-w net.ipv4.tcp_tw_recycle=1
Check for DNS issues

By binding the host address directly in the/etc/hosts file, the binding is complete to see if the problem still exists, and if it does, it is not a DNS resolution problem

192.168.1.1  *.redis.rds.aliyuncs.com
Summarize

If you follow the above investigation and then there are problems can be caught by the bag and will be the error point, error information, grab package files sent to Aliyun after the sale of students to analyze. The grab command is the sudo tcpdump-i eth0 TCP and Port 6379-n-nn-s 74-w redis.cap.


Click to open the link

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.