1.建立maven项目pox.xml导入依赖包

           <dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>

2.建立连接

public class RedisConnectionDemoA {
public static final String REDIS_ADDRESS = "redis://"自己设置的验证信息"@redis-server:6379/0"; public static void main(String[] args) {
RedisURI redisURI = RedisURI.create(REDIS_ADDRESS);
RedisClient redisClient = RedisClient.create(redisURI);
StatefulRedisConnection<String,String> connect = redisClient.connect();
System.out.println("【连接返回】"+connect);
connect.close();
redisClient.shutdown();
}
}

3.创建连接池管理

public class RedisConnectionPool {
private static final int MAX_IDLE = 10 ; // 最大的维持连接数量
private static final int MIN_IDLE = 1 ; // 最小维持的可用数量
private static final int MAX_TOTAL = 1 ; // 最大的可用数量
private static final boolean TEST_ON_BORROW = true ;
public static void main(String[] args) throws Exception {
// 1、如果要进行连接池的操作,则肯定要进行一些连接池的基本配置
GenericObjectPoolConfig config = new GenericObjectPoolConfig() ; // 配置对象
config.setMaxIdle(MAX_IDLE); // 设置最大维持连接数量
config.setMinIdle(MIN_IDLE); // 设置最小维持连接数量
config.setMaxTotal(MAX_TOTAL); // 连接池总共的可用连接数量
config.setTestOnBorrow(TEST_ON_BORROW); // 连接测试后返回
// 2、连接池的创建需要依赖于连接的配置类实例
GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(() -> RedisConnectionUtil.getConnection(), config);
for (int x = 0 ; x < 10 ; x ++) {
StatefulRedisConnection<String, String> connection = pool.borrowObject();// 通过连接池获取一个连接
System.out.println("【连接池对象】" + connection);
// 发出一个“ping”命令
System.out.println("【测试连接】ping = " + connection.sync().ping());
connection.close();
}
}
}

4.连接池工具类。

public class RedisConnectionPool {
private static final int MAX_IDLE = 10 ; // 最大的维持连接数量
private static final int MIN_IDLE = 1 ; // 最小维持的可用数量
private static final int MAX_TOTAL = 1 ; // 最大的可用数量
private static final boolean TEST_ON_BORROW = true ;
public static void main(String[] args) throws Exception {
// 1、如果要进行连接池的操作,则肯定要进行一些连接池的基本配置
GenericObjectPoolConfig config = new GenericObjectPoolConfig() ; // 配置对象
config.setMaxIdle(MAX_IDLE); // 设置最大维持连接数量
config.setMinIdle(MIN_IDLE); // 设置最小维持连接数量
config.setMaxTotal(MAX_TOTAL); // 连接池总共的可用连接数量
config.setTestOnBorrow(TEST_ON_BORROW); // 连接测试后返回
// 2、连接池的创建需要依赖于连接的配置类实例
GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(() -> RedisConnectionUtil.getConnection(), config);
for (int x = 0 ; x < 10 ; x ++) {
StatefulRedisConnection<String, String> connection = pool.borrowObject();// 通过连接池获取一个连接
System.out.println("【连接池对象】" + connection);
// 发出一个“ping”命令
System.out.println("【测试连接】ping = " + connection.sync().ping());
connection.close();
}
}
}

最新文章

  1. 如何使用RobotFramework编写好的测试用例
  2. Linux如何查找大文件或目录总结
  3. 让文档和Demo生成更加简单和强大 - SmartDoc 0.1.1 说明
  4. 用thinkphp进行微信开发的整体设计思考
  5. android在view.requestFocus(0)返回false的解决办法
  6. linux文件特殊属性介绍(s,s,t)
  7. Java 伪静态 Mapping
  8. 使用 FLASH DATABASE 恢复误删除的用户
  9. 开始ITGEGE教育社区的视频录制----嵌入式基础知识讲解
  10. 2、使用rpm包安装grafana
  11. Laravel--查询构造器 方法 及聚合函数
  12. flash设置里面:您的 Flash 设置会一直保留到您退出 Chrome 为止。
  13. HDU 1035(走迷宫 模拟)
  14. Workbox 缓存
  15. python学习笔记_week15
  16. Java 判断当前系统为Window或者Linux
  17. linux修改hosts
  18. React Router基础教程
  19. ExtJS 4.2 教程-07:Ext.Direct
  20. sublime text配置fcitx输入法

热门文章

  1. MessagePack Java Jackson Dataformat - 安装
  2. flask框架(九): 请求和响应扩展以及中间件
  3. vfprintf()函数
  4. Codeforces 474D Flowers(DP)
  5. Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/Cppcc] due to a StackOverflowError. Possible root causes include a too low setting for -Xs
  6. C++入门经典-友元
  7. mysql 日常操作-DDL
  8. 伪分布式下Hadoop3.2版本打不开localhost:50070,可以打开localhost:8088
  9. Python定时框架 Apscheduler 详解【转】
  10. Cortex-M3 SVC与PendSV