Jedis-Jedis connection pool (optimization)

1. Overview of Jedis (tool class for java client to operate Redis)

   Jedis is a Java connection development tool officially recommended by Redis . In addition to using commands, basically mainstream languages ​​now have client support. The main Java client is Jedis. Jedis provides complete Redis commands, while Redisson has more distributed container implementations.

   Java operation Redis tool class: Jedis

Jedis connection pool uses
   jedis connection pool based on apache-commons pool2. When constructing the connection pool object, you need to provide the configuration object of the pool object, and JedisPoolConfig (inherited from GenericObjectPoolConfig). We can configure the relevant parameters of the connection pool through this configuration object (such as the maximum number of connections, the maximum number of empty, etc.).

   j two jar packages
Insert picture description here

1.1 Test Jedis connection to Redis operation

public class TestJedis {
    
    
    @Test
    public void test01(){
    
    
        //1:创建一个Jedis对象   创建一个连接,参1 ip地址(Redis服务器的ip地址) 参2 端口
        Jedis jedis = new Jedis("localhost", 6379);
        //2:执行Redis的指令(set指令)
        //写入数据
        jedis.set("name","[email protected]");//添加 string-string
        jedis.sadd("set","hello","[email protected]","Welcome to use");//添加string-set集合类型
        //读取数据
        System.out.println(jedis.get("name"));
        System.out.println(jedis.smembers("set"));
        //关闭连接
        jedis.close();
    }
}

operation result:

[email protected]
[hello, Welcome to use, [email protected]]

2. Jedis connection pool principle (space for time is equivalent to a collection)

Advantages of connection pool:

Because the frequent creation and destruction of connections is very performance consuming (TCP/IP three-way handshake and four waves are very performance consuming), the connection pool is to solve the problem of performance consumption caused by frequent resource allocation and release.
Method: Create a collection to initialize multiple Connections, call the getConnection() method to apply for a connection when needed, and call close() to put it back into the connection pool after use. (The close() here is to put back into the connection pool, not to close the connection)

2.1 Jedis connection pool implementation (hard-coded)

Hard-coded
configuration parameters are written in java code, compiled into a class, you must modify the java code in the future to change the configuration

public class TestJedisPool {
    
    
    @Test
    public void test01(){
    
    

//        1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大链接数
        config.setMaxTotal(20);
        //设置空闲连接数
        config.setMaxIdle(10);
//        2:创建连接池 参1:Redis服务器ip地址  参2:端口号
        JedisPool pool = new JedisPool(config, "localhost", 6379);
//        3:从连接池中获取一个连接
        Jedis jedis = pool.getResource();//获取一个连接
//        4:执行Redis执行  Map<String,String>   set   get
        jedis.set("name","JedisPool连接池");
        String value = jedis.get("name");
        System.out.println(value);
//        5:释放连接
        jedis.close();
    }
}

operation result:

JedisPool connection pool



2.2 Jedis connection pool optimization (written as a tool class call)

In the project, use the configuration file to configure the parameters, you do not need to modify the java code in the future, directly change the parameters in the file.

  1. Write test class (business logic)
//Jedis连接池优化(JedisPool优化)
public class TestJedisUtils {
    
    
    @Test
    public void test01(){
    
    
        //1:从连接池获取连接
        Jedis jedis = JedisUtils.getRedis();
        //2: 进行读写操作
        jedis.set("name","JedisPool连接池优化JedisUtils");
        
        String value = jedis.get("name");
        System.out.println(value);
        //3: 关闭连接
        JedisUtils.close(jedis);
    }
}
  1. Create a properties file to store parameter variables (jedis.properties)
    //The properties file contains data in the form of key-value pairs, which can be read using a special reading tool Properties class
maxTotal=50
maxIdle=15
url=localhost
port=6379
  1. Tool class for creating jedis connection pool (read files in .properties)

  The way to read the .properties file:

  • You can use the class loader to load the stream first, and then use the Properties class to read .
  • You can also use ResourceBundle to read .propties

3.1 Use the class loader to load into a stream, and then use the Properties class to read

public class JedisUtils {
    
    
    //单例模式 静态代码块只执行一次,因为创建会很消耗性能
    private static JedisPool pool;
    //静态代码在项目中,如果被使用只会加载一次
    static {
    
    

        //读src下的文件用类加载器的方式  - 类加载器去加载成流,再使用Properties类来读
        InputStream inputStream= JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");

        //.properties文件专门的读取工具
        Properties properties = new Properties();
        try {
    
    
            //将流中的数据读成map
            properties.load(inputStream);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        // 1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大链接数
        config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
        //设置空闲连接数  "3"
        config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
        //2:创建连接池
         pool = new JedisPool(config, properties.getProperty("url"), Integer.parseInt(properties.getProperty("port")));
    }
    public static Jedis getRedis() {
    
    
//        3:从连接池中获取一个连接
        Jedis jedis = pool.getResource();//获取一个连接
        return jedis;
    }

    public static void close(Jedis jedis) {
    
    
        if(jedis!=null){
    
    
            jedis.close();
        }
    }
}

3.2 Use ResourceBundle to read .propties

public class JedisUtils {
    
    
    private static JedisPool pool = null;
    //1:创建一个连接池
    static{
    
    
        //1.1 解析properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //获取参数
        String maxTotal = bundle.getString("maxTotal");
        String maxIdle = bundle.getString("maxIdle");
        String url = bundle.getString("url");
        String port = bundle.getString("port");

        //1.2创建连接池
        //1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //1.1 设置最大连接数
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //1.2 设置空闲连接数
        config.setMaxIdle(Integer.parseInt(maxIdle));

        //2:创建连接池
         pool = new JedisPool(config, url, Integer.parseInt(port));
    }

    //2:对外提供一个获取连接的方法
    public static Jedis getJedis(){
    
    
        return  pool.getResource();
    }

    //3:提供释放资源的方法
    public static void close(Jedis jedis){
    
    
        if(jedis != null) {
    
    
            jedis.close();
        }
    }
}

operation result:

JedisPool connection pool optimization JedisUtils

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108730660